summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLudovic Courtès <ludo@gnu.org>2011-05-08 18:19:52 +0200
committerLudovic Courtès <ludo@gnu.org>2011-05-08 18:20:42 +0200
commit19301dc56d297a24eedc48928bb1b7df40cd4688 (patch)
treee5a9383fdcadc698bcf6ff46dd09554e7a181f3e
parentbc00e06c7ec5575f405bee4e12a062d4269f4eab (diff)
downloadguile-19301dc56d297a24eedc48928bb1b7df40cd4688.tar.gz
Add `vhash-fold-right'.
* module/ice-9/vlist.scm (vhash-fold-right): New procedure. * test-suite/tests/vlist.test ("vhash")["vhash-fold-right"]: New test. * doc/ref/api-compound.texi (VHashes): Document `vhash-fold-right'.
-rw-r--r--doc/ref/api-compound.texi9
-rw-r--r--module/ice-9/vlist.scm12
-rw-r--r--test-suite/tests/vlist.test7
3 files changed, 23 insertions, 5 deletions
diff --git a/doc/ref/api-compound.texi b/doc/ref/api-compound.texi
index 27ba437ad..da8813baf 100644
--- a/doc/ref/api-compound.texi
+++ b/doc/ref/api-compound.texi
@@ -1,7 +1,7 @@
@c -*-texinfo-*-
@c This is part of the GNU Guile Reference Manual.
-@c Copyright (C) 1996, 1997, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2009, 2010
-@c Free Software Foundation, Inc.
+@c Copyright (C) 1996, 1997, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
+@c 2007, 2009, 2010, 2011 Free Software Foundation, Inc.
@c See the file guile.texi for copying conditions.
@node Compound Data Types
@@ -3294,8 +3294,9 @@ Again the choice of @var{hash-proc} must be consistent with previous calls to
@end deffn
@deffn {Scheme Procedure} vhash-fold proc vhash
-Fold over the key/pair elements of @var{vhash}. For each pair call @var{proc}
-as @code{(@var{proc} key value result)}.
+@deffnx {Scheme Procedure} vhash-fold-right proc vhash
+Fold over the key/value elements of @var{vhash} in the given direction.
+For each pair call @var{proc} as @code{(@var{proc} key value result)}.
@end deffn
@deffn {Scheme Procedure} vhash-fold* proc init key vhash [equal? [hash]]
diff --git a/module/ice-9/vlist.scm b/module/ice-9/vlist.scm
index b554e1105..d5e28d540 100644
--- a/module/ice-9/vlist.scm
+++ b/module/ice-9/vlist.scm
@@ -33,7 +33,7 @@
vhash? vhash-cons vhash-consq vhash-consv
vhash-assoc vhash-assq vhash-assv
vhash-delete vhash-delq vhash-delv
- vhash-fold
+ vhash-fold vhash-fold-right
vhash-fold* vhash-foldq* vhash-foldv*
alist->vhash))
@@ -560,6 +560,16 @@ with @var{equal?}."
seed
vhash))
+(define (vhash-fold-right proc seed vhash)
+ "Fold over the key/pair elements of @var{vhash}, starting from the 0th
+element. For each pair call @var{proc} as @code{(@var{proc} key value
+result)}."
+ (vlist-fold-right (lambda (key+value result)
+ (proc (car key+value) (cdr key+value)
+ result))
+ seed
+ vhash))
+
(define* (alist->vhash alist #:optional (hash hash))
"Return the vhash corresponding to @var{alist}, an association list."
(fold-right (lambda (pair result)
diff --git a/test-suite/tests/vlist.test b/test-suite/tests/vlist.test
index b590bbda1..d939284c1 100644
--- a/test-suite/tests/vlist.test
+++ b/test-suite/tests/vlist.test
@@ -301,6 +301,13 @@
(alist (fold alist-cons '() keys values)))
(equal? alist (reverse (vhash-fold alist-cons '() vh)))))
+ (pass-if "vhash-fold-right"
+ (let* ((keys '(a b c d e f g d h i))
+ (values '(1 2 3 4 5 6 7 0 8 9))
+ (vh (fold vhash-cons vlist-null keys values))
+ (alist (fold alist-cons '() keys values)))
+ (equal? alist (vhash-fold-right alist-cons '() vh))))
+
(pass-if "alist->vhash"
(let* ((keys '(a b c d e f g d h i))
(values '(1 2 3 4 5 6 7 0 8 9))