aboutsummaryrefslogtreecommitdiff
path: root/guix/utils.scm
diff options
context:
space:
mode:
authorLudovic Courtès <ludo@gnu.org>2013-04-14 16:56:08 +0200
committerLudovic Courtès <ludo@gnu.org>2013-04-14 16:56:08 +0200
commit04fd96cac33fa7557e574e54575252564ba27111 (patch)
tree42f0ca2251cf6effb82b9d38f7789e2ad54842a8 /guix/utils.scm
parent77ffd691bfbb152cde94b60aa8df5135d39727c3 (diff)
downloadgnu-guix-04fd96cac33fa7557e574e54575252564ba27111.tar
gnu-guix-04fd96cac33fa7557e574e54575252564ba27111.tar.gz
utils: Add `fold2'.
* gnu/packages.scm (fold2): Remove. * guix/utils.scm (fold2): New procedure. Generalization of the above to one and two lists. * tests/utils.scm ("fold2, 1 list", "fold2, 2 lists"): New tests.
Diffstat (limited to 'guix/utils.scm')
-rw-r--r--guix/utils.scm29
1 files changed, 28 insertions, 1 deletions
diff --git a/guix/utils.scm b/guix/utils.scm
index d7c37e37d1..f13e585e2b 100644
--- a/guix/utils.scm
+++ b/guix/utils.scm
@@ -59,7 +59,8 @@
%current-system
version-compare
version>?
- package-name->name+version))
+ package-name->name+version
+ fold2))
;;;
@@ -463,6 +464,32 @@ introduce the version part."
((head tail ...)
(loop tail (cons head prefix))))))
+(define fold2
+ (case-lambda
+ ((proc seed1 seed2 lst)
+ "Like `fold', but with a single list and two seeds."
+ (let loop ((result1 seed1)
+ (result2 seed2)
+ (lst lst))
+ (if (null? lst)
+ (values result1 result2)
+ (call-with-values
+ (lambda () (proc (car lst) result1 result2))
+ (lambda (result1 result2)
+ (loop result1 result2 (cdr lst)))))))
+ ((proc seed1 seed2 lst1 lst2)
+ "Like `fold', but with a two lists and two seeds."
+ (let loop ((result1 seed1)
+ (result2 seed2)
+ (lst1 lst1)
+ (lst2 lst2))
+ (if (or (null? lst1) (null? lst2))
+ (values result1 result2)
+ (call-with-values
+ (lambda () (proc (car lst1) (car lst2) result1 result2))
+ (lambda (result1 result2)
+ (fold2 proc result1 result2 (cdr lst1) (cdr lst2)))))))))
+
;;;
;;; Source location.