From 301bd5410e46335a5b0afbbea789b2d77f2a935b Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Tue, 31 Mar 2015 22:48:47 +0200 Subject: [PATCH 1/2] Add replicate --- src/Data/Array.purs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/Data/Array.purs b/src/Data/Array.purs index f3a70e7b..f3ed7b06 100644 --- a/src/Data/Array.purs +++ b/src/Data/Array.purs @@ -51,6 +51,7 @@ module Data.Array , span , dropWhile , takeWhile + , replicate ) where import Control.Alt @@ -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 From 212a11adcbe3e916edc8a337b76b29401f9f6cbe Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Tue, 31 Mar 2015 22:50:21 +0200 Subject: [PATCH 2/2] Update docs. --- README.md | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index cfc3a3c9..4dc171e2 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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. \ No newline at end of file +Running time: `O(n)`, where `n` is the length of the array. + + +