Next: Common List Operations, Previous: Editing Buffer, Up: Top
This module provides procedures to do efficient in-order accumulation. To load it:
(use-modules (ice-9 accumulate))
An accumulator is a procedure that appends its arguments to a private list, in the order given, and returns the list when called with no argument.
Thus, you can replace this idiom:
(let loop ((item (get)) (acc '()))
(if (finished? item)
(reverse! acc)
(loop (get) (cons item acc))))
with something like:
(let ((acc (accumulator/one-only)))
(let loop ((item (get)))
(or (finished? item)
(begin (acc item) (loop (get)))))
(acc))
The only difference between a procedure created with accumulator
and one with accumulator/one-only is that the latter is slightly
more efficient for accumulating one item at a time.
Return an accumulator optimized for accumulating one item at a time.
To keep count of accumulated items, use accumulator/counting.
Like
accumulator, except that if the returned proc is called with the same object (they areeq?), a count of the accumulated items thus far is returned.(define acc (accumulator/counting #:count)) (acc 'once 'there) (acc #:count) ⇒ 2 (acc 'was '(only one) 1) (acc #:count) ⇒ 5 (acc) ⇒ (once there was (only one) 1)