TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Stupid Languages

99 点作者 decklin超过 12 年前

10 条评论

politician超过 12 年前
In JavaScript, every function is varadic. The problem is one of contract, not language. Passing varadic functions to higher-order functions without checking whether the types are compatible will cause a problem in <i>every such language</i>.<p>The problem is parseInt, not map. Consider:<p><pre><code> // using lodash.js (http://lodash.com/) _.mixin({ args: function () { return _.toArray(arguments); } }) ['10','10','10','10'].map(_.compose(parseInt, _.first, _.args)); // #=&#62; [10,10,10,10]</code></pre>
评论 #5122838 未加载
评论 #5123525 未加载
评论 #5123568 未加载
chimeracoder超过 12 年前
&#62; But it can do things the other maps can't easily, like create an array of differences between elements, or a running total, and so on. Other languages have other solutions to those problems.<p>Lisp has some rather elegant solutions to both of those use cases. It's misleading to say that Javascript's 'map' is more powerful just because it tries to cram two or three distinct use cases into one.<p>It's not like Javascript even uses the Lisp definition of 'map' (which is <i>not</i> what most languages use for map - they use Lisp's 'mapcar').
评论 #5122678 未加载
Xion超过 12 年前
This example clearly violates the principle of least surprise, but it doesn't necessarily mean that the language it comes from is bad. Rather, the <i>code</i> it's used in is.
评论 #5122356 未加载
评论 #5122361 未加载
laureny超过 12 年前
This is more of a case of a stupid library than stupid language.<p>And whether it's a stupid library is even debatable. map() has gained a more popular understanding from functional programming these days but it was probably not so when this Javascript map() function was added.
评论 #5123582 未加载
jhuni超过 12 年前
One important characteristic of the map function is that it can be used reversibly. For example, (partial map inc) is inverted by (partial map dec). If I want to map over a collection by a function that takes the entire collection as an argument I'd rather use some function other then map to avoid confusion.
评论 #5124046 未加载
评论 #5124669 未加载
dkersten超过 12 年前
To me, the bigger issue is that the unused 3rd argument is silently ignored without error or warning.
评论 #5122546 未加载
评论 #5122735 未加载
评论 #5122934 未加载
gsg超过 12 年前
&#62; They want to know if function arguments are call by value or call by reference (neither).<p>My knowledge of Python is limited, but I was under the impression that it is call by value just like everything else (where Python's notion of value is "pointer to something"). Am I missing something?
评论 #5123463 未加载
评论 #5124380 未加载
ricardobeat超过 12 年前
I think one of the reasons for the third argument (the array itself) is to permit chaining. Consider this:<p><pre><code> var publishedPosts = posts.filter(function(post){ return post.published }) var intervals = publishedPosts.map(function(post, i){ var next = publishedPosts[i+1] return post.date - (next &#38;&#38; next.date || 0) }) </code></pre> With the array reference you don't need an intermediate var:<p><pre><code> var intervals = posts.filter(function(post){ return post.published }).map(function(post, i, arr){ var next = arr[i+1] return post.date - (next &#38;&#38; next.date || 0) })</code></pre>
评论 #5123581 未加载
rekwah超过 12 年前
For another quick 'WAT' talk, see Gary Bernhardt from CodeMash 2012.<p><a href="https://www.destroyallsoftware.com/talks/wat" rel="nofollow">https://www.destroyallsoftware.com/talks/wat</a>
xyproto超过 12 年前
Base 1 does make sense!<p>0 = 0<p>00 = 1<p>000 = 2<p>0000 = 3<p>etc
评论 #5122761 未加载
评论 #5122776 未加载
评论 #5122829 未加载