add lib.lists.groupByMult

main
Felix Stupp 1 year ago
parent ea7a167bf9
commit 07c9126aa9
Signed by: zocker
GPG Key ID: 93E1BD26F6B02FB7

@ -11,6 +11,7 @@ nixpkgs.lib
# groups
// mapAttrs (autoExtend nixpkgs.lib) {
attrsets = ./attrsets.nix;
lists = ./lists.nix;
math = ./math.nix;
modules = ./modules.nix;
network = ./network.nix;

@ -0,0 +1,42 @@
{ lib, ... }@flakeArg:
let
inherit (builtins)
deepSeq
foldl'
groupBy
mapAttrs
;
inherit (lib.lists) singleton;
inherit (lib.trivial) flip pipe;
in
{
groupByMult =
groupers:
# TODO (maybe) make more efficient by building up grouping function from right
# from left
# 0= (x: x) vals
# 1= groupBy values[0] <0>
# 2= mapAttrs (_: groupBy values[1]) <1>
# 3= mapAttrs (_: mapAttrs (_: groupBy values[2])) <2>
# from right
# 1= (groupBy values[0])
# 2= ...
let
nul = {
mapper = x: x;
result = [ ];
};
op =
{ mapper, result }:
grouper: {
mapper = fun: mapAttrs (_: mapper fun);
result = result ++ singleton (mapper grouper);
};
list = map groupBy groupers;
pipeList = (foldl' op nul list).result;
in
# catch errors while building groupers before values are passed through
deepSeq pipeList (flip pipe pipeList);
}
Loading…
Cancel
Save