在 Coffeescript 中枚举和压缩?
来自 Python,我喜欢 Coffeescript 从 Python 和 Perl 借用的许多功能(范围/切片、推导式、解构赋值)。 Coffeescript 中是否有任何语法糖可以模仿 Python 的 enumerate
或 zip
(itertools.izip
) 函数?
以下是我不太关心的模式:
# an enumerate call would be helpful here
i = 0
for x in arr
... use x and i ...
i++
和
# a zip would be useful here
n = Math.min(arr1.length,arr2.length)
for i in 0...n
x = arr1[i]; y = arr2[i]
... use x and y ...
Coming from Python, I like many of the features that Coffeescript borrows from Python and Perl (ranges/slices, comprehensions, destructuring assignments). Is there any syntactic sugar in Coffeescript to mimic Python's enumerate
or zip
(itertools.izip
) functions?
Here are the patterns that I don't care much for:
# an enumerate call would be helpful here
i = 0
for x in arr
... use x and i ...
i++
and
# a zip would be useful here
n = Math.min(arr1.length,arr2.length)
for i in 0...n
x = arr1[i]; y = arr2[i]
... use x and y ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
forEach 有效地内置于:
forEach is effectively built in:
枚举:
zip
/zipWith
(我从 Haskell 学到了这些;我认为它们在 Python 中的意思是相同的?):一些示例(经过测试):
更新:重新实现 Haskell 风格,只是为了好玩。如果没有模式匹配,就不那么酷了,但是哦,好吧..
哦,伙计,这很有趣。所以需要更多这样的问题:D
Gist for
zip
和zipWith
Enumerate:
zip
/zipWith
(I learned these from Haskell; I assume they mean the same thing in Python?):Some examples (tested):
Update: Reimplemented Haskell-style, just for fun. Not as cool without the pattern matching, but oh well..
Oh, man, this was fun. SO needs more questions like this :D
Gist for
zip
andzipWith
对于压缩和其他此类实用函数,Underscore.js 几乎是标准库 - 而且它恰好是由 Jeremy 创建的Ashkenas,CoffeeScript 背后的人。有了它,您可以
编写您的 zip 示例,或者更好
使用模式匹配 。但请注意,
_.zip
使用arr1
和arr2
的最大长度,而不是最小长度;因此,如果您不想处理未定义的值,则应首先截断较长的数组。还有一个 Underscore 的 CoffeeScript 实现,Underscore.coffee,这是一个很好的地方如果您想知道如何在 CoffeeScript 中实现特定循环,请查看。
For zipping and other such utility functions, Underscore.js is pretty much the standard library—and it happens to have been created by Jeremy Ashkenas, the man behind CoffeeScript. With it, you could write your zip example as
or better yet
using pattern-matching. Note, however, that
_.zip
uses the max length ofarr1
andarr2
, not the min; so if you don't want to handleundefined
values, you should truncate the longer array first.There's also a CoffeeScript implementation of Underscore, Underscore.coffee, which is a great place to look if you're wondering how to implement a particular loop in CoffeeScript.
CoffeeScript Cookbook 列出了 zip 函数的一个很好的实现:
http://coffeescriptcookbook.com/chapters/arrays/zip-function
The CoffeeScript Cookbook lists a nice implementation of a zip function:
http://coffeescriptcookbook.com/chapters/arrays/zip-function
不要忘记 CoffeeScript 只是 ECMAScript 的替代语法。至少对于您的第一个示例,有一个非常好的 ECMAscript 函数(
Array.prototype。 forEach
),它已经完成了您想要的操作:不幸的是,没有
Array.prototype.zip
或Array.prototype.zipWith
。这似乎是一个相当大的遗漏,特别是考虑到同时存在reduce
和reduceRight
,后者是许多其他语言所没有的。我的猜测是,这是一个简单的疏忽,我们将在该语言的某些未来版本中看到zip
。Don't forget that CoffeeScript is just an alternate syntax for ECMAScript. At least for your first example, there is a perfectly good ECMAscript function (
Array.prototype.forEach
), which already does what you want:Unfortunately, there is no
Array.prototype.zip
orArray.prototype.zipWith
. That seems to be a pretty big omission, especially considering that there is bothreduce
andreduceRight
, the latter of which many other languages don't have. My guess is that it is a simple oversight, and we are going to seezip
in some future version of the language.对于 zip,请尝试以下方法:
如果您希望一直压缩到最后,请使用 Math.max()。
For zip, try this one:
If you prefer to zip all the way to the end, use
Math.max()
.