summary refs log tree commit diff
path: root/pkgs/lib/default.nix
diff options
context:
space:
mode:
authorMarc Weber <marco-oweber@gmx.de>2008-11-04 21:40:51 +0000
committerMarc Weber <marco-oweber@gmx.de>2008-11-04 21:40:51 +0000
commit2bf82b4470371d47b68c97c8b23bc576c4004ac9 (patch)
treeb985127fae48c98e6e494b46b1074f579b210957 /pkgs/lib/default.nix
parentb9173d87553cf3202d6761808bdce60d8aa83b34 (diff)
downloadnixlib-2bf82b4470371d47b68c97c8b23bc576c4004ac9.tar
nixlib-2bf82b4470371d47b68c97c8b23bc576c4004ac9.tar.gz
nixlib-2bf82b4470371d47b68c97c8b23bc576c4004ac9.tar.bz2
nixlib-2bf82b4470371d47b68c97c8b23bc576c4004ac9.tar.lz
nixlib-2bf82b4470371d47b68c97c8b23bc576c4004ac9.tar.xz
nixlib-2bf82b4470371d47b68c97c8b23bc576c4004ac9.tar.zst
nixlib-2bf82b4470371d47b68c97c8b23bc576c4004ac9.zip
added examples for composedArgsAndFun, introducing setAttrMerge
svn path=/nixpkgs/trunk/; revision=13193
Diffstat (limited to 'pkgs/lib/default.nix')
-rw-r--r--pkgs/lib/default.nix30
1 files changed, 24 insertions, 6 deletions
diff --git a/pkgs/lib/default.nix b/pkgs/lib/default.nix
index 838f7d9eb422..23fd7b92ed20 100644
--- a/pkgs/lib/default.nix
+++ b/pkgs/lib/default.nix
@@ -40,12 +40,24 @@ rec {
   sumTwoArgs = f: x: y: 
     f (defaultMerge x y);
   foldArgs = merger: f: init: x: 
-    let arg=(merger init (defaultMergeArg init x)); in
-      (f arg) // {
-        passthru = (getAttr ["passthru"] {} arg) // {
-	  function = foldArgs merger f arg;
-	};
-      };
+    let arg=(merger init (defaultMergeArg init x)); in  
+      # now add the function with composed args already applied to the final attrs
+    setAttrMerge "passthru" {} (f arg) ( x : x // { function = foldArgs merger f arg; } );
+ 
+  # rec { # an example of how composedArgsAndFun can be used
+  #  a  = composedArgsAndFun (x : x) { a = ["2"]; meta = { d = "bar";}; };
+  #  # meta.d will be lost ! It's your task to preserve it (eg using a merge function)
+  #  b  = a.passthru.function { a = [ "3" ]; meta = { d2 = "bar2";}; };
+  #  # instead of passing/ overriding values you can use a merge function:
+  #  c  = b.passthru.function ( x: { a = x.a  ++ ["4"]; }); # consider using (maybeAttr "a" [] x)
+  # }
+  # result:
+  # {
+  #   a = { a = ["2"];     meta = { d = "bar"; }; passthru = { function = .. }; };
+  #   b = { a = ["3"];     meta = { d2 = "bar2"; }; passthru = { function = .. }; };
+  #   c = { a = ["3" "4"]; meta = { d2 = "bar2"; }; passthru = { function = .. }; };
+  #   # c2 is equal to c
+  # }
   composedArgsAndFun = f: foldArgs defaultMerge f {};
 
   # example a = pairMap (x : y : x + y) ["a" "b" "c" "d"];
@@ -526,6 +538,12 @@ rec {
   # adds / replaces an attribute of an attribute set
   setAttr = set : name : v : set // (nvs name v);
 
+  # setAttrMerge (similar to mergeAttrsWithFunc but only merges the values of a particular name)
+  # setAttrMerge "a" [] { a = [2];} (x : x ++ [3]) -> { a = [2 3]; } 
+  # setAttrMerge "a" [] {         } (x : x ++ [3]) -> { a = [  3]; }
+  setAttrMerge = name : default : attrs : f :
+    setAttr attrs name (f (maybeAttr name default attrs));
+
   # iterates over a list of attributes collecting the attribute attr if it exists
   catAttrs = attr : l : fold ( s : l : if (hasAttr attr s) then [(builtins.getAttr attr s)] ++ l else l) [] l;