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.

Magical, Mystical JavaScript Transducers

77 pointsby ecbualmost 6 years ago

13 comments

donutalmost 6 years ago
Impressive amount of code and words. I think I&#x27;d think twice before approving this in a code review, though.<p>Why is this not the obvious solution to the stated problem?<p><pre><code> function avgPopularity(slang) { let sum = 0; let count = 0; for (item of slang) { if (item.popularity &gt; 0) { sum += item.popularity; count += 1; } } return sum &#x2F; count; }</code></pre>
评论 #20311288 未加载
评论 #20311830 未加载
评论 #20310957 未加载
评论 #20311268 未加载
评论 #20311265 未加载
评论 #20311119 未加载
评论 #20311784 未加载
评论 #20312444 未加载
评论 #20310849 未加载
评论 #20310873 未加载
chunkstuntmanalmost 6 years ago
Every time I see this blog I&#x27;m extremely interested in the content, but the stylized presentation is so jarring and tough to read. I doubt this is an original qualm and I&#x27;m fully able to switch to reading mode in Firefox to mitigate this problem, but frankly it&#x27;s off-putting.
评论 #20311595 未加载
评论 #20312156 未加载
SigmundAalmost 6 years ago
I am somewhat confused here, I primarily do C# so I am used to IEnumerable and Linq for doing this kind of stuff and you would just do .Where().Select().Average() without worrying about intermediate memory usage. In C# you do this stuff with millions of results from a database (although you would do in the db if you can, but might be a flat file too).<p>Linq with IEnumerable doesn&#x27;t necessarily create intermediate list (js arrays) in memory unless it has to like say .OrderBy()<p>So I basically assumed js was the same since it has generators but its seems map and filter etc don&#x27;t support them yet?:<p><a href="https:&#x2F;&#x2F;dev.to&#x2F;nestedsoftware&#x2F;lazy-evaluation-in-javascript-with-generators-map-filter-and-reduce--36h5" rel="nofollow">https:&#x2F;&#x2F;dev.to&#x2F;nestedsoftware&#x2F;lazy-evaluation-in-javascript-...</a><p>So then you get a solution like this that looks to me very confusing vs a fluent style.<p>Please tell me I am missing something
bjolialmost 6 years ago
I recently wrote a srfi (scheme request for implementation) for transducers, if anyone is interested. The code is guile-specific, but is easily portable to other schemes. It is still a draft, so it is bound to change: <a href="https:&#x2F;&#x2F;srfi.schemers.org&#x2F;srfi-171&#x2F;srfi-171.html" rel="nofollow">https:&#x2F;&#x2F;srfi.schemers.org&#x2F;srfi-171&#x2F;srfi-171.html</a>
Waterluvianalmost 6 years ago
In almost all cases you don&#x27;t need to worry this much about performance. Just traverse the array three times and move on. Go measure and improve if it becomes an issue.
bluntealmost 6 years ago
It&#x27;s so depressing that JavaScript is growing in use, especially when I see articles like TFA. I hate that momentum has allowed a clusterf*ck of a language to become the standard for front end and now one of the top 3 for back-end.<p>Here&#x27;s Clojure&#x27;s explanation of transducers: <a href="https:&#x2F;&#x2F;clojure.org&#x2F;reference&#x2F;transducers" rel="nofollow">https:&#x2F;&#x2F;clojure.org&#x2F;reference&#x2F;transducers</a><p>The whole concept is explained more clearly and with far fewer words and code; and the final result is pretty, or at least clean and tidy.
评论 #20312557 未加载
评论 #20312786 未加载
talkingtabalmost 6 years ago
The concept of transducers - I think of it is a conveyor belt of functionality - is great and I periodically run into problems where having an easy to use JavaScript transducer system would be great. Right now I&#x27;m processing sets of markdown files, reading shortcodes from them, generating html, applying more shortcodes, adding variables and then using mustache ... It is just a conveyor belt, but with lots of little odds and ends - perfect for a transducer, especially with async.<p>Ramada looks interesting ...
sametmaxalmost 6 years ago
Isn&#x27;t it exactly the use case of generators ? I though js had the yield keyword by now.
tomxoralmost 6 years ago
<p><pre><code> function isFound(item) { return item.found; }; </code></pre> Every time I see this pattern in code (one line accessors) I have a sinking feeling in my stomach in expectation of whats to come.<p>This is the best example of a forced pattern.
Veedracalmost 6 years ago
I recommend ignoring transducers unless you know you really need push-based iteration, which probably isn&#x27;t the case. Generators are generally a superior approach otherwise.
mbostockalmost 6 years ago
I know this is cheating and missing the point, but using d3-array:<p>d3.mean(victorianSlang, d =&gt; d.popularity)<p>This implicitly ignores invalid values (null, NaN or undefined) after coercing to a number.
fulafelalmost 6 years ago
Related: Transducers for Python and related HN discussion <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=10591118" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=10591118</a><p>I don&#x27;t know how JS generators work but Python users seemed to think Python&#x27;s generators cover most transducer use cases.
mtarnovanalmost 6 years ago
Correct me if i&#x27;m wrong but if you use something like Elixir&#x27;s Stream you get composability for free and don&#x27;t need transducers.