I do wish the article did a better job of distinguishing between 1) laziness as a concept, and 2) clojure's lazy seq functions like the 3+ arity versions of map, take, filter, etc.
The article also conflates transducers with eagerness even though transducers can be used in either lazy or eager contexts.
As mentioned, there are multiple ways to implement laziness and clojure even has more than one option for laziness!
Specifically, the article ignores both clojure.core/sequence and clojure.core/eduction which each offer lazy options with different tradeoffs and performance characteristics.
In general, using sequence with transducers is a more efficient lazy option than the 3+ arity seq functions (although still generally slower than into, mapv). It would be nice to include it in the microbenchmarks.
In some cases, lazy seqs can also be replaced with eduction. Eductions offer some of the benefits of laziness with performance similar (or better depending on the context) than some of the eager approaches.
Edit: On second read, I did see the author mentioned both sequence and eduction at the bottom, but I think it would have been useful to include them earlier in the discussion.
On second read, I did see the author mentioned both sequence and eduction at the bottom, but I think it would have been useful to include them earlier in the discussion.
The article was already too long, and going in detail about transducers and how to use them properly is another rabbit hole I was not willing to take here. Perhaps, it makes a good topic for the follow-up post.
The article also conflates transducers with eagerness even though transducers can be used in either lazy or eager contexts.
Wasn't my intent. I see transducers as an explicit composable transformation rather than implicit. Again, the next post can resolve the confusion, had no space to properly do it here.
The article was already too long, and going in detail about transducers and how to use them properly is another rabbit hole I was not willing to take here. Perhaps, it makes a good topic for the follow-up post.
I think there are a few places where sequence and eduction can at least be mentioned without ballooning the article. Specifically, there's a list under "There are several sources whence a developer can obtain a lazy sequence:" where sequence and eduction go unmentioned. Even if the article doesn't do a deep dive on those options, it seems like it would benefit the discussion to at least give them a mention.
Wasn't my intent. I see transducers as an explicit composable transformation rather than implicit. Again, the next post can resolve the confusion, had no space to properly do it here.
I think there are some simple changes that can avoid some of the confusion:
There's a benchmark between "lazy map", "eager mapv", and "Transducers" that I think would be better labeled as "lazy map" vs "eager mapv" vs "into".
The same benchmark omits eduction and sequence (that would make the article slightly longer, but I think it's worthwhile).
The sentence "Transducers are overall an adequate replacement for lazy sequences" is a bit confusing since transducers can be eager or lazy. Maybe something like "into is often an good replacement for lazy sequences".
One last nitpick:
The classic hand rolled loop has some unnecessary seq calls. I would instead write it as:
A can do both a and b, B can only do b. Can you say that A is a sufficient replacement for B?
Choosing lazy sequences and choosing transducers are two independent choices.
Maybe I'm misunderstanding the context, but it seemed like the recommendation was to prefer into over lazy sequences since just using transducers doesn't actually affect laziness.
Not sure it contains unnecessary seq calls, but it is overall wrong (stops iterating if the sequence contains a nil). I'll rewrite it correctly.
It's a little bit getting into the weeds, but rest destructuring expands to 1) a call to seq and 2) a call to rest. So every loop, there's two calls to get the rest of the sequence.
I see now about calling seq twice (explicitly and inside next). I've fixed the bug in the example, but I'm actually keeping the example written in the original way, even though yours is faster and more fair to lazy sequences. The reason is that people prefer destructuring, and I've seen and written many more loops over lazy sequences using destructuring than in the faster manner that you've suggested.
I don't think it's unfair to lazy sequences because none of the benchmarks/code examples from "Inefficient iteration with sequence API" are iterating over lazy sequences (it's also possible to iterate over lazy sequences more efficiently with IReduceInit).
There's several concepts at play here:
laziness in the abstract
the various implementations in clojure that support laziness
the various methods for iterating over sequences and collections.
the various methods for transforming sequences and collections
Many of the newer APIs simplify these concepts and allow clojure devs to make independent choices for each of these concepts.
In some places, the article does a good job teasing these apart, but in some places, there's imprecise language that IMO conflates independent concepts (eg. transducers and laziness).
Precisely describing all the concepts at play is a difficult job since some APIs also couple independent concepts (eg. the 3+ arity lazy seq fns like map, filter, etc).
Even if explaining all of the details for all the concepts is too much for one post, I think there's a lot of value in choosing the right word for the concept being discussed.
4
u/phronmophobic Jul 28 '23 edited Jul 29 '23
Great article!
I do wish the article did a better job of distinguishing between 1) laziness as a concept, and 2) clojure's lazy seq functions like the 3+ arity versions of
map,take,filter, etc.The article also conflates transducers with eagerness even though transducers can be used in either lazy or eager contexts.
As mentioned, there are multiple ways to implement laziness and clojure even has more than one option for laziness!
Specifically, the article ignores both
clojure.core/sequenceandclojure.core/eductionwhich each offer lazy options with different tradeoffs and performance characteristics.In general, using
sequencewith transducers is a more efficient lazy option than the 3+ arity seq functions (although still generally slower thaninto,mapv). It would be nice to include it in the microbenchmarks.In some cases, lazy seqs can also be replaced with
eduction. Eductions offer some of the benefits of laziness with performance similar (or better depending on the context) than some of the eager approaches.Edit: On second read, I did see the author mentioned both sequence and eduction at the bottom, but I think it would have been useful to include them earlier in the discussion.