2 ms·
Transducers are an under-appreciated feature in Clojure. They are incredibly useful, allow for composable and reusable code, and come with really nice performan
by jwr 3y ago
Transducers are an under-appreciated feature in Clojure. They are incredibly useful, allow for composable and reusable code, and come with really nice performance benefits as well (by not creating intermediate collections).
Once you get used to them, they become a very natural tool — in my case, almost every time I do something to a sequence, I'll start with `into`. Even if I'm just applying a single transformation, this lets me quickly change the resulting collection and easily add more transformations if needed.
On a higher level, I found myself thinking about business logic (model) in terms of transducer pipelines and ended up with a number of reusable transformations, and clearly specified pipeline logic.
One gripe I have with transducers is that writing stateful transducers is hard. As in, well, really hard (hope you remembered to flush using `unreduced` in your single-arity version!). I still write them sometimes, but it's never a walk in the park (it does provide satisfaction when done, though). But I guess that's what you need to go through in order to get good performance.
- geokon 3y agojoinr pointed out this lib to me and it seems to make using them more ergonomic (if you're into pipelining) https://github.com/johnmn3/injest https://github.com/johnmn3/injest However writing your own indeed doesn't sound fun :))
- jwr 3y agoHmm. I don't see much value in the new syntax: the current syntax works for me just fine.
- geokon 3y agoI use pipelining everywhere. Going from piping through map/reduce/filter/etc back to inside out Lisp style calls felt like a huge ergonomic step back. Maybe it's a matter of familiarity but it's hard for me to visually scan that kind of code