1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-06-10 14:00:21 +02:00

Optimize `vlist-fold-right'.

* module/ice-9/vlist.scm (vlist-fold-right): Avoid `vlist-reverse' and
  instead `vlist-ref' individual elements.  The result is about twice
  faster.  Thanks Andy for suggesting it indirectly.  :-)
This commit is contained in:
Ludovic Courtès 2011-05-08 18:15:10 +02:00
parent 0a6506781a
commit bc00e06c7e

View file

@ -245,7 +245,14 @@ tail."
(define (vlist-fold-right proc init vlist)
"Fold over @var{vlist}, calling @var{proc} for each element, starting from
the last element."
(vlist-fold proc init (vlist-reverse vlist)))
(define len (vlist-length vlist))
(let loop ((index (1- len))
(result init))
(if (< index 0)
result
(loop (1- index)
(proc (vlist-ref vlist index) result)))))
(define (vlist-reverse vlist)
"Return a new @var{vlist} whose content are those of @var{vlist} in reverse