From bccab965b9226e7d515e1ee1cdf47bb32d1df63d Mon Sep 17 00:00:00 2001 From: Jan Malakhovski Date: Thu, 7 Dec 2017 21:26:30 +0000 Subject: lib: implement `compare`, `splitByAndCompare`, and `compareLists` --- lib/lists.nix | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'lib/lists.nix') 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: -- cgit 1.4.1