diff options
author | polykernel <81340136+polykernel@users.noreply.github.com> | 2021-08-20 12:24:39 -0400 |
---|---|---|
committer | polykernel <81340136+polykernel@users.noreply.github.com> | 2021-08-23 18:04:47 -0400 |
commit | 3f4ce46a4702940cb7d1d12349c0407576225605 (patch) | |
tree | 54f280d7479dcdd0883db59b72649712db072600 /lib/attrsets.nix | |
parent | 6fb5d4cedc1d07f9bd8d4e0cb678dfe3d6c6424a (diff) | |
download | nixpkgs-3f4ce46a4702940cb7d1d12349c0407576225605.tar nixpkgs-3f4ce46a4702940cb7d1d12349c0407576225605.tar.gz nixpkgs-3f4ce46a4702940cb7d1d12349c0407576225605.tar.bz2 nixpkgs-3f4ce46a4702940cb7d1d12349c0407576225605.tar.lz nixpkgs-3f4ce46a4702940cb7d1d12349c0407576225605.tar.xz nixpkgs-3f4ce46a4702940cb7d1d12349c0407576225605.tar.zst nixpkgs-3f4ce46a4702940cb7d1d12349c0407576225605.zip |
lib: optimize setAttrByPath and cleaup imports
- Remove inheritance of `lists.fold` as it isn't used anywhere. - Inherit `foldl'` for consistency as only `cartesianProductOfSets` explicitly reference lib. - Inline `foldr` to generate nested attrs instead of using `listToAttrs` and `tail`.
Diffstat (limited to 'lib/attrsets.nix')
-rw-r--r-- | lib/attrsets.nix | 15 |
1 files changed, 9 insertions, 6 deletions
diff --git a/lib/attrsets.nix b/lib/attrsets.nix index 0b61819f6b4..31fddc59e20 100644 --- a/lib/attrsets.nix +++ b/lib/attrsets.nix @@ -5,7 +5,7 @@ let inherit (builtins) head tail length; inherit (lib.trivial) and; inherit (lib.strings) concatStringsSep sanitizeDerivationName; - inherit (lib.lists) fold foldr concatMap concatLists; + inherit (lib.lists) foldr foldl' concatMap concatLists elemAt; in rec { @@ -55,10 +55,13 @@ rec { => { a = { b = 3; }; } */ setAttrByPath = attrPath: value: - if attrPath == [] then value - else listToAttrs - [ { name = head attrPath; value = setAttrByPath (tail attrPath) value; } ]; - + let + len = length attrPath; + atDepth = n: + if n == len + then value + else { ${elemAt attrPath n} = atDepth (n + 1); }; + in atDepth 0; /* Like `attrByPath' without a default value. If it doesn't find the path it will throw. @@ -195,7 +198,7 @@ rec { ] */ cartesianProductOfSets = attrsOfLists: - lib.foldl' (listOfAttrs: attrName: + foldl' (listOfAttrs: attrName: concatMap (attrs: map (listValue: attrs // { ${attrName} = listValue; }) attrsOfLists.${attrName} ) listOfAttrs |