Skip to content

Commit 7d21191

Browse files
committed
Merge pull request #35 from jacereda/replicate
Replicate
2 parents a94f609 + 212a11a commit 7d21191

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,14 @@ dropWhile :: forall a. (a -> Boolean) -> [a] -> [a]
431431
Remove the longest initial subarray for which all element satisfy the specified predicate,
432432
creating a new array.
433433

434+
#### `replicate`
435+
436+
``` purescript
437+
replicate :: forall a. Number -> a -> [a]
438+
```
439+
440+
Create an array with repeated instances of a value.
441+
434442
#### `functorArray`
435443

436444
``` purescript
@@ -662,4 +670,7 @@ init :: forall a. [a] -> [a]
662670

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

665-
Running time: `O(n)`, where `n` is the length of the array.
673+
Running time: `O(n)`, where `n` is the length of the array.
674+
675+
676+

src/Data/Array.purs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ module Data.Array
5151
, span
5252
, dropWhile
5353
, takeWhile
54+
, replicate
5455
) where
5556

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

483+
484+
-- | Create an array with repeated instances of a value.
485+
foreign import replicate
486+
"""
487+
function replicate(nn) {
488+
return function(v) {
489+
var n = nn > 0? nn : 0;
490+
var r = new Array(n);
491+
for (var i = 0; i < n; i++)
492+
r[i] = v;
493+
return r;
494+
};
495+
}
496+
""" :: forall a. Number -> a -> [a]
497+
482498
instance functorArray :: Functor [] where
483499
(<$>) = map
484500

0 commit comments

Comments
 (0)