diff options
author | Alyssa Ross <hi@alyssa.is> | 2021-09-08 17:57:14 +0000 |
---|---|---|
committer | Alyssa Ross <hi@alyssa.is> | 2021-09-13 11:31:47 +0000 |
commit | ee7984efa14902a2ddd820c937457667a4f40c6a (patch) | |
tree | c9c1d046733cefe5e21fdd8a52104175d47b2443 /nixpkgs/lib/attrsets.nix | |
parent | ffc9d4ba381da62fd08b361bacd1e71e2a3d934d (diff) | |
parent | b3c692172e5b5241b028a98e1977f9fb12eeaf42 (diff) | |
download | nixlib-ee7984efa14902a2ddd820c937457667a4f40c6a.tar nixlib-ee7984efa14902a2ddd820c937457667a4f40c6a.tar.gz nixlib-ee7984efa14902a2ddd820c937457667a4f40c6a.tar.bz2 nixlib-ee7984efa14902a2ddd820c937457667a4f40c6a.tar.lz nixlib-ee7984efa14902a2ddd820c937457667a4f40c6a.tar.xz nixlib-ee7984efa14902a2ddd820c937457667a4f40c6a.tar.zst nixlib-ee7984efa14902a2ddd820c937457667a4f40c6a.zip |
Merge commit 'b3c692172e5b5241b028a98e1977f9fb12eeaf42'
Diffstat (limited to 'nixpkgs/lib/attrsets.nix')
-rw-r--r-- | nixpkgs/lib/attrsets.nix | 21 |
1 files changed, 12 insertions, 9 deletions
diff --git a/nixpkgs/lib/attrsets.nix b/nixpkgs/lib/attrsets.nix index 5c787940cb0c..31fddc59e20e 100644 --- a/nixpkgs/lib/attrsets.nix +++ b/nixpkgs/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 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. @@ -152,8 +155,8 @@ rec { => { a = [ 2 3 ]; } */ foldAttrs = op: nul: list_of_attrs: - fold (n: a: - fold (name: o: + foldr (n: a: + foldr (name: o: o // { ${name} = op n.${name} (a.${name} or nul); } ) a (attrNames n) ) {} list_of_attrs; @@ -195,7 +198,7 @@ rec { ] */ cartesianProductOfSets = attrsOfLists: - lib.foldl' (listOfAttrs: attrName: + foldl' (listOfAttrs: attrName: concatMap (attrs: map (listValue: attrs // { ${attrName} = listValue; }) attrsOfLists.${attrName} ) listOfAttrs @@ -455,7 +458,7 @@ rec { => true */ matchAttrs = pattern: attrs: assert isAttrs pattern; - fold and true (attrValues (zipAttrsWithNames (attrNames pattern) (n: values: + foldr and true (attrValues (zipAttrsWithNames (attrNames pattern) (n: values: let pat = head values; val = head (tail values); in if length values == 1 then false else if isAttrs pat then isAttrs val && matchAttrs pat val |