summary refs log tree commit diff
diff options
context:
space:
mode:
authorSilvan Mosberger <contact@infinisil.com>2021-12-02 19:34:08 +0100
committerSilvan Mosberger <contact@infinisil.com>2022-03-18 00:05:10 +0100
commit1ad7812c4ac038b653dd57b887c7c07d02bf1bec (patch)
treef280b79d5cbb4bf8d430334867899b42ecd3f667
parent71b130c581c81bf3bd2a3c777f7cc11d746327ae (diff)
downloadnixpkgs-1ad7812c4ac038b653dd57b887c7c07d02bf1bec.tar
nixpkgs-1ad7812c4ac038b653dd57b887c7c07d02bf1bec.tar.gz
nixpkgs-1ad7812c4ac038b653dd57b887c7c07d02bf1bec.tar.bz2
nixpkgs-1ad7812c4ac038b653dd57b887c7c07d02bf1bec.tar.lz
nixpkgs-1ad7812c4ac038b653dd57b887c7c07d02bf1bec.tar.xz
nixpkgs-1ad7812c4ac038b653dd57b887c7c07d02bf1bec.tar.zst
nixpkgs-1ad7812c4ac038b653dd57b887c7c07d02bf1bec.zip
lib.lists: Use builtins.groupBy for lib.groupBy
builtins.groupBy is much more performant. It was introduced in
https://github.com/NixOS/nix/pull/5715
-rw-r--r--lib/lists.nix17
-rw-r--r--lib/tests/misc.nix18
2 files changed, 26 insertions, 9 deletions
diff --git a/lib/lists.nix b/lib/lists.nix
index 1dbff7668d7..a030280c8dc 100644
--- a/lib/lists.nix
+++ b/lib/lists.nix
@@ -4,6 +4,7 @@
 let
   inherit (lib.strings) toInt;
   inherit (lib.trivial) compare min;
+  inherit (lib.attrsets) mapAttrs;
 in
 rec {
 
@@ -340,15 +341,15 @@ rec {
        groupBy' builtins.add 0 (x: boolToString (x > 2)) [ 5 1 2 3 4 ]
        => { true = 12; false = 3; }
   */
-  groupBy' = op: nul: pred: lst:
-    foldl' (r: e:
-              let
-                key = pred e;
-              in
-                r // { ${key} = op (r.${key} or nul) e; }
-           ) {} lst;
+  groupBy' = op: nul: pred: lst: mapAttrs (name: foldl op nul) (groupBy pred lst);
 
-  groupBy = groupBy' (sum: e: sum ++ [e]) [];
+  groupBy = builtins.groupBy or (
+    pred: foldl' (r: e:
+       let
+         key = pred e;
+       in
+         r // { ${key} = (r.${key} or []) ++ [e]; }
+    ) {});
 
   /* Merges two lists of the same size together. If the sizes aren't the same
      the merging stops at the shortest. How both lists are merged is defined
diff --git a/lib/tests/misc.nix b/lib/tests/misc.nix
index c4a34369f50..c2e76f18d95 100644
--- a/lib/tests/misc.nix
+++ b/lib/tests/misc.nix
@@ -781,6 +781,22 @@ runTests {
       "a2-b"
       "_bc'de"
     ];
-    expected = "\".\".foo.\"2\".a2-b._bc'de";
+    expected = ''".".foo."2".a2-b._bc'de'';
+  };
+
+  testGroupBy = {
+    expr = groupBy (n: toString (mod n 5)) (range 0 16);
+    expected = {
+      "0" = [ 0 5 10 15 ];
+      "1" = [ 1 6 11 16 ];
+      "2" = [ 2 7 12 ];
+      "3" = [ 3 8 13 ];
+      "4" = [ 4 9 14 ];
+    };
+  };
+
+  testGroupBy' = {
+    expr = groupBy' builtins.add 0 (x: boolToString (x > 2)) [ 5 1 2 3 4 ];
+    expected = { false = 3; true = 12; };
   };
 }