summary refs log tree commit diff
path: root/lib
diff options
context:
space:
mode:
authorJan Malakhovski <oxij@oxij.org>2017-12-07 21:26:30 +0000
committerJan Malakhovski <oxij@oxij.org>2017-12-07 21:26:30 +0000
commitbccab965b9226e7d515e1ee1cdf47bb32d1df63d (patch)
treecd46278495ed5ec59a22bbc111b9d9b2065a320b /lib
parentaeb32cf187edd854cb5541f5f4fe40713c0e96b5 (diff)
downloadnixpkgs-bccab965b9226e7d515e1ee1cdf47bb32d1df63d.tar
nixpkgs-bccab965b9226e7d515e1ee1cdf47bb32d1df63d.tar.gz
nixpkgs-bccab965b9226e7d515e1ee1cdf47bb32d1df63d.tar.bz2
nixpkgs-bccab965b9226e7d515e1ee1cdf47bb32d1df63d.tar.lz
nixpkgs-bccab965b9226e7d515e1ee1cdf47bb32d1df63d.tar.xz
nixpkgs-bccab965b9226e7d515e1ee1cdf47bb32d1df63d.tar.zst
nixpkgs-bccab965b9226e7d515e1ee1cdf47bb32d1df63d.zip
lib: implement `compare`, `splitByAndCompare`, and `compareLists`
Diffstat (limited to 'lib')
-rw-r--r--lib/default.nix6
-rw-r--r--lib/lists.nix24
-rw-r--r--lib/trivial.nix25
3 files changed, 52 insertions, 3 deletions
diff --git a/lib/default.nix b/lib/default.nix
index 9dc4fea99fc..eb19dc06ce8 100644
--- a/lib/default.nix
+++ b/lib/default.nix
@@ -56,7 +56,7 @@ let
       replaceStrings seq stringLength sub substring tail;
     inherit (trivial) id const concat or and boolToString mergeAttrs
       flip mapNullable inNixShell min max importJSON warn info
-      nixpkgsVersion mod;
+      nixpkgsVersion mod compare splitByAndCompare;
 
     inherit (fixedPoints) fix fix' extends composeExtensions
       makeExtensible makeExtensibleWithCustomName;
@@ -71,8 +71,8 @@ let
     inherit (lists) singleton foldr fold foldl foldl' imap0 imap1
       concatMap flatten remove findSingle findFirst any all count
       optional optionals toList range partition zipListsWith zipLists
-      reverseList listDfs toposort sort take drop sublist last init
-      crossLists unique intersectLists subtractLists
+      reverseList listDfs toposort sort compareLists take drop sublist
+      last init crossLists unique intersectLists subtractLists
       mutuallyExclusive;
     inherit (strings) concatStrings concatMapStrings concatImapStrings
       intersperse concatStringsSep concatMapStringsSep
diff --git a/lib/lists.nix b/lib/lists.nix
index 8f67c6bb0ca..f7e09040a5a 100644
--- a/lib/lists.nix
+++ b/lib/lists.nix
@@ -385,6 +385,30 @@ rec {
       if len < 2 then list
       else (sort strictLess pivot.left) ++  [ first ] ++  (sort strictLess pivot.right));
 
+  /* Compare two lists element-by-element.
+
+     Example:
+       compareLists compare [] []
+       => 0
+       compareLists compare [] [ "a" ]
+       => -1
+       compareLists compare [ "a" ] []
+       => 1
+       compareLists compare [ "a" "b" ] [ "a" "c" ]
+       => 1
+  */
+  compareLists = cmp: a: b:
+    if a == []
+    then if b == []
+         then 0
+         else -1
+    else if b == []
+         then 1
+         else let rel = cmp (head a) (head b); in
+              if rel == 0
+              then compareLists cmp (tail a) (tail b)
+              else rel;
+
   /* Return the first (at most) N elements of a list.
 
      Example:
diff --git a/lib/trivial.nix b/lib/trivial.nix
index c452c7b65bc..5f18c0b61cc 100644
--- a/lib/trivial.nix
+++ b/lib/trivial.nix
@@ -81,6 +81,31 @@ rec {
   */
   mod = base: int: base - (int * (builtins.div base int));
 
+  /* C-style comparisons
+
+     a < b  => -1
+     a == b => 0
+     a > b  => 1
+  */
+  compare = a: b:
+    if a < b
+    then -1
+    else if a > b
+         then 1
+         else 0;
+
+  /* Split type into two subtypes by predicate `p`, assume
+
+       forall x y . x < y if p x == true && p y == false
+
+     compare elements of the same subtype with `yes` and `no`
+     comparisons respectively.
+  */
+  splitByAndCompare = p: yes: no: a: b:
+    if p a
+    then if p b then yes a b else -1
+    else if p b then 1 else no a b;
+
   /* Reads a JSON file. */
   importJSON = path:
     builtins.fromJSON (builtins.readFile path);