From 97e15cc2b8e7642ced353cd210169b32c289fd2d Mon Sep 17 00:00:00 2001
From: Michael Raskin <7c6f434c@mail.ru>
Date: Wed, 20 Aug 2008 06:45:14 +0000
Subject: [PATCH] Noticed an associativity direction problem

svn path=/nixpkgs/trunk/; revision=12664
---
 pkgs/lib/default.nix | 22 +++++++++++++---------
 1 file changed, 13 insertions(+), 9 deletions(-)

diff --git a/pkgs/lib/default.nix b/pkgs/lib/default.nix
index a23b7d947efd..cdf1b2f174f5 100644
--- a/pkgs/lib/default.nix
+++ b/pkgs/lib/default.nix
@@ -32,16 +32,20 @@ rec {
 	else (innerComposedArgs f (y x))));
   composedArgs = f: innerComposedArgs f {};
 
+  defaultMerge = x : y: if builtins.isAttrs y then
+    x // y
+  else 
+    y x;
   sumTwoArgs = f: x: y: 
-    if builtins.isAttrs y then 
-      (f (x // y)) 
-    else 
-      (f (y x));
-  composedArgsAndFun = f : x : (f x) // {
-    meta = {
-      function = composedArgsAndFun (sumTwoArgs f x);
-    };
-  };
+    f (defaultMerge x y);
+  foldArgs = merger: f: init: x: 
+    let arg=(merger init (defaultMerge init x)); in
+      (f arg) // {
+        meta = {
+	  function = foldArgs merger f arg;
+	};
+      };
+  composedArgsAndFun = f: foldArgs (x: y: y) {} f;
 
   # example a = pairMap (x : y : x + y) ["a" "b" "c" "d"];
   # result: ["ab" "cd"]