on master help sign in
Define convenience function List.replicate.
commitd6e7172f45a8dab2a119af70982d0ece0f1c9b8c authorAlexander Noriega <lambdatoast@gmail.com> dateSat 31 Aug 2013, 16:29 (13 years ago) parent1a45cb5
2 files changed+15−1
libraries/List.elm+4 libraries/Native/List.js+11−1
libraries/List.elm+4
@@ -185,3 +185,7 @@ take = Native.List.take
185185-- Drop the first n members of a list: `(drop 2 [1,2,3,4] == [3,4])`
186186drop : Int -> [a] -> [a]
187187drop = Native.List.drop
188+
189+-- Creates a list with n copies of a value: `(replicate 3 (0,0) == [(0,0),(0,0),(0,0)]`
190+replicate : Int -> a -> [a]
191+replicate = Native.List.replicate
libraries/Native/List.js+11−1
@@ -241,6 +241,15 @@ Elm.Native.List = function(elm) {
241241 return xs;
242242 }
243243
244+ function replicate(n, a) {
245+ var arr = [];
246+ while (n > 0) {
247+ arr.push(a);
248+ --n;
249+ }
250+ return fromArray(arr);
251+ }
252+
244253 function join(sep, xss) {
245254 if (typeof sep === 'string') return toArray(xss).join(sep);
246255 if (xss.ctor === '[]') return Nil;
@@ -344,10 +353,11 @@ Elm.Native.List = function(elm) {
344353 nth:F2(nth),
345354 take:F2(take),
346355 drop:F2(drop),
356+ replicate:F2(replicate),
347357
348358 join:F2(join),
349359 split:F2(split)
350360 };
351361 return elm.Native.List = Elm.Native.List.values;
352362
353−};
363+};