Re: Generators and accumulators
Lassi Kortela 21 Jan 2020 22:57 UTC
>> Are character-by-character generators and accumulators reasonably fast?
>> Are they amenable to well-known compiler optimizations?
> Gens and accs are just procedures of zero arguments and one argument
> respectively, so their efficiency is constrained by what they do. Doing
> JSON pretty much requires char-by-char parsing.
This inlines nicely in Chez and Loko, gets rid of the generator lambda:
(expand/optimize
'(let* ((chars (string->list "hello world"))
(gen (lambda ()
(if (null? chars) (eof-object)
(let ((char (car chars)))
(set! chars (cdr chars))
char)))))
(let loop ()
(let ((char (gen)))
(if (eof-object? char) (newline)
(begin (display char) (loop)))))))