aboutsummaryrefslogtreecommitdiff
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>2017-12-07 21:26:30 +0000
commitbccab965b9226e7d515e1ee1cdf47bb32d1df63d (patch)
treecd46278495ed5ec59a22bbc111b9d9b2065a320b /lib/lists.nix
parentaeb32cf187edd854cb5541f5f4fe40713c0e96b5 (diff)
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 8f67c6bb0ca3..f7e09040a5aa 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: