3 ms·
For those that have a hard time reading the clojure code, i recomend this blogpost where they are explained with js. See: http://phuu.net/2014/08/31/csp-and-tr
by nickik 12y ago
For those that have a hard time reading the clojure code, i recomend this blogpost where they are explained with js.
See: http://phuu.net/2014/08/31/csp-and-transducers.html http://phuu.net/2014/08/31/csp-and-transducers.html
- elbenshira 12y agoThat article was what inspired me to write this blog post. In short, the key insight of the blog post is this (via untested javascript): You can define transforming functions like map and filter via reduce (e.g. foldleft or, for Ruby, inject) instead. For example, map([...], function (x) { return x + 1; }); Can be written as: // append (x + 1) to the result collection reduce([...], [], function (result, x) { return append(result, plusOne(x)); }); But notice that, above, we explicitly chose `plusOne` and `append`. If we allow a user to pass in any function instead, then we have a higher-order function: function mapping(f) { return function (reducing) { return function (result, input) { return reducing(result, f(input)); } }; } Now we can do: var xform = mapping(plusOne)(append); reduce([...], [], xform); `xform` is a transducer.