···5656 replaceStrings seq stringLength sub substring tail;
5757 inherit (trivial) id const concat or and boolToString mergeAttrs
5858 flip mapNullable inNixShell min max importJSON warn info
5959- nixpkgsVersion mod functionArgs setFunctionArgs isFunction;
5959+ nixpkgsVersion mod compare splitByAndCompare
6060+ functionArgs setFunctionArgs isFunction;
60616162 inherit (fixedPoints) fix fix' extends composeExtensions
6263 makeExtensible makeExtensibleWithCustomName;
···7172 inherit (lists) singleton foldr fold foldl foldl' imap0 imap1
7273 concatMap flatten remove findSingle findFirst any all count
7374 optional optionals toList range partition zipListsWith zipLists
7474- reverseList listDfs toposort sort take drop sublist last init
7575- crossLists unique intersectLists subtractLists
7575+ reverseList listDfs toposort sort compareLists take drop sublist
7676+ last init crossLists unique intersectLists subtractLists
7677 mutuallyExclusive;
7778 inherit (strings) concatStrings concatMapStrings concatImapStrings
7879 intersperse concatStringsSep concatMapStringsSep
+24
lib/lists.nix
···385385 if len < 2 then list
386386 else (sort strictLess pivot.left) ++ [ first ] ++ (sort strictLess pivot.right));
387387388388+ /* Compare two lists element-by-element.
389389+390390+ Example:
391391+ compareLists compare [] []
392392+ => 0
393393+ compareLists compare [] [ "a" ]
394394+ => -1
395395+ compareLists compare [ "a" ] []
396396+ => 1
397397+ compareLists compare [ "a" "b" ] [ "a" "c" ]
398398+ => 1
399399+ */
400400+ compareLists = cmp: a: b:
401401+ if a == []
402402+ then if b == []
403403+ then 0
404404+ else -1
405405+ else if b == []
406406+ then 1
407407+ else let rel = cmp (head a) (head b); in
408408+ if rel == 0
409409+ then compareLists cmp (tail a) (tail b)
410410+ else rel;
411411+388412 /* Return the first (at most) N elements of a list.
389413390414 Example:
+36
lib/trivial.nix
···8181 */
8282 mod = base: int: base - (int * (builtins.div base int));
83838484+ /* C-style comparisons
8585+8686+ a < b, compare a b => -1
8787+ a == b, compare a b => 0
8888+ a > b, compare a b => 1
8989+ */
9090+ compare = a: b:
9191+ if a < b
9292+ then -1
9393+ else if a > b
9494+ then 1
9595+ else 0;
9696+9797+ /* Split type into two subtypes by predicate `p`, take all elements
9898+ of the first subtype to be less than all the elements of the
9999+ second subtype, compare elements of a single subtype with `yes`
100100+ and `no` respectively.
101101+102102+ Example:
103103+104104+ let cmp = splitByAndCompare (hasPrefix "foo") compare compare; in
105105+106106+ cmp "a" "z" => -1
107107+ cmp "fooa" "fooz" => -1
108108+109109+ cmp "f" "a" => 1
110110+ cmp "fooa" "a" => -1
111111+ # while
112112+ compare "fooa" "a" => 1
113113+114114+ */
115115+ splitByAndCompare = p: yes: no: a: b:
116116+ if p a
117117+ then if p b then yes a b else -1
118118+ else if p b then 1 else no a b;
119119+84120 /* Reads a JSON file. */
85121 importJSON = path:
86122 builtins.fromJSON (builtins.readFile path);