Clone of https://github.com/NixOS/nixpkgs.git (to stress-test knotserver)

lib/lists: Update documentation comments for doc generation

Updates documentation comments with extra information for nixdoc[1]
compatibility.

[1]: https://github.com/tazjin/nixdoc

+136 -48
+136 -48
lib/lists.nix
··· 1 1 # General list operations. 2 + 2 3 { lib }: 3 4 with lib.trivial; 4 5 let ··· 8 9 9 10 inherit (builtins) head tail length isList elemAt concatLists filter elem genList; 10 11 11 - /* Create a list consisting of a single element. `singleton x' is 12 - sometimes more convenient with respect to indentation than `[x]' 12 + /* Create a list consisting of a single element. `singleton x` is 13 + sometimes more convenient with respect to indentation than `[x]` 13 14 when x spans multiple lines. 14 15 16 + Type: singleton :: a -> [a] 17 + 15 18 Example: 16 19 singleton "foo" 17 20 => [ "foo" ] 18 21 */ 19 22 singleton = x: [x]; 20 23 21 - /* “right fold” a binary function `op' between successive elements of 22 - `list' with `nul' as the starting value, i.e., 23 - `foldr op nul [x_1 x_2 ... x_n] == op x_1 (op x_2 ... (op x_n nul))'. 24 - Type: 25 - foldr :: (a -> b -> b) -> b -> [a] -> b 24 + /* “right fold” a binary function `op` between successive elements of 25 + `list` with `nul' as the starting value, i.e., 26 + `foldr op nul [x_1 x_2 ... x_n] == op x_1 (op x_2 ... (op x_n nul))`. 27 + 28 + Type: foldr :: (a -> b -> b) -> b -> [a] -> b 26 29 27 30 Example: 28 31 concat = foldr (a: b: a + b) "z" ··· 42 45 else op (elemAt list n) (fold' (n + 1)); 43 46 in fold' 0; 44 47 45 - /* `fold' is an alias of `foldr' for historic reasons */ 48 + /* `fold` is an alias of `foldr` for historic reasons */ 46 49 # FIXME(Profpatsch): deprecate? 47 50 fold = foldr; 48 51 49 52 50 - /* “left fold”, like `foldr', but from the left: 53 + /* “left fold”, like `foldr`, but from the left: 51 54 `foldl op nul [x_1 x_2 ... x_n] == op (... (op (op nul x_1) x_2) ... x_n)`. 52 55 53 - Type: 54 - foldl :: (b -> a -> b) -> b -> [a] -> b 56 + Type: foldl :: (b -> a -> b) -> b -> [a] -> b 55 57 56 58 Example: 57 59 lconcat = foldl (a: b: a + b) "z" ··· 70 72 else op (foldl' (n - 1)) (elemAt list n); 71 73 in foldl' (length list - 1); 72 74 73 - /* Strict version of `foldl'. 75 + /* Strict version of `foldl`. 74 76 75 77 The difference is that evaluation is forced upon access. Usually used 76 78 with small whole results (in contract with lazily-generated list or large 77 79 lists where only a part is consumed.) 80 + 81 + Type: foldl' :: (b -> a -> b) -> b -> [a] -> b 78 82 */ 79 83 foldl' = builtins.foldl' or foldl; 80 84 81 85 /* Map with index starting from 0 82 86 87 + Type: imap0 :: (int -> a -> b) -> [a] -> [b] 88 + 83 89 Example: 84 90 imap0 (i: v: "${v}-${toString i}") ["a" "b"] 85 91 => [ "a-0" "b-1" ] ··· 87 93 imap0 = f: list: genList (n: f n (elemAt list n)) (length list); 88 94 89 95 /* Map with index starting from 1 96 + 97 + Type: imap1 :: (int -> a -> b) -> [a] -> [b] 90 98 91 99 Example: 92 100 imap1 (i: v: "${v}-${toString i}") ["a" "b"] ··· 95 103 imap1 = f: list: genList (n: f (n + 1) (elemAt list n)) (length list); 96 104 97 105 /* Map and concatenate the result. 106 + 107 + Type: concatMap :: (a -> [b]) -> [a] -> [b] 98 108 99 109 Example: 100 110 concatMap (x: [x] ++ ["z"]) ["a" "b"] ··· 118 128 119 129 /* Remove elements equal to 'e' from a list. Useful for buildInputs. 120 130 131 + Type: remove :: a -> [a] -> [a] 132 + 121 133 Example: 122 134 remove 3 [ 1 3 4 3 ] 123 135 => [ 1 4 ] 124 136 */ 125 - remove = e: filter (x: x != e); 137 + remove = 138 + # Element to remove from the list 139 + e: filter (x: x != e); 126 140 127 141 /* Find the sole element in the list matching the specified 128 - predicate, returns `default' if no such element exists, or 129 - `multiple' if there are multiple matching elements. 142 + predicate, returns `default` if no such element exists, or 143 + `multiple` if there are multiple matching elements. 144 + 145 + Type: findSingle :: (a -> bool) -> a -> a -> [a] -> a 130 146 131 147 Example: 132 148 findSingle (x: x == 3) "none" "multiple" [ 1 3 3 ] ··· 136 152 findSingle (x: x == 3) "none" "multiple" [ 1 9 ] 137 153 => "none" 138 154 */ 139 - findSingle = pred: default: multiple: list: 155 + findSingle = 156 + # Predicate 157 + pred: 158 + # Default value to return if element was not found. 159 + default: 160 + # Default value to return if more than one element was found 161 + multiple: 162 + # Input list 163 + list: 140 164 let found = filter pred list; len = length found; 141 165 in if len == 0 then default 142 166 else if len != 1 then multiple 143 167 else head found; 144 168 145 169 /* Find the first element in the list matching the specified 146 - predicate or returns `default' if no such element exists. 170 + predicate or return `default` if no such element exists. 171 + 172 + Type: findFirst :: (a -> bool) -> a -> [a] -> a 147 173 148 174 Example: 149 175 findFirst (x: x > 3) 7 [ 1 6 4 ] ··· 151 177 findFirst (x: x > 9) 7 [ 1 6 4 ] 152 178 => 7 153 179 */ 154 - findFirst = pred: default: list: 180 + findFirst = 181 + # Predicate 182 + pred: 183 + # Default value to return 184 + default: 185 + # Input list 186 + list: 155 187 let found = filter pred list; 156 188 in if found == [] then default else head found; 157 189 158 - /* Return true iff function `pred' returns true for at least element 159 - of `list'. 190 + /* Return true if function `pred` returns true for at least one 191 + element of `list`. 192 + 193 + Type: any :: (a -> bool) -> [a] -> bool 160 194 161 195 Example: 162 196 any isString [ 1 "a" { } ] ··· 166 200 */ 167 201 any = builtins.any or (pred: foldr (x: y: if pred x then true else y) false); 168 202 169 - /* Return true iff function `pred' returns true for all elements of 170 - `list'. 203 + /* Return true if function `pred` returns true for all elements of 204 + `list`. 205 + 206 + Type: all :: (a -> bool) -> [a] -> bool 171 207 172 208 Example: 173 209 all (x: x < 3) [ 1 2 ] ··· 177 213 */ 178 214 all = builtins.all or (pred: foldr (x: y: if pred x then y else false) true); 179 215 180 - /* Count how many times function `pred' returns true for the elements 181 - of `list'. 216 + /* Count how many elements of `list` match the supplied predicate 217 + function. 218 + 219 + Type: count :: (a -> bool) -> [a] -> int 182 220 183 221 Example: 184 222 count (x: x == 3) [ 3 2 3 4 6 ] 185 223 => 2 186 224 */ 187 - count = pred: foldl' (c: x: if pred x then c + 1 else c) 0; 225 + count = 226 + # Predicate 227 + pred: foldl' (c: x: if pred x then c + 1 else c) 0; 188 228 189 229 /* Return a singleton list or an empty list, depending on a boolean 190 230 value. Useful when building lists with optional elements 191 231 (e.g. `++ optional (system == "i686-linux") flashplayer'). 232 + 233 + Type: optional :: bool -> a -> [a] 192 234 193 235 Example: 194 236 optional true "foo" ··· 200 242 201 243 /* Return a list or an empty list, depending on a boolean value. 202 244 245 + Type: optionals :: bool -> [a] -> [a] 246 + 203 247 Example: 204 248 optionals true [ 2 3 ] 205 249 => [ 2 3 ] 206 250 optionals false [ 2 3 ] 207 251 => [ ] 208 252 */ 209 - optionals = cond: elems: if cond then elems else []; 253 + optionals = 254 + # Condition 255 + cond: 256 + # List to return if condition is true 257 + elems: if cond then elems else []; 210 258 211 259 212 260 /* If argument is a list, return it; else, wrap it in a singleton ··· 223 271 224 272 /* Return a list of integers from `first' up to and including `last'. 225 273 274 + Type: range :: int -> int -> [int] 275 + 226 276 Example: 227 277 range 2 4 228 278 => [ 2 3 4 ] 229 279 range 3 2 230 280 => [ ] 231 281 */ 232 - range = first: last: 282 + range = 283 + # First integer in the range 284 + first: 285 + # Last integer in the range 286 + last: 233 287 if first > last then 234 288 [] 235 289 else 236 290 genList (n: first + n) (last - first + 1); 237 291 238 - /* Splits the elements of a list in two lists, `right' and 239 - `wrong', depending on the evaluation of a predicate. 292 + /* Splits the elements of a list in two lists, `right` and 293 + `wrong`, depending on the evaluation of a predicate. 294 + 295 + Type: (a -> bool) -> [a] -> { right :: [a], wrong :: [a] } 240 296 241 297 Example: 242 298 partition (x: x > 2) [ 5 1 2 3 4 ] ··· 252 308 /* Splits the elements of a list into many lists, using the return value of a predicate. 253 309 Predicate should return a string which becomes keys of attrset `groupBy' returns. 254 310 255 - `groupBy'' allows to customise the combining function and initial value 311 + `groupBy'` allows to customise the combining function and initial value 256 312 257 313 Example: 258 314 groupBy (x: boolToString (x > 2)) [ 5 1 2 3 4 ] ··· 268 324 xfce = [ { name = "xfce"; script = "xfce4-session &"; } ]; 269 325 } 270 326 271 - 272 - groupBy' allows to customise the combining function and initial value 273 - 274 - Example: 275 327 groupBy' builtins.add 0 (x: boolToString (x > 2)) [ 5 1 2 3 4 ] 276 328 => { true = 12; false = 3; } 277 329 */ ··· 289 341 the merging stops at the shortest. How both lists are merged is defined 290 342 by the first argument. 291 343 344 + Type: zipListsWith :: (a -> b -> c) -> [a] -> [b] -> [c] 345 + 292 346 Example: 293 347 zipListsWith (a: b: a + b) ["h" "l"] ["e" "o"] 294 348 => ["he" "lo"] 295 349 */ 296 - zipListsWith = f: fst: snd: 350 + zipListsWith = 351 + # Function to zip elements of both lists 352 + f: 353 + # First list 354 + fst: 355 + # Second list 356 + snd: 297 357 genList 298 358 (n: f (elemAt fst n) (elemAt snd n)) (min (length fst) (length snd)); 299 359 300 360 /* Merges two lists of the same size together. If the sizes aren't the same 301 361 the merging stops at the shortest. 302 362 363 + Type: zipLists :: [a] -> [b] -> [{ fst :: a, snd :: b}] 364 + 303 365 Example: 304 366 zipLists [ 1 2 ] [ "a" "b" ] 305 367 => [ { fst = 1; snd = "a"; } { fst = 2; snd = "b"; } ] ··· 308 370 309 371 /* Reverse the order of the elements of a list. 310 372 373 + Type: reverseList :: [a] -> [a] 374 + 311 375 Example: 312 376 313 377 reverseList [ "b" "o" "j" ] ··· 321 385 `before a b == true` means that `b` depends on `a` (there's an 322 386 edge from `b` to `a`). 323 387 324 - Examples: 325 - 388 + Example: 326 389 listDfs true hasPrefix [ "/home/user" "other" "/" "/home" ] 327 390 == { minimal = "/"; # minimal element 328 391 visited = [ "/home/user" ]; # seen elements (in reverse order) ··· 336 399 rest = [ "/home" "other" ]; # everything else 337 400 338 401 */ 339 - 340 402 listDfs = stopOnCycles: before: list: 341 403 let 342 404 dfs' = us: visited: rest: ··· 361 423 `before a b == true` means that `b` should be after `a` 362 424 in the result. 363 425 364 - Examples: 426 + Example: 365 427 366 428 toposort hasPrefix [ "/home/user" "other" "/" "/home" ] 367 429 == { result = [ "/" "/home" "/home/user" "other" ]; } ··· 376 438 toposort (a: b: a < b) [ 3 2 1 ] == { result = [ 1 2 3 ]; } 377 439 378 440 */ 379 - 380 441 toposort = before: list: 381 442 let 382 443 dfsthis = listDfs true before list; ··· 467 528 468 529 /* Return the first (at most) N elements of a list. 469 530 531 + Type: take :: int -> [a] -> [a] 532 + 470 533 Example: 471 534 take 2 [ "a" "b" "c" "d" ] 472 535 => [ "a" "b" ] 473 536 take 2 [ ] 474 537 => [ ] 475 538 */ 476 - take = count: sublist 0 count; 539 + take = 540 + # Number of elements to take 541 + count: sublist 0 count; 477 542 478 543 /* Remove the first (at most) N elements of a list. 544 + 545 + Type: drop :: int -> [a] -> [a] 479 546 480 547 Example: 481 548 drop 2 [ "a" "b" "c" "d" ] ··· 483 550 drop 2 [ ] 484 551 => [ ] 485 552 */ 486 - drop = count: list: sublist count (length list) list; 553 + drop = 554 + # Number of elements to drop 555 + count: 556 + # Input list 557 + list: sublist count (length list) list; 487 558 488 - /* Return a list consisting of at most ‘count’ elements of ‘list’, 489 - starting at index ‘start’. 559 + /* Return a list consisting of at most `count` elements of `list`, 560 + starting at index `start`. 561 + 562 + Type: sublist :: int -> int -> [a] -> [a] 490 563 491 564 Example: 492 565 sublist 1 3 [ "a" "b" "c" "d" "e" ] ··· 494 567 sublist 1 3 [ ] 495 568 => [ ] 496 569 */ 497 - sublist = start: count: list: 570 + sublist = 571 + # Index at which to start the sublist 572 + start: 573 + # Number of elements to take 574 + count: 575 + # Input list 576 + list: 498 577 let len = length list; in 499 578 genList 500 579 (n: elemAt list (n + start)) ··· 503 582 else count); 504 583 505 584 /* Return the last element of a list. 585 + 586 + This function throws an error if the list is empty. 587 + 588 + Type: last :: [a] -> a 506 589 507 590 Example: 508 591 last [ 1 2 3 ] ··· 512 595 assert lib.assertMsg (list != []) "lists.last: list must not be empty!"; 513 596 elemAt list (length list - 1); 514 597 515 - /* Return all elements but the last 598 + /* Return all elements but the last. 599 + 600 + This function throws an error if the list is empty. 601 + 602 + Type: init :: [a] -> [a] 516 603 517 604 Example: 518 605 init [ 1 2 3 ] ··· 523 610 take (length list - 1) list; 524 611 525 612 526 - /* return the image of the cross product of some lists by a function 613 + /* Return the image of the cross product of some lists by a function. 527 614 528 615 Example: 529 616 crossLists (x:y: "${toString x}${toString y}") [[1 2] [3 4]] ··· 534 621 535 622 /* Remove duplicate elements from the list. O(n^2) complexity. 536 623 624 + Type: unique :: [a] -> [a] 625 + 537 626 Example: 538 - 539 627 unique [ 3 2 3 4 ] 540 628 => [ 3 2 4 ] 541 629 */