diff options
author | Silvan Mosberger <contact@infinisil.com> | 2023-11-15 04:37:39 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-11-15 04:37:39 +0100 |
commit | b04b7d64f7a344e6e20e0cd432838733751e6cab (patch) | |
tree | 3e7d7df2101bffdf18f045f0ed7937532dfd1ffe /lib | |
parent | 486911d06452f5c3c174bac3686ce79840f7eac4 (diff) | |
parent | 66261e9961254a5c8a7c096e091fd42deb988cee (diff) | |
download | nixpkgs-b04b7d64f7a344e6e20e0cd432838733751e6cab.tar nixpkgs-b04b7d64f7a344e6e20e0cd432838733751e6cab.tar.gz nixpkgs-b04b7d64f7a344e6e20e0cd432838733751e6cab.tar.bz2 nixpkgs-b04b7d64f7a344e6e20e0cd432838733751e6cab.tar.lz nixpkgs-b04b7d64f7a344e6e20e0cd432838733751e6cab.tar.xz nixpkgs-b04b7d64f7a344e6e20e0cd432838733751e6cab.tar.zst nixpkgs-b04b7d64f7a344e6e20e0cd432838733751e6cab.zip |
Merge pull request #239722 from Stunkymonkey/lib-allUnique
lib.lists.allUnique: init
Diffstat (limited to 'lib')
-rw-r--r-- | lib/default.nix | 2 | ||||
-rw-r--r-- | lib/lists.nix | 13 | ||||
-rw-r--r-- | lib/tests/misc.nix | 9 |
3 files changed, 23 insertions, 1 deletions
diff --git a/lib/default.nix b/lib/default.nix index 0dac50a08ca..a2958e561cf 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -92,7 +92,7 @@ let concatMap flatten remove findSingle findFirst any all count optional optionals toList range replicate partition zipListsWith zipLists reverseList listDfs toposort sort naturalSort compareLists take - drop sublist last init crossLists unique intersectLists + drop sublist last init crossLists unique allUnique intersectLists subtractLists mutuallyExclusive groupBy groupBy'; inherit (self.strings) concatStrings concatMapStrings concatImapStrings intersperse concatStringsSep concatMapStringsSep diff --git a/lib/lists.nix b/lib/lists.nix index 3835e3ba69c..15047f488f4 100644 --- a/lib/lists.nix +++ b/lib/lists.nix @@ -821,6 +821,19 @@ rec { */ unique = foldl' (acc: e: if elem e acc then acc else acc ++ [ e ]) []; + /* Check if list contains only unique elements. O(n^2) complexity. + + Type: allUnique :: [a] -> bool + + Example: + allUnique [ 3 2 3 4 ] + => false + allUnique [ 3 2 4 1 ] + => true + */ + allUnique = list: (length (unique list) == length list); + + /* Intersects list 'e' and another list. O(nm) complexity. Example: diff --git a/lib/tests/misc.nix b/lib/tests/misc.nix index 0d30e93aafb..06cb5e763e2 100644 --- a/lib/tests/misc.nix +++ b/lib/tests/misc.nix @@ -726,6 +726,15 @@ runTests { expected = 7; }; + testAllUnique_true = { + expr = allUnique [ 3 2 4 1 ]; + expected = true; + }; + testAllUnique_false = { + expr = allUnique [ 3 2 3 4 ]; + expected = false; + }; + # ATTRSETS testConcatMapAttrs = { |