Impressive amount of code and words. I think I'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 > 0) {
sum += item.popularity;
count += 1;
}
}
return sum / count;
}</code></pre>
Every time I see this blog I'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'm fully able to switch to reading mode in Firefox to mitigate this problem, but frankly it's off-putting.
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'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't support them yet?:<p><a href="https://dev.to/nestedsoftware/lazy-evaluation-in-javascript-with-generators-map-filter-and-reduce--36h5" rel="nofollow">https://dev.to/nestedsoftware/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
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://srfi.schemers.org/srfi-171/srfi-171.html" rel="nofollow">https://srfi.schemers.org/srfi-171/srfi-171.html</a>
In almost all cases you don'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.
It'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's Clojure's explanation of transducers: <a href="https://clojure.org/reference/transducers" rel="nofollow">https://clojure.org/reference/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.
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'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 ...
<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.
I recommend ignoring transducers unless you know you really need push-based iteration, which probably isn't the case. Generators are generally a superior approach otherwise.
I know this is cheating and missing the point, but using d3-array:<p>d3.mean(victorianSlang, d => d.popularity)<p>This implicitly ignores invalid values (null, NaN or undefined) after coercing to a number.
Related: Transducers for Python and related HN discussion <a href="https://news.ycombinator.com/item?id=10591118" rel="nofollow">https://news.ycombinator.com/item?id=10591118</a><p>I don't know how JS generators work but Python users seemed to think Python's generators cover most transducer use cases.