@@ -6,17 +6,26 @@ rec {
66
77 inherit (builtins) head tail length isList elemAt concatLists filter elem genList;
88
9−
10− # Create a list consisting of a single element. `singleton x' is
11− # sometimes more convenient with respect to indentation than `[x]'
12− # when x spans multiple lines.
9+ /* Create a list consisting of a single element. `singleton x' is
10+ sometimes more convenient with respect to indentation than `[x]'
11+ when x spans multiple lines.
12+
13+ Example:
14+ singleton "foo"
15+ => [ "foo" ]
16+ */
1317 singleton = x: [x];
1418
19+ /* "Fold" a binary function `op' between successive elements of
20+ `list' with `nul' as the starting value, i.e., `fold op nul [x_1
21+ x_2 ... x_n] == op x_1 (op x_2 ... (op x_n nul))'. (This is
22+ Haskell's foldr).
1523
16− # "Fold" a binary function `op' between successive elements of
17− # `list' with `nul' as the starting value, i.e., `fold op nul [x_1
18− # x_2 ... x_n] == op x_1 (op x_2 ... (op x_n nul))'. (This is
19− # Haskell's foldr).
24+ Example:
25+ concat = fold (a: b: a + b) "z"
26+ concat [ "a" "b" "c" ]
27+ => "abcnul"
28+ */
2029 fold = op: nul: list:
2130 let
2231 len = length list;
@@ -26,8 +35,14 @@ rec {
2635 else op (elemAt list n) (fold' (n + 1));
2736 in fold' 0;
2837
29− # Left fold: `fold op nul [x_1 x_2 ... x_n] == op (... (op (op nul
30− # x_1) x_2) ... x_n)'.
38+ /* Left fold: `fold op nul [x_1 x_2 ... x_n] == op (... (op (op nul
39+ x_1) x_2) ... x_n)'.
40+
41+ Example:
42+ lconcat = foldl (a: b: a + b) "z"
43+ lconcat [ "a" "b" "c" ]
44+ => "zabc"
45+ */
3146 foldl = op: nul: list:
3247 let
3348 len = length list;
@@ -37,13 +52,22 @@ rec {
3752 else op (foldl' (n - 1)) (elemAt list n);
3853 in foldl' (length list - 1);
3954
55+ /* Strict version of foldl.
4056
41− # Strict version of foldl.
57+ The difference is that evaluation is forced upon access. Usually used
58+ with small whole results (in contract with lazily-generated list or large
59+ lists where only a part is consumed.)
60+ */
4261 foldl' = builtins.foldl' or foldl;
4362
63+ /* Map with index
64+
65+ FIXME(zimbatm): why does this start to count at 1?
4466
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?
67+ Example:
68+ imap (i: v: "${v}-${toString i}") ["a" "b"]
69+ => [ "a-1" "b-2" ]
70+ */
4771 imap =
4872 if builtins ? genList then
4973 f: list: genList (n: f (n + 1) (elemAt list n)) (length list)
@@ -57,73 +81,141 @@ rec {
5781 else [ (f (n + 1) (elemAt list n)) ] ++ imap' (n + 1);
5882 in imap' 0;
5983
84+ /* Map and concatenate the result.
6085
61− # Map and concatenate the result.
86+ Example:
87+ concatMap (x: [x] ++ ["z"]) ["a" "b"]
88+ => [ "a" "z" "b" "z" ]
89+ */
6290 concatMap = f: list: concatLists (map f list);
6391
92+ /* Flatten the argument into a single list; that is, nested lists are
93+ spliced into the top-level lists.
6494
65− # Flatten the argument into a single list; that is, nested lists are
66− # spliced into the top-level lists. E.g., `flatten [1 [2 [3] 4] 5]
67− # == [1 2 3 4 5]' and `flatten 1 == [1]'.
95+ Example:
96+ flatten [1 [2 [3] 4] 5]
97+ => [1 2 3 4 5]
98+ flatten 1
99+ => [1]
100+ */
68101 flatten = x:
69102 if isList x
70103 then foldl' (x: y: x ++ (flatten y)) [] x
71104 else [x];
72105
106+ /* Remove elements equal to 'e' from a list. Useful for buildInputs.
73107
74− # Remove elements equal to 'e' from a list. Useful for buildInputs.
108+ Example:
109+ remove 3 [ 1 3 4 3 ]
110+ => [ 1 4 ]
111+ */
75112 remove = e: filter (x: x != e);
76113
77−
78− # Find the sole element in the list matching the specified
79− # predicate, returns `default' if no such element exists, or
80− # `multiple' if there are multiple matching elements.
114+ /* Find the sole element in the list matching the specified
115+ predicate, returns `default' if no such element exists, or
116+ `multiple' if there are multiple matching elements.
117+
118+ Example:
119+ findSingle (x: x == 3) "none" "multiple" [ 1 3 3 ]
120+ => "multiple"
121+ findSingle (x: x == 3) "none" "multiple" [ 1 3 ]
122+ => 3
123+ findSingle (x: x == 3) "none" "multiple" [ 1 9 ]
124+ => "none"
125+ */
81126 findSingle = pred: default: multiple: list:
82127 let found = filter pred list; len = length found;
83128 in if len == 0 then default
84129 else if len != 1 then multiple
85130 else head found;
86131
132+ /* Find the first element in the list matching the specified
133+ predicate or returns `default' if no such element exists.
87134
88− # Find the first element in the list matching the specified
89− # predicate or returns `default' if no such element exists.
135+ Example:
136+ findFirst (x: x > 3) 7 [ 1 6 4 ]
137+ => 6
138+ findFirst (x: x > 9) 7 [ 1 6 4 ]
139+ => 7
140+ */
90141 findFirst = pred: default: list:
91142 let found = filter pred list;
92143 in if found == [] then default else head found;
93144
145+ /* Return true iff function `pred' returns true for at least element
146+ of `list'.
94147
95− # Return true iff function `pred' returns true for at least element
96− # of `list'.
148+ Example:
149+ any isString [ 1 "a" { } ]
150+ => true
151+ any isString [ 1 { } ]
152+ => false
153+ */
97154 any = builtins.any or (pred: fold (x: y: if pred x then true else y) false);
98155
156+ /* Return true iff function `pred' returns true for all elements of
157+ `list'.
99158
100− # Return true iff function `pred' returns true for all elements of
101− # `list'.
159+ Example:
160+ all (x: x < 3) [ 1 2 ]
161+ => true
162+ all (x: x < 3) [ 1 2 3 ]
163+ => false
164+ */
102165 all = builtins.all or (pred: fold (x: y: if pred x then y else false) true);
103166
167+ /* Count how many times function `pred' returns true for the elements
168+ of `list'.
104169
105− # Count how many times function `pred' returns true for the elements
106− # of `list'.
170+ Example:
171+ count (x: x == 3) [ 3 2 3 4 6 ]
172+ => 2
173+ */
107174 count = pred: foldl' (c: x: if pred x then c + 1 else c) 0;
108175
176+ /* Return a singleton list or an empty list, depending on a boolean
177+ value. Useful when building lists with optional elements
178+ (e.g. `++ optional (system == "i686-linux") flashplayer').
109179
110− # Return a singleton list or an empty list, depending on a boolean
111− # value. Useful when building lists with optional elements
112− # (e.g. `++ optional (system == "i686-linux") flashplayer').
180+ Example:
181+ optional true "foo"
182+ => [ "foo" ]
183+ optional false "foo"
184+ => [ ]
185+ */
113186 optional = cond: elem: if cond then [elem] else [];
114187
188+ /* Return a list or an empty list, dependening on a boolean value.
115189
116− # Return a list or an empty list, dependening on a boolean value.
190+ Example:
191+ optionals true [ 2 3 ]
192+ => [ 2 3 ]
193+ optionals false [ 2 3 ]
194+ => [ ]
195+ */
117196 optionals = cond: elems: if cond then elems else [];
118197
119198
120− # If argument is a list, return it; else, wrap it in a singleton
121− # list. If you're using this, you should almost certainly
122− # reconsider if there isn't a more "well-typed" approach.
199+ /* If argument is a list, return it; else, wrap it in a singleton
200+ list. If you're using this, you should almost certainly
201+ reconsider if there isn't a more "well-typed" approach.
202+
203+ Example:
204+ toList [ 1 2 ]
205+ => [ 1 2 ]
206+ toList "hi"
207+ => [ "hi "]
208+ */
123209 toList = x: if isList x then x else [x];
124210
211+ /* Return a list of integers from `first' up to and including `last'.
125212
126− # Return a list of integers from `first' up to and including `last'.
213+ Example:
214+ range 2 4
215+ => [ 2 3 4 ]
216+ range 3 2
217+ => [ ]
218+ */
127219 range =
128220 if builtins ? genList then
129221 first: last:
@@ -136,9 +228,13 @@ rec {
136228 then []
137229 else [first] ++ range (first + 1) last;
138230
231+ /* Splits the elements of a list in two lists, `right' and
232+ `wrong', depending on the evaluation of a predicate.
139233
140− # Partition the elements of a list in two lists, `right' and
141− # `wrong', depending on the evaluation of a predicate.
234+ Example:
235+ partition (x: x > 2) [ 5 1 2 3 4 ]
236+ => { right = [ 5 3 4 ]; wrong = [ 1 2 ]; }
237+ */
142238 partition = pred:
143239 fold (h: t:
144240 if pred h
@@ -146,7 +242,14 @@ rec {
146242 else { right = t.right; wrong = [h] ++ t.wrong; }
147243 ) { right = []; wrong = []; };
148244
245+ /* Merges two lists of the same size together. If the sizes aren't the same
246+ the merging stops at the shortest. How both lists are merged is defined
247+ by the first argument.
149248
249+ Example:
250+ zipListsWith (a: b: a + b) ["h" "l"] ["e" "o"]
251+ => ["he" "lo"]
252+ */
150253 zipListsWith =
151254 if builtins ? genList then
152255 f: fst: snd: genList (n: f (elemAt fst n) (elemAt snd n)) (min (length fst) (length snd))
@@ -161,21 +264,37 @@ rec {
161264 else [];
162265 in zipListsWith' 0;
163266
267+ /* Merges two lists of the same size together. If the sizes aren't the same
268+ the merging stops at the shortest.
269+
270+ Example:
271+ zipLists [ 1 2 ] [ "a" "b" ]
272+ => [ { fst = 1; snd = "a"; } { fst = 2; snd = "b"; } ]
273+ */
164274 zipLists = zipListsWith (fst: snd: { inherit fst snd; });
165275
276+ /* Reverse the order of the elements of a list.
166277
167− # Reverse the order of the elements of a list.
278+ Example:
279+
280+ reverseList [ "b" "o" "j" ]
281+ => [ "j" "o" "b" ]
282+ */
168283 reverseList =
169284 if builtins ? genList then
170285 xs: let l = length xs; in genList (n: elemAt xs (l - n - 1)) l
171286 else
172287 fold (e: acc: acc ++ [ e ]) [];
173288
289+ /* Sort a list based on a comparator function which compares two
290+ elements and returns true if the first argument is strictly below
291+ the second argument. The returned list is sorted in an increasing
292+ order. The implementation does a quick-sort.
174293
175− # Sort a list based on a comparator function which compares two
176− # elements and returns true if the first argument is strictly below
177− # the second argument. The returned list is sorted in an increasing
178− # order. The implementation does a quick-sort.
294+ Example:
295+ sort (a: b: a < b) [ 5 3 7 ]
296+ => [ 3 5 7 ]
297+ */
179298 sort = builtins.sort or (
180299 strictLess: list:
181300 let
@@ -193,8 +312,14 @@ rec {
193312 if len < 2 then list
194313 else (sort strictLess pivot.left) ++ [ first ] ++ (sort strictLess pivot.right));
195314
315+ /* Return the first (at most) N elements of a list.
196316
197− # Return the first (at most) N elements of a list.
317+ Example:
318+ take 2 [ "a" "b" "c" "d" ]
319+ => [ "a" "b" ]
320+ take 2 [ ]
321+ => [ ]
322+ */
198323 take =
199324 if builtins ? genList then
200325 count: sublist 0 count
@@ -209,8 +334,14 @@ rec {
209334 [ (elemAt list n) ] ++ take' (n + 1);
210335 in take' 0;
211336
337+ /* Remove the first (at most) N elements of a list.
212338
213− # Remove the first (at most) N elements of a list.
339+ Example:
340+ drop 2 [ "a" "b" "c" "d" ]
341+ => [ "c" "d" ]
342+ drop 2 [ ]
343+ => [ ]
344+ */
214345 drop =
215346 if builtins ? genList then
216347 count: list: sublist count (length list) list
@@ -225,9 +356,15 @@ rec {
225356 drop' (n - 1) ++ [ (elemAt list n) ];
226357 in drop' (len - 1);
227358
359+ /* Return a list consisting of at most ‘count’ elements of ‘list’,
360+ starting at index ‘start’.
228361
229− # Return a list consisting of at most ‘count’ elements of ‘list’,
230− # starting at index ‘start’.
362+ Example:
363+ sublist 1 3 [ "a" "b" "c" "d" "e" ]
364+ => [ "b" "c" "d" ]
365+ sublist 1 3 [ ]
366+ => [ ]
367+ */
231368 sublist = start: count: list:
232369 let len = length list; in
233370 genList
@@ -236,20 +373,36 @@ rec {
236373 else if start + count > len then len - start
237374 else count);
238375
376+ /* Return the last element of a list.
239377
240− # Return the last element of a list.
378+ Example:
379+ last [ 1 2 3 ]
380+ => 3
381+ */
241382 last = list:
242383 assert list != []; elemAt list (length list - 1);
243384
385+ /* Return all elements but the last
244386
245− # Return all elements but the last
387+ Example:
388+ init [ 1 2 3 ]
389+ => [ 1 2 ]
390+ */
246391 init = list: assert list != []; take (length list - 1) list;
247392
248393
394+ /* FIXME(zimbatm) Not used anywhere
395+ */
249396 crossLists = f: foldl (fs: args: concatMap (f: map f args) fs) [f];
250397
251398
252− # Remove duplicate elements from the list. O(n^2) complexity.
399+ /* Remove duplicate elements from the list. O(n^2) complexity.
400+
401+ Example:
402+
403+ unique [ 3 2 3 4 ]
404+ => [ 3 2 4 ]
405+ */
253406 unique = list:
254407 if list == [] then
255408 []
@@ -259,15 +412,24 @@ rec {
259412 xs = unique (drop 1 list);
260413 in [x] ++ remove x xs;
261414
415+ /* Intersects list 'e' and another list. O(nm) complexity.
262416
263− # Intersects list 'e' and another list. O(nm) complexity.
417+ Example:
418+ intersectLists [ 1 2 3 ] [ 6 3 2 ]
419+ => [ 3 2 ]
420+ */
264421 intersectLists = e: filter (x: elem x e);
265422
423+ /* Subtracts list 'e' from another list. O(nm) complexity.
266424
267− # Subtracts list 'e' from another list. O(nm) complexity.
425+ Example:
426+ subtractLists [ 3 2 ] [ 1 2 3 4 5 3 ]
427+ => [ 1 4 5 ]
428+ */
268429 subtractLists = e: filter (x: !(elem x e));
269430
270− deepSeqList = throw "removed 2016-02-29 because unused and broken";
431+ /*** deprecated stuff ***/
271432
433+ deepSeqList = throw "removed 2016-02-29 because unused and broken";
272434
273435}