yonran yonran - 1 month ago 20
CoffeeScript Question

enumerate and zip within Coffeescript?

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 ...

Answer

forEach is effectively built in:

a = ['a','b','c']
for el,i in a
    alert "Element #{el} is at index #{i}"