1
0
Fork 1
mirror of https://github.com/NixOS/nixpkgs.git synced 2024-12-01 10:00:56 +00:00

Merge pull request #134969 from polykernel/setattrsbypath-patch

lib: optimize setAttrByPath and cleaup imports
This commit is contained in:
Robert Hensing 2021-08-24 00:38:30 +02:00 committed by GitHub
commit 9a775bf778
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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