Skip to content

Commit

Permalink
lib/lists: introduce fastUnique
Browse files Browse the repository at this point in the history
A more efficient alternative to unique if sorted output is acceptable.
  • Loading branch information
bb010g committed Apr 14, 2021
1 parent ab91f6d commit 5d25df5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ let
ifilter0 concatMap flatten remove findSingle findFirst any all count
optional optionals toList range partition zipListsWith zipLists
reverseList listDfs toposort sort naturalSort compareLists take
drop sublist last init crossLists uniq uniqBy unique intersectLists
subtractLists mutuallyExclusive groupBy groupBy';
drop sublist last init crossLists uniq uniqBy unique fastUnique
intersectLists subtractLists mutuallyExclusive groupBy groupBy';
inherit (self.strings) concatStrings concatMapStrings concatImapStrings
intersperse concatStringsSep concatMapStringsSep
concatImapStringsSep makeSearchPath makeSearchPathOutput
Expand Down
11 changes: 11 additions & 0 deletions lib/lists.nix
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,17 @@ rec {
*/
unique = foldl' (acc: e: if elem e acc then acc else acc ++ [ e ]) [];

/* Sort and remove duplicate elements from the list.
O(n) complexity on top of `sort`.
Type: fastUnique :: (a -> a -> bool) -> [a] -> [a]
Example:
fastUnique (a: b: a < b) [ 3 2 3 4 ]
=> [ 2 3 4 ]
*/
fastUnique = comparator: list: uniq (sort comparator list);

/* Intersects list 'e' and another list. O(nm) complexity.
Example:
Expand Down

0 comments on commit 5d25df5

Please sign in to comment.