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.

Lodash's Chain vs. Native Methods

38 pointsby MrHusover 3 years ago

4 comments

tshaddoxover 3 years ago
That&#x27;s a neat little implementation. For bonus points, try adding these features:<p>1. More non-commutative operations, like Ruby&#x27;s each_cons, each_slice, chunk (different than lodash&#x27;s chunk). I especially enjoy implementing these both iteratively and recursively.<p>2. Infinite sequences (like Python&#x27;s itertools.count). You may find that implementing the other operations becomes easier when you treat the sequences themselves as being lazy and potentially infinite. Do you still need to make the distinction between commutative and non-commutative operations?<p>3. Operations that combine multiple sequences, like iterools.zip, itertools.product, and merge (assuming sorted sequences).<p>4. Operations that <i>return</i> multiple sequences (say, a sequence of sequences). Can you implement the inverse of itertools.zip (perhaps call it unzip)?
评论 #28323090 未加载
评论 #28322874 未加载
darepublicover 3 years ago
In the article when comparing the native vs lodash implementation the native one is given as:<p>persons .map(p =&gt; ({ ...p, age: p.age + 1 })) .filter(p =&gt; p.age &gt;= 30) .slice(0, 5);<p>^ But this is intentionally(?) unoptimized. You would instead put the filter and slice first, i.e.<p>person.filter(p =&gt; p.age &gt;= 29) .slice(0,5) .map(p =&gt; ({...p, age: p.age + 1})
评论 #28323552 未加载
评论 #28323416 未加载
评论 #28323460 未加载
评论 #28323455 未加载
tengbretsonover 3 years ago
Comparisons of lodash to native methods are interesting but they always miss the real selling points of lodash in my opinion, which are the richer data structure manipulations like groupBy, partition, intersection, difference, union, etc.
评论 #28325061 未加载
eyelidlessnessover 3 years ago
Good article detailing a particularly high-value part of Lodash.<p>I tend to be highly critical of the library overall:<p>- It takes most of its inspiration from functional programming, but sometimes surprises with unexpected mutation.<p>- Surprises of that kind are especially likely to be missed, because the API is quite large, and because (as the article author mentions) it provides a lot of shorthand functionality&#x2F;magic behavior that one might not expect… especially combined.<p>- FP patterns like composition and currying are unnecessarily ergonomically difficult, because argument order is wrong throughout most of the API; the lodash&#x2F;fp module mostly addresses this, but it’s not in wide use by a lot of projects heavily dependent on Lodash.<p>- Where composition is accounted for, it tends to exacerbate surprises caused by magic behavior.<p>All of that said, the laziness of chain is one bright spot that (afaik) is pretty faithful to FP expectations, notwithstanding those other issues. And while I’m not personally a fan of “fluent” APIs and chaining syntax, with laziness it’s not hard to squint and see it as roughly analogous to a threading macro&#x2F;pipe operator.<p>IMO there are better-overall FP libraries in the ecosystem, but they tend to feel very unidiomatic. E.g. dropping in Haskell-like semantics and names that are likely unfamiliar to JS&#x2F;TS devs who would nonetheless be familiar with similar equivalents in the ecosystem.<p>These days I mostly treat Lodash as YAGNI, and very seldom install or import it unless there’s a strong team preference. But I think, given its prevalence, I’d like to see a fresh “Lodash done right” which:<p>- Exports functions rather than a namespace (ESM and tree-shaking friendly, encourages use and composition of plain functions)<p>- Adopts either the native-equivalent name (if available) or the familiar Lodash name for functions (with convenience aliases if desired, but ideally isolated in some compat module)<p>- Places arguments in an order one would expect coming from the equivalent implementation in a FP language (collection first, operator&#x2F;parameter after) to encourage composition<p>- Eliminates all implicit magic (missing arguments are treated as a mistake; null is a value)<p>- Provides&#x2F;expects lightweight data types for explicit magic (eg `path(&#x27;foo.bar&#x27;)` is better and safer than &#x27;foo.bar&#x27;)<p>- Embraces evolution of the language&#x2F;platform, deferring to native implementations as they’re available&#x2F;introduced<p>- Engaged with the standardization process to coordinate and advocate for the principles and benefits of FP functionality<p>… and all of that’s a lot to ask. Certainly beyond my bandwidth alone. But I’d be happy to collaborate if other folks want to see it happen :)
评论 #28323232 未加载