TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Transducers for JavaScript

79 pointsby dahjelleover 10 years ago

8 comments

gcantiover 10 years ago
When I see concepts like transducers I&#x27;m always torn in two:<p><i></i>my theoretical side<i></i> is really excited: reducing (pun intended) a bunch of different operations to a minimal set of basic, composable operations is an awesome intellectual challenge. I imagine how succinct, reusable and multipurpose could become my code.<p><i></i>my pragmatic side<i></i> is skeptic: in functional programming it&#x27;s a little step to end up with incomprehensible code, for me and my coworkers:<p><pre><code> var inc = function(n) { return n + 1; }; var isEven = function(n) { return n % 2 == 0; }; var xf = comp(map(inc), filter(isEven)); console.log(into([], xf, [0,1,2,3,4])); &#x2F;&#x2F; [2,4] </code></pre> vs<p><pre><code> var inc = function(n) { return n + 1; }; var isEven = function(n) { return n % 2 == 0; }; [0,1,2,3,4].map(inc).filter(isEven); &#x2F;&#x2F; [2,4] </code></pre> The latter is comprehensible by everyone (and no additional library). Ok you iterate twice but if you really (REALLY) have performance problems, maybe you&#x27;d end up with something like this:<p><pre><code> var input = [0,1,2,3,4]; var output = []; var x; for (var i = 0, len = input.length ; i &lt; len ; i++ ) { x = input[i] + 1; &#x2F;&#x2F; map if (x % 2 == 0) ouput.push(x); &#x2F;&#x2F; filter }</code></pre>
评论 #8442545 未加载
评论 #8441630 未加载
评论 #8442086 未加载
warbleover 10 years ago
From: <a href="http://phuu.net/2014/08/31/csp-and-transducers.html" rel="nofollow">http:&#x2F;&#x2F;phuu.net&#x2F;2014&#x2F;08&#x2F;31&#x2F;csp-and-transducers.html</a><p>&quot;To me, transducers are a generic and composable way to operate on a collection of values, producing a new value or new collection of new values. The word &#x27;transducer&#x27; itself can be split into two parts that reflect this definition: &#x27;transform&#x27; — to produce some value from another — and &#x27;reducer&#x27; — to combine the values of a data structure to produce a new one.&quot;
评论 #8439796 未加载
评论 #8439773 未加载
jlongsterover 10 years ago
It&#x27;ll be interesting to look into how the Clojure people implemented this. I released the same library a few weeks ago: <a href="https://github.com/jlongster/transducers.js" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;jlongster&#x2F;transducers.js</a>, and I&#x27;m about to release the next version with an API almost the same as the OP. I think mine has more integration with JS data structures and stuff like monkeypatching existing ones (like immutable-js) to work with this. We&#x27;ll see how all this shakes out though; not sure if it&#x27;ll be good to converge on one official library or if it&#x27;s ok to have multiple.
评论 #8480661 未加载
评论 #8440595 未加载
aikahover 10 years ago
So I checked the definition of a transducer,but i&#x27;m not smart enough to get it. What the difference between that library and say lodash where I can bind and compose functions? because it seems like the same stuff<p><pre><code> var f=_.compose( _.partialRight(_.filter,isEven),_.partialRight(_.map,inc)) ; </code></pre> or something.
评论 #8441261 未加载
评论 #8439778 未加载
评论 #8439927 未加载
评论 #8441266 未加载
zoomerangover 10 years ago
As somebody with more experience with Haskell and Scala than Clojure - how to Transducers differ from Functors in Haskell?<p>On the surface they seem very similar.
tripzilchover 10 years ago
Friendly advice: maybe also include the output of the example code in the README.
coding4allover 10 years ago
Notes and examples of Transducers and Reducers <a href="https://gist.github.com/runexec/06b56a9dbd15e43145b9" rel="nofollow">https:&#x2F;&#x2F;gist.github.com&#x2F;runexec&#x2F;06b56a9dbd15e43145b9</a>
d--bover 10 years ago
Why not wait for ES6, and use proper iterators and use them as they do in C#?<p>as in var evens = [1,2,3,4].where(x =&gt; x%2 == 0).toarray()<p>no ?