News for hackers. Stuff that matters.
Re:Clojure's Transducers are as fundamental as function comp... by moomin on Saturday August 30, 2014 @01:52PM (#8247052)
If you ignore the arities, ignore the internal state, and correctly observe the unwritten rules, yes transducers act like function composition. I looked into this here: http://www.colourcoding.net/blog/archive/2014/08/16/lets-wri...

I think Rich's innovation here is extremely clever and quite subtle, but it's pretty Clojure-specific, both in terms of the problem it solves and the way it solves it.

Re:Clojure's Transducers are as fundamental as function comp... by lkrubner on Saturday August 30, 2014 @02:59PM (#8247251)
The overall pattern of map-reduce is possible in most languages, certainly any language that has closures. And the idea of composing multiple reducing functions together is something that comes up in many languages. If a language does not have closures, you could do something similar in any language by walking an accumulating object through multiple loops, but that is awkward and ugly. But you could certainly do something like this in Javascript, through the clever use of multiple closures, composing the closures together. But transducers formalize this as an idiomatic part of Clojure.
Re:Clojure's Transducers are as fundamental as function comp... by Tuna-Fish on Saturday August 30, 2014 @05:14PM (#8247617)
In languages with more leeway on execution order, this problem can be made to go away. For example, Haskell's stream fusion combines back-to-back calls to list processing functions into a single iteration over the list.
Re:Clojure's Transducers are as fundamental as function comp... by seanmcdirmid on Saturday August 30, 2014 @09:47PM (#8248175)
Lazy evaluation can also be encoded fairly easily in strict languages using constructs like lazy stream abstractions.
Re:Clojure's Transducers are as fundamental as function comp... by jeremyjh on Sunday August 31, 2014 @12:14AM (#8248405)
Uh, sure but does any other language have a compiler that actually implements stream fusion?
Re:Clojure's Transducers are as fundamental as function comp... by gazarsgo on Sunday August 31, 2014 @10:39AM (#8249385)
Not in the compiler, but enjoy a Microsoft Research paper about Steno, a C# library that claims to be superior to stream fusion (section 8.2): http://research.microsoft.com/pubs/173946/paper-pldi.pdf

In case anyone is unaware, LINQ lets you represent queries as a series of function calls which it represents as a series of Expression objects and an in-memory AST.

Re:Clojure's Transducers are as fundamental as function comp... by seanmcdirmid on Sunday August 31, 2014 @09:45PM (#8251175)
There is also the dynamic language run-time (DLR), which as a generalization of LINQ (to support statements) is pretty powerful when writing up these tools on .NET. I use it often in my work.
Re:Clojure's Transducers are as fundamental as function comp... by DanWaterworth on Sunday August 31, 2014 @03:37AM (#8248697)
I don't believe GHC actually does implement stream fusion. I think stream fusion happens with rewrite rules.
Re:Clojure's Transducers are as fundamental as function comp... by riffraff on Sunday August 31, 2014 @04:50AM (#8248798)
pardon my ignorance, but what would be the difference between the two? Don't rewrite rules happen in GCH anyway?
Re:Clojure's Transducers are as fundamental as function comp... by DanWaterworth on Sunday August 31, 2014 @08:55AM (#8249190)
Sorry, I should have been clearer. The rewriting happens in GHC, but it's the application programmer who creates the rules. So, stream fusion isn't implemented by the compiler AFAIK.
Re:Clojure's Transducers are as fundamental as function comp... by riffraff on Monday September 01, 2014 @06:19AM (#8252226)
ah that makes sense, thanks.
Re:Clojure's Transducers are as fundamental as function comp... by moomin on Saturday August 30, 2014 @11:18PM (#8248340)
The thing is, it's something of a redefinition of what idiomatic clojure is. Overloading arity to do radically different things isn't idiomatic Clojure, but transducers do it at two levels. Equally, functions are expected to do explicit state passing e.g. old school drop, rather than implicit state e.g transducer drop.
Re:Clojure's Transducers are as fundamental as function comp... by kazinator on Saturday August 30, 2014 @09:48PM (#8248181)
What's clever is recognizing the "kernel" of a function like map. Hickey answers the question: if we take this list-processing/decimating function like "count-if" or "map" or whatever, and express it with reduce, what is that reducer function which we will need? And how is that reducer derived from or related to the function that goes into the list processing function, like the mapping function in map? He then makes those functions behave as methods (when called with one less argument) which give you that reducer.

Now what if this is done to reduce itself? What should a reduced arity (reduce fun) return? I think that is a noop: nothing needs to be done to fun to make it reduce under reduce, so (reduce fun) -> fun. I.e. to transduce the reducer "fun", an arbitrary function, into a function X such that (reduce X list) will do the job of (reduce fun list), we simply equate X with fun.