···
7
-
inherit (builtins) head tail length isList elemAt concatLists filter elem;
7
+
inherit (builtins) head tail length isList elemAt concatLists filter elem genList;
# Create a list consisting of a single element. `singleton x' is
···
foldl' = builtins.foldl' or foldl;
45
-
# map with index: `imap (i: v: "${v}-${toString i}") ["a" "b"] ==
53
-
else [ (f (n + 1) (elemAt list n)) ] ++ imap' (n + 1);
45
+
# Map with index: `imap (i: v: "${v}-${toString i}") ["a" "b"] ==
46
+
# ["a-1" "b-2"]'. FIXME: why does this start to count at 1?
48
+
if builtins ? genList then
49
+
f: list: genList (n: f (n + 1) (elemAt list n)) (length list)
57
+
else [ (f (n + 1) (elemAt list n)) ] ++ imap' (n + 1);
# Map and concatenate the result.
···
# Return a list of integers from `first' up to and including `last'.
123
-
range = first: last:
126
-
else [first] ++ range (first + 1) last;
128
+
if builtins ? genList then
132
+
else genList (n: first + n) (last - first + 1)
137
+
else [first] ++ range (first + 1) last;
# Partition the elements of a list in two lists, `right' and
···
) { right = []; wrong = []; };
139
-
zipListsWith = f: fst: snd:
143
-
len = if len1 < len2 then len1 else len2;
146
-
[ (f (elemAt fst n) (elemAt snd n)) ]
147
-
++ zipListsWith' (n + 1)
149
-
in zipListsWith' 0;
151
+
if builtins ? genList then
152
+
f: fst: snd: genList (n: f (elemAt fst n) (elemAt snd n)) (min (length fst) (length snd))
156
+
len = min (length fst) (length snd);
159
+
[ (f (elemAt fst n) (elemAt snd n)) ]
160
+
++ zipListsWith' (n + 1)
162
+
in zipListsWith' 0;
zipLists = zipListsWith (fst: snd: { inherit fst snd; });
154
-
# Reverse the order of the elements of a list. FIXME: O(n^2)!
155
-
reverseList = fold (e: acc: acc ++ [ e ]) [];
167
+
# Reverse the order of the elements of a list.
169
+
if builtins ? genList then
170
+
xs: let l = length xs; in genList (n: elemAt xs (l - n - 1)) l
172
+
fold (e: acc: acc ++ [ e ]) [];
# Sort a list based on a comparator function which compares two
···
# Return the first (at most) N elements of a list.
180
-
take = count: list:
184
-
if n == len || n == count
187
-
[ (elemAt list n) ] ++ take' (n + 1);
198
+
if builtins ? genList then
199
+
count: sublist 0 count
205
+
if n == len || n == count
208
+
[ (elemAt list n) ] ++ take' (n + 1);
# Remove the first (at most) N elements of a list.
192
-
drop = count: list:
196
-
if n == -1 || n < count
199
-
drop' (n - 1) ++ [ (elemAt list n) ];
200
-
in drop' (len - 1);
214
+
if builtins ? genList then
215
+
count: list: sublist count (length list) list
221
+
if n == -1 || n < count
224
+
drop' (n - 1) ++ [ (elemAt list n) ];
225
+
in drop' (len - 1);
228
+
# Return a list consisting of at most ‘count’ elements of ‘list’,
229
+
# starting at index ‘start’.
230
+
sublist = start: count: list:
231
+
let len = length list; in
233
+
(n: elemAt list (n + start))
234
+
(if start >= len then 0
235
+
else if start + count > len then len - start
# Return the last element of a list.
···
deepSeqList = xs: y: if any (x: deepSeq x false) xs then y else y;
crossLists = f: foldl (fs: args: concatMap (f: map f args) fs) [f];
216
-
# Remove duplicate elements from the list
254
+
# Remove duplicate elements from the list. O(n^2) complexity.
···
xs = unique (drop 1 list);
226
-
# Intersects list 'e' and another list
265
+
# Intersects list 'e' and another list. O(nm) complexity.
intersectLists = e: filter (x: elem x e);
229
-
# Subtracts list 'e' from another list
269
+
# Subtracts list 'e' from another list. O(nm) complexity.
subtractLists = e: filter (x: !(elem x e));