Transducers.js: A JavaScript Library for Transformation of Data
jlongster.com
jlongster.com
I feel a bit underwhelmed by these examples. Seems a bit to imperative, an enhancement of underscore. I want the declarations of how data representations relate to each other to be separate from the "how" in how to transform from one format to another. Probably something inspired by lenses (1). "Lenses are the XPath of _everything_." (2).
(1) - https://www.fpcomplete.com/school/to-infinity-and-beyond/pic...
(2) - https://twitter.com/steveklabnik/status/463748643141349376
edit: Relevant HN comment about using Lenses for reaching into data objects: https://news.ycombinator.com/item?id=7704504
You will rarely actually use `transduce` though, just like you probably rarely use `reduce`. You use the other two functions, `into` and `sequence` a lot more, and you don't really have to understand what's going on underneath (even if it is is pretty simple):
sequence(compose(map(x => x + 1),
filter(x => x < 10)
take(10)),
[1, 2, 3]);
It is a little different than standard imperative code, but I think you will find that it's more expressive. // induction : ((t (Fix t), Fix t -> r) -> t r, t r -> r) -> Fix t -> r
function induction(map, f) {
// g : Fix t -> r
function g(x) {
return f(map(x.unfold(), g));
};
return g;
};
// coinduction : ((t s, s -> Fix t) -> t (Fix t), s -> t s) -> s -> Fix t
function coinduction(map, f) {
// g : s -> Fix t
function g(s) {
return {
unfold: function() {
return map(f(s), g);
}
};
};
return g;
};
// fold : t (Fix t) -> Fix t
function fold(w) {
return {
unfold: function() { return w; }
};
};Thanks for the great library and explanation.
That said, I easily get turned off by something if it isn't obvious within 10 minutes how it works. This really isn't that complex. I think people who read my post can overcome the weird word once they see how simple it is.
There's a large group of people where it helps to have the clojure connection; I'll keep posting practical examples of how it works and simply teaching. A name is not really that big of a deal.
But pandering to anti-intellectualism is extremely unfortunate. Irrational fear of new terminology is a major stumbling block to progress. There is a wealth of new things to discover that will require new modes of thinking, new concepts, and new terminology.
If devs wish to remain stagnant in their knowledge and their practice, that's fine. But in doing so, there's no reason why they should offer their opinions on matters they've chosen to ignore. I hope they'd have the intellectual humility to simply listen, rather than contribute noise to the discussion.
So a lot of people are going to see 'transducers.js' and think this is a library for interfacing I2S or 1-wire sensors to Node, or something.
That's not anti-intellectualism, it's pointing out that the word already has an established meaning among engineers, and trying to redefine it is going to mislead a lot of people about what this library is trying to do.
(defn sum [xs] (reduce + xs 0))
(or whatever) only reinforces the naive assumption that it makes the thing smaller. It was only after playing with dozens of problems on 4clojure (a great site), that I grokked how reduce can make the input much bigger, or anything you want.So in that respect, I think "transduce" better fits the concept of transformation, which is what this is all about.
see http://www.openfst.org and many others.
tacking on some random new etymology, 'transducer is just a combination of transform and reduce', is going to lead to all sorts of unnecessary confusion, made worse by the inherent similarities...