mirror of
https://git.savannah.gnu.org/git/guile.git
synced 2025-06-11 14:21:10 +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:
parent
0a6506781a
commit
bc00e06c7e
1 changed files with 8 additions and 1 deletions
|
@ -245,7 +245,14 @@ tail."
|
||||||
(define (vlist-fold-right proc init vlist)
|
(define (vlist-fold-right proc init vlist)
|
||||||
"Fold over @var{vlist}, calling @var{proc} for each element, starting from
|
"Fold over @var{vlist}, calling @var{proc} for each element, starting from
|
||||||
the last element."
|
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)
|
(define (vlist-reverse vlist)
|
||||||
"Return a new @var{vlist} whose content are those of @var{vlist} in reverse
|
"Return a new @var{vlist} whose content are those of @var{vlist} in reverse
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue