summary refs log tree commit diff
path: root/pkgs/lib/attrsets.nix
diff options
context:
space:
mode:
authorEelco Dolstra <eelco.dolstra@logicblox.com>2012-05-25 17:01:58 +0000
committerEelco Dolstra <eelco.dolstra@logicblox.com>2012-05-25 17:01:58 +0000
commit89af9f3f4c95c3dcca5c4f33b830201ab20b75bf (patch)
tree8b48db644545b7608aa0cdd3f3ebf7563bd25617 /pkgs/lib/attrsets.nix
parent3ff236c739c9e7973ea9d94f64582997761e0254 (diff)
downloadnixpkgs-89af9f3f4c95c3dcca5c4f33b830201ab20b75bf.tar
nixpkgs-89af9f3f4c95c3dcca5c4f33b830201ab20b75bf.tar.gz
nixpkgs-89af9f3f4c95c3dcca5c4f33b830201ab20b75bf.tar.bz2
nixpkgs-89af9f3f4c95c3dcca5c4f33b830201ab20b75bf.tar.lz
nixpkgs-89af9f3f4c95c3dcca5c4f33b830201ab20b75bf.tar.xz
nixpkgs-89af9f3f4c95c3dcca5c4f33b830201ab20b75bf.tar.zst
nixpkgs-89af9f3f4c95c3dcca5c4f33b830201ab20b75bf.zip
* Add a variant of mapAttrs that allows rewriting the name of each
  attribute in addition to the value.

svn path=/nixpkgs/trunk/; revision=34246
Diffstat (limited to 'pkgs/lib/attrsets.nix')
-rw-r--r--pkgs/lib/attrsets.nix19
1 files changed, 16 insertions, 3 deletions
diff --git a/pkgs/lib/attrsets.nix b/pkgs/lib/attrsets.nix
index db4b9fb210c..260c61558a4 100644
--- a/pkgs/lib/attrsets.nix
+++ b/pkgs/lib/attrsets.nix
@@ -126,12 +126,25 @@ rec {
 
      Example:
        mapAttrs (name: value: name + "-" + value)
-          {x = "foo"; y = "bar";}
-       => {x = "x-foo"; y = "y-bar";}
+          { x = "foo"; y = "bar"; }
+       => { x = "x-foo"; y = "y-bar"; }
   */
   mapAttrs = f: set:
     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