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

lib: implement `compare`, `splitByAndCompare`, and `compareLists`

+64 -3
+4 -3
lib/default.nix
··· 56 56 replaceStrings seq stringLength sub substring tail; 57 57 inherit (trivial) id const concat or and boolToString mergeAttrs 58 58 flip mapNullable inNixShell min max importJSON warn info 59 - nixpkgsVersion mod functionArgs setFunctionArgs isFunction; 59 + nixpkgsVersion mod compare splitByAndCompare 60 + functionArgs setFunctionArgs isFunction; 60 61 61 62 inherit (fixedPoints) fix fix' extends composeExtensions 62 63 makeExtensible makeExtensibleWithCustomName; ··· 71 72 inherit (lists) singleton foldr fold foldl foldl' imap0 imap1 72 73 concatMap flatten remove findSingle findFirst any all count 73 74 optional optionals toList range partition zipListsWith zipLists 74 - reverseList listDfs toposort sort take drop sublist last init 75 - crossLists unique intersectLists subtractLists 75 + reverseList listDfs toposort sort compareLists take drop sublist 76 + last init crossLists unique intersectLists subtractLists 76 77 mutuallyExclusive; 77 78 inherit (strings) concatStrings concatMapStrings concatImapStrings 78 79 intersperse concatStringsSep concatMapStringsSep
+24
lib/lists.nix
··· 385 385 if len < 2 then list 386 386 else (sort strictLess pivot.left) ++ [ first ] ++ (sort strictLess pivot.right)); 387 387 388 + /* Compare two lists element-by-element. 389 + 390 + Example: 391 + compareLists compare [] [] 392 + => 0 393 + compareLists compare [] [ "a" ] 394 + => -1 395 + compareLists compare [ "a" ] [] 396 + => 1 397 + compareLists compare [ "a" "b" ] [ "a" "c" ] 398 + => 1 399 + */ 400 + compareLists = cmp: a: b: 401 + if a == [] 402 + then if b == [] 403 + then 0 404 + else -1 405 + else if b == [] 406 + then 1 407 + else let rel = cmp (head a) (head b); in 408 + if rel == 0 409 + then compareLists cmp (tail a) (tail b) 410 + else rel; 411 + 388 412 /* Return the first (at most) N elements of a list. 389 413 390 414 Example:
+36
lib/trivial.nix
··· 81 81 */ 82 82 mod = base: int: base - (int * (builtins.div base int)); 83 83 84 + /* C-style comparisons 85 + 86 + a < b, compare a b => -1 87 + a == b, compare a b => 0 88 + a > b, compare a b => 1 89 + */ 90 + compare = a: b: 91 + if a < b 92 + then -1 93 + else if a > b 94 + then 1 95 + else 0; 96 + 97 + /* Split type into two subtypes by predicate `p`, take all elements 98 + of the first subtype to be less than all the elements of the 99 + second subtype, compare elements of a single subtype with `yes` 100 + and `no` respectively. 101 + 102 + Example: 103 + 104 + let cmp = splitByAndCompare (hasPrefix "foo") compare compare; in 105 + 106 + cmp "a" "z" => -1 107 + cmp "fooa" "fooz" => -1 108 + 109 + cmp "f" "a" => 1 110 + cmp "fooa" "a" => -1 111 + # while 112 + compare "fooa" "a" => 1 113 + 114 + */ 115 + splitByAndCompare = p: yes: no: a: b: 116 + if p a 117 + then if p b then yes a b else -1 118 + else if p b then 1 else no a b; 119 + 84 120 /* Reads a JSON file. */ 85 121 importJSON = path: 86 122 builtins.fromJSON (builtins.readFile path);