diff options
author | Jaka Hudoklin <jakahudoklin@gmail.com> | 2015-09-19 00:16:06 +0200 |
---|---|---|
committer | Jaka Hudoklin <jakahudoklin@gmail.com> | 2015-09-19 00:33:44 +0200 |
commit | 341ac85644e93f145277be93db2a732ae3409249 (patch) | |
tree | eeb68f67ae0ae3738795a65d8bb7e0390c92e8ef /lib/attrsets.nix | |
parent | 5baac1e9625b609ef8a76acfe4487433955a83b0 (diff) | |
download | nixpkgs-341ac85644e93f145277be93db2a732ae3409249.tar nixpkgs-341ac85644e93f145277be93db2a732ae3409249.tar.gz nixpkgs-341ac85644e93f145277be93db2a732ae3409249.tar.bz2 nixpkgs-341ac85644e93f145277be93db2a732ae3409249.tar.lz nixpkgs-341ac85644e93f145277be93db2a732ae3409249.tar.xz nixpkgs-341ac85644e93f145277be93db2a732ae3409249.tar.zst nixpkgs-341ac85644e93f145277be93db2a732ae3409249.zip |
Add lib.filterAttrsRecursive function
Diffstat (limited to 'lib/attrsets.nix')
-rw-r--r-- | lib/attrsets.nix | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/lib/attrsets.nix b/lib/attrsets.nix index 2300ee9c000..5aad76e75e4 100644 --- a/lib/attrsets.nix +++ b/lib/attrsets.nix @@ -78,6 +78,26 @@ rec { listToAttrs (concatMap (name: let v = set.${name}; in if pred name v then [(nameValuePair name v)] else []) (attrNames set)); + /* Filter an attribute set recursivelly by removing all attributes for + which the given predicate return false. + + Example: + filterAttrsRecursive (n: v: v != null) { foo = { bar = null; }; } + => { foo = {}; } + */ + filterAttrsRecursive = pred: set: + listToAttrs ( + concatMap (name: + let v = set.${name}; in + if pred name v then [ + (nameValuePair name ( + if isAttrs v then filterAttrsRecursive pred v + else v + )) + ] else [] + ) (attrNames set) + ); + /* foldAttrs: apply fold functions to values grouped by key. Eg accumulate values as list: foldAttrs (n: a: [n] ++ a) [] [{ a = 2; } { a = 3; }] => { a = [ 2 3 ]; } |