Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib.lists.allUnique: init #239722

Merged
merged 1 commit into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions lib/lists.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
9 changes: 9 additions & 0 deletions lib/tests/misc.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down