This post highlights of one of the core ideas posted in this
you’ve already read it and you’re intimately familiar with
transducers, this post probably won’t have anything new for you. I’ve
posted this to
Stackoverflow before and
saving this to my blog for archival purposes.
In the pre-transducer era, reading text files was often done like this:
this would return (1 2 1 6 4 4 6). One caveat with this approach is
you have to realize the result inside the with-open macro, else the
file would already be closed.
What if we want to use transducers
instead of lazy collection transformations? The ingredient you need is
something that allows you to treat the lines as a reducible collection
and which closes the reader when you’re done reducing: