summary refs log tree commit diff
path: root/lib/lists.nix
diff options
context:
space:
mode:
authorGraham Christensen <graham@grahamc.com>2018-02-09 20:36:27 -0500
committerGitHub <noreply@github.com>2018-02-09 20:36:27 -0500
commit5aabf0fc34832b55e789da60cb58f7cbf75705e8 (patch)
treeb74568d505de9aa207aa08f95e44525de98ae502 /lib/lists.nix
parent94e62d505ffe19c621df3e16817fbdc9c92268ce (diff)
parent0d1a6432100e10b833fdb5557329f9e0818ecbfe (diff)
downloadnixpkgs-5aabf0fc34832b55e789da60cb58f7cbf75705e8.tar
nixpkgs-5aabf0fc34832b55e789da60cb58f7cbf75705e8.tar.gz
nixpkgs-5aabf0fc34832b55e789da60cb58f7cbf75705e8.tar.bz2
nixpkgs-5aabf0fc34832b55e789da60cb58f7cbf75705e8.tar.lz
nixpkgs-5aabf0fc34832b55e789da60cb58f7cbf75705e8.tar.xz
nixpkgs-5aabf0fc34832b55e789da60cb58f7cbf75705e8.tar.zst
nixpkgs-5aabf0fc34832b55e789da60cb58f7cbf75705e8.zip
Merge pull request #33898 from oxij/nixos/related-packages-v5
nixos: doc: implement related packages in the manual (again)
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 f2e6bacdc98..424d2c57f55 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: