Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions docs/Data.Array.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,30 @@ for many use cases due to their poor asymptotics.
This operator provides a safe way to read a value at a particular index
from an array.

#### `cons`

``` purescript
cons :: forall a. a -> [a] -> [a]
```

Attaches an element to the front of an array, creating a new array.

```purescript
cons 1 [2, 3, 4] = [1, 2, 3, 4]
```

Note, the running time of this function is `O(n)`.

#### `(:)`

``` purescript
(:) :: forall a. a -> [a] -> [a]
```

An infix alias for `cons`.

Note, the running time of this function is `O(n)`.

#### `snoc`

``` purescript
Expand Down
26 changes: 26 additions & 0 deletions src/Data/Array.purs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
module Data.Array
( (!!)
, (..)
, cons
, (:)
, snoc
, singleton
, head
Expand Down Expand Up @@ -72,6 +74,30 @@ infixl 8 !!
(!!) xs n | n < zero || n >= length xs = Nothing
| otherwise = Just (xs `unsafeIndex` toNumber n)

-- | Attaches an element to the front of an array, creating a new array.
-- |
-- | ```purescript
-- | cons 1 [2, 3, 4] = [1, 2, 3, 4]
-- | ```
-- |
-- | Note, the running time of this function is `O(n)`.
foreign import cons
"""
function cons(e) {
return function(l) {
return [e].concat(l);
};
}
""" :: forall a. a -> [a] -> [a]

infixr 6 :

-- | An infix alias for `cons`.
-- |
-- | Note, the running time of this function is `O(n)`.
(:) :: forall a. a -> [a] -> [a]
(:) = cons

-- | Append an element to the end of an array, creating a new array.
foreign import snoc
"""
Expand Down