diff --git a/nix/lib/default.nix b/nix/lib/default.nix index a38a486..da93182 100644 --- a/nix/lib/default.nix +++ b/nix/lib/default.nix @@ -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; diff --git a/nix/lib/lists.nix b/nix/lib/lists.nix new file mode 100644 index 0000000..c0ad80a --- /dev/null +++ b/nix/lib/lists.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); + +}