lib.lists: add replicate
`replicate` returns n copies of an element as a list. Co-Authored-By: Silvan Mosberger <contact@infinisil.com>
This commit is contained in:
parent
41169b15c5
commit
7c4abbf80e
3 changed files with 18 additions and 1 deletions
|
@ -87,7 +87,7 @@ let
|
|||
updateManyAttrsByPath;
|
||||
inherit (self.lists) singleton forEach foldr fold foldl foldl' imap0 imap1
|
||||
concatMap flatten remove findSingle findFirst any all count
|
||||
optional optionals toList range partition zipListsWith zipLists
|
||||
optional optionals toList range replicate partition zipListsWith zipLists
|
||||
reverseList listDfs toposort sort naturalSort compareLists take
|
||||
drop sublist last init crossLists unique intersectLists
|
||||
subtractLists mutuallyExclusive groupBy groupBy';
|
||||
|
|
|
@ -303,6 +303,18 @@ rec {
|
|||
else
|
||||
genList (n: first + n) (last - first + 1);
|
||||
|
||||
/* Return a list with `n` copies of an element.
|
||||
|
||||
Type: replicate :: int -> a -> [a]
|
||||
|
||||
Example:
|
||||
replicate 3 "a"
|
||||
=> [ "a" "a" "a" ]
|
||||
replicate 2 true
|
||||
=> [ true true ]
|
||||
*/
|
||||
replicate = n: elem: genList (_: elem) n;
|
||||
|
||||
/* Splits the elements of a list in two lists, `right` and
|
||||
`wrong`, depending on the evaluation of a predicate.
|
||||
|
||||
|
|
|
@ -459,6 +459,11 @@ runTests {
|
|||
expected = [2 30 40 42];
|
||||
};
|
||||
|
||||
testReplicate = {
|
||||
expr = replicate 3 "a";
|
||||
expected = ["a" "a" "a"];
|
||||
};
|
||||
|
||||
testToIntShouldConvertStringToInt = {
|
||||
expr = toInt "27";
|
||||
expected = 27;
|
||||
|
|
Loading…
Reference in a new issue