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'.
This commit is contained in:
Ludovic Courtès 2011-05-08 18:19:52 +02:00
commit 19301dc56d
3 changed files with 23 additions and 5 deletions

View file

@ -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]]

View file

@ -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)

View file

@ -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))