* Add a variant of mapAttrs that allows rewriting the name of each

attribute in addition to the value.

svn path=/nixpkgs/trunk/; revision=34246
This commit is contained in:
Eelco Dolstra 2012-05-25 17:01:58 +00:00
parent 3ff236c739
commit 89af9f3f4c
1 changed files with 16 additions and 3 deletions

View File

@ -133,6 +133,19 @@ rec {
listToAttrs (map (attr: nameValuePair attr (f attr (getAttr attr set))) (attrNames set));
/* Like `mapAttrs', but allows the name of each attribute to be
changed in addition to the value. The applied function should
return both the new name and value as a `nameValuePair'.
Example:
mapAttrs' (name: value: nameValuePair ("foo_" + name) ("bar-" + value))
{ x = "a"; y = "b"; }
=> { foo_x = "bar-a"; foo_y = "bar-b"; }
*/
mapAttrs' = f: set:
listToAttrs (map (attr: f attr (getAttr attr set)) (attrNames set));
/* Like `mapAttrs', except that it recursively applies itself to
attribute sets. Also, the first argument of the argument
function is a *list* of the names of the containing attributes.