Skip to content

Commit 301bd54

Browse files
committed
Add replicate
1 parent b376ae8 commit 301bd54

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

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)