diff options
author | adisbladis <adisbladis@gmail.com> | 2020-11-24 06:20:01 +0100 |
---|---|---|
committer | adisbladis <adisbladis@gmail.com> | 2020-11-24 14:31:54 +0100 |
commit | 85605c8a296bd54e9c12e1bd1c33bf1e100d40b7 (patch) | |
tree | 4501dd994f969bfd7b8d4ffdda157cecbc8c8ef6 | |
parent | 8d8532cd32d05c0813db78db4b6043115bf06417 (diff) |
lib.lists.unique: Switch from recursive function to using a fold
This improves performance by ~30-40% for smaller test cases and makes
larger cases where my laptop would OOM pass in seconds.
-rw-r--r-- | lib/lists.nix | 8 |
1 files changed, 1 insertions, 7 deletions
diff --git a/lib/lists.nix b/lib/lists.nix index 6c97e0686aa8..06cee2eb112a 100644 --- a/lib/lists.nix +++ b/lib/lists.nix @@ -640,13 +640,7 @@ rec { unique [ 3 2 3 4 ] => [ 3 2 4 ] */ - unique = list: - if list == [] then - [] - else - let - x = head list; - in [x] ++ unique (remove x list); + unique = foldl' (acc: e: if elem e acc then acc else acc ++ [ e ]) []; /* Intersects list 'e' and another list. O(nm) complexity. |