diff --git a/lib/default.nix b/lib/default.nix index 3bff968a60e4a..49bb873fc8ef1 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -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 diff --git a/lib/lists.nix b/lib/lists.nix index 98007d2a972db..54998d4048e05 100644 --- a/lib/lists.nix +++ b/lib/lists.nix @@ -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: