summary refs log tree commit diff
path: root/lib/lists.nix
diff options
context:
space:
mode:
authorJan Malakhovski <oxij@oxij.org>2017-12-07 21:26:30 +0000
committerJan Malakhovski <oxij@oxij.org>2018-02-09 19:51:05 +0000
commitee3220440da59e39154b520353a3d03a1abf3405 (patch)
treeaa3bf8018184d395869ffd2a32dd97632c2b100f /lib/lists.nix
parent2341c81427420be95cd9f8640e3e96e3e317c645 (diff)
downloadnixpkgs-ee3220440da59e39154b520353a3d03a1abf3405.tar
nixpkgs-ee3220440da59e39154b520353a3d03a1abf3405.tar.gz
nixpkgs-ee3220440da59e39154b520353a3d03a1abf3405.tar.bz2
nixpkgs-ee3220440da59e39154b520353a3d03a1abf3405.tar.lz
nixpkgs-ee3220440da59e39154b520353a3d03a1abf3405.tar.xz
nixpkgs-ee3220440da59e39154b520353a3d03a1abf3405.tar.zst
nixpkgs-ee3220440da59e39154b520353a3d03a1abf3405.zip
lib: implement `compare`, `splitByAndCompare`, and `compareLists`
Diffstat (limited to 'lib/lists.nix')
-rw-r--r--lib/lists.nix24
1 files changed, 24 insertions, 0 deletions
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: