Discussion
Transducers
eduction: The key insight behind transducers is that a ton of performance is lost not to bad algorithms or slow interpreters but to copying things around needlessly in memory, specifically through intermediate collections.While the mechanics of transducers are interesting the bottom line is they allow you to fuse functions and basic conditional logic together in such a way that you transform a collection exactly once instead of n times, meaning new allocation happens only once. Once you start using them you begin to see intermediate collections everywhere.Of course, in any language you can theoretically do everything in one hyperoptimized loop; transducers get you this loop without much of a compromise on keeping your program broken into simple, composable parts where intent is very clear. In fact your code ends up looking nearly identical (especially once you learn about eductions… cough).
fud101: These sound wild in terms of promise but I never understood them in a practical way.
drob518: Transducers work even better with a Clojure library called Injest. It has macros similar to the standard Clojure threading macros except Injest’s macros will recognize when you’re using transducers and automatically compose them correctly. You can even mix and match transducers and non-transducer functions and Injest will do its best to optimize the sequence of operations. And wait, there’s more! Injest has a parallelizing macro that will use transducers with the Clojure reducers library for simple and easy use of all your cores. Get it here: https://github.com/johnmn3/injestNote: I’m not the author of Injest, just a satisfied programmer.
talkingtab: When I first read about transducers I was wowed. For example, if I want to walk all the files on my computer and find the duplicate photos in the whole file system, transducers provide a conveyor belt approach. And whether there are saving in terms of memory or anything, maybe. But the big win for me was to think about the problem as pipes instead of loops. And then if you could add conditionals and branches it is even easier to think about. At least I find it so.I tried to implement transducers in JavaScript using yield and generators and that worked. That was before async/await, but now you can just `await readdir("/"); I'm unclear as to whether transducers offer significant advantages over async/await?[[Note: I have a personal grudge against Java and since Clojure requires Java I just find myself unable to go down that road]]
blast0ff: You can't be serious. This is such a trivial problem. All you need is a loop with a stack and a hashmap to keep track of checksums. And it would be 100x more readable than whatever transducer based solution anyone could come up with. Reminds me of this video:https://www.youtube.com/watch?v=b2F-DItXtZs
thih9: From (2016) at least.https://web.archive.org/web/20161219045343/https://clojure.o...
whalesalad: It's a blessing and a curse that zero innovation has occurred in the Clojure space since 2016. Pretty sure the only big things has been clojure.spec becoming more mainstream and the introduction of deps.edn to supplant lein. altho I am still partial to lein.
seancorfield: Clojure 1.9: Spec.Clojure 1.10: datafy/nav + tap> which has spawned a whole new set of tooling for exploring data.Clojure 1.11: portable math (clojure.math, which also works on ClojureScript).Clojure 1.12: huge improvements in Java interop.And, yes, the new CLI and deps.edn, and tools.build to support "builds as programs".
instig007: You get this for free in Haskell, and you also save on not having to remember useless terminology for something that has no application on their own outside Foldables anyways.
eduction: Canonical example is rewriting a non transducing set of collection transformations like (->> posts (map with-user) (filter authorized?) (map with-friends) (into [])) That’s five collections, this is two, using transducers: (into [] (comp (map with-user) (filter authorized?) (map with-friends)) posts) A transducer is returned by comp, and each item within comp is itself a transducer. You can see how the flow is exactly like the double threading macro.map for example is called with one arg, this means it will return a transducer, unlike in the first example when it has a second argument, the coll posts, so immediately runs over that and returns a new coll.The composed transducer returned by comp is passed to into as the second of three arguments. In three argument form, into applies the transducer to each item in coll, the third argument. In two argument form, as in the first example, it just puts coll into the first argument (also a coll).
kccqzy: That does not sound like a good example. The two-argument form of `map` already returns a lazy sequence. Same for `filter`. I thought lazy sequences are already supposed to get rid of the performance problem of materializing the entire collection. So