Skip to content

Commit

Permalink
Merge pull request #35 from jacereda/replicate
Browse files Browse the repository at this point in the history
Replicate
  • Loading branch information
paf31 committed Apr 1, 2015
2 parents a94f609 + 212a11a commit 7d21191
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,14 @@ dropWhile :: forall a. (a -> Boolean) -> [a] -> [a]
Remove the longest initial subarray for which all element satisfy the specified predicate,
creating a new array.

#### `replicate`

``` purescript
replicate :: forall a. Number -> a -> [a]
```

Create an array with repeated instances of a value.

#### `functorArray`

``` purescript
Expand Down Expand Up @@ -662,4 +670,7 @@ init :: forall a. [a] -> [a]

Get all but the last element of a non-empty array.

Running time: `O(n)`, where `n` is the length of the array.
Running time: `O(n)`, where `n` is the length of the array.



16 changes: 16 additions & 0 deletions src/Data/Array.purs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ module Data.Array
, span
, dropWhile
, takeWhile
, replicate
) where

import Control.Alt
Expand Down Expand Up @@ -479,6 +480,21 @@ takeWhile p xs = (span p xs).init
dropWhile :: forall a. (a -> Boolean) -> [a] -> [a]
dropWhile p xs = (span p xs).rest


-- | Create an array with repeated instances of a value.
foreign import replicate
"""
function replicate(nn) {
return function(v) {
var n = nn > 0? nn : 0;
var r = new Array(n);
for (var i = 0; i < n; i++)
r[i] = v;
return r;
};
}
""" :: forall a. Number -> a -> [a]

instance functorArray :: Functor [] where
(<$>) = map

Expand Down

0 comments on commit 7d21191

Please sign in to comment.