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.

Haskell in ES6: Part 1

85 pointsby megalodonover 9 years ago

10 comments

pdpiover 9 years ago
I&#x27;m not quite sure what the end-game is, here?<p>Several of the functions listed here are quite different from their Haskell counterparts in ways that make them subtly less useful — and in ways that make them not have the same types as the Haskell originals, which is interesting considering we&#x27;re barely given more than the type of each of the functions. flip and zipWith are the ones that were quite obvious to me, others might have the same problems (with flip being outright wrong due to it inverting all the arguments instead of flipping the first two, and zipWith working fine with two lists, but generalising to multiple lists in a way that can&#x27;t be a generalisation of the two-list zipWith). Composition also works in reverse to what one would expect in Haskell, for no reason I can fathom.
评论 #10565550 未加载
评论 #10565304 未加载
评论 #10565292 未加载
infinity0over 9 years ago
The biggest power of Haskell (and other functional programming languages) is in the type system and the ability to abstract well (e.g. equals()), and these functional functions are secondary. How do you suppose to replicate that in JS?
评论 #10566499 未加载
评论 #10566438 未加载
评论 #10565402 未加载
评论 #10566465 未加载
icholyover 9 years ago
Haskell&#x27;s strengths are it&#x27;s laziness, currying, and type stystem. Implementing a few higher order functions != haskell. This is HN clickbait.
评论 #10567001 未加载
olalondeover 9 years ago
Pretty cool. Few suggestions (which may or may not require babel):<p>- `f.apply(null, args)` calls can be replaced with `f(...args)`<p>- Your head function returns the first argument while your last function returns an <i>array containing</i> the last argument. I&#x27;d rewrite those functions as:<p><pre><code> const head = (x) =&gt; x; const last = (...xs) =&gt; xs[xs.length-1]; const tail = (x, ...xs) =&gt; xs; const init = (...xs) =&gt; xs.slice(0, -1); </code></pre> (Unfortunately it seems the rest operator can only appear as the last argument.)<p>- Why put everything on the prototype? It doesn&#x27;t seem necessary since you are not using the `this` keyword. I would just stick the `export` keyword in front of functions to be exported.<p>Looking forward to part 2.
评论 #10565441 未加载
评论 #10565935 未加载
sjclemmyover 9 years ago
I found the way the examples are written confusing at first glance. I think the norm is to call the function and then show the returned output.<p>e.g. instead of<p>1&#x2F;4 === comp(1)<p>do<p>comp(1)<p>&#x2F;&#x2F; returns 1&#x2F;4<p>This might be my personal preference but it&#x27;s the way it&#x27;s written in the Mozilla javascript reference pages.<p>Other than that it looks great!
kmonadover 9 years ago
May I ask what the (ideal) outcome of this would be? I&#x27;m not sure this is clear to me and I couldn&#x27;t find more info on your blog.
nvartolomeiover 9 years ago
Why `Array.prototype.slice.call(xs, 1)` for tail operation?
评论 #10565202 未加载
评论 #10565165 未加载
bricssover 9 years ago
Please, keep bloging
ameliusover 9 years ago
But how to implement laziness?
评论 #10566783 未加载
评论 #10565235 未加载
mbrockover 9 years ago
Here&#x27;s another way of phrasing that.<p>&quot;Functional programming isn&#x27;t an end unto itself, it&#x27;s one of several strategies for breaking programs down into manageable chunks. I would have found the article more interesting if it showed how this type of tiny function is used in journeyman Haskell code, or how the translation to ES6 would apply to real world JavaScript.&quot;<p><a href="http:&#x2F;&#x2F;blog.ycombinator.com&#x2F;new-hacker-news-guideline" rel="nofollow">http:&#x2F;&#x2F;blog.ycombinator.com&#x2F;new-hacker-news-guideline</a>
评论 #10566995 未加载
评论 #10566918 未加载
评论 #10565654 未加载