I am new in CoffeeScript
(I started learning this morning) and the only thing that I am not able to find is an elegant way to split an array in subarrays. For example, I have the following array:
myarr = [1, 7, 8, 3, 5, 1, 4, 9, 0, 2]
And I want to split it in subarrays of two elements each one:
myarr = [[1, 7], [8, 3], [5, 1], [4, 9], [0, 2]]
I know how to do it with pure javascript
however, and given I am learning, I was not able to find an elegant-coffeescript way. Does CoffeeScript
allow a nice solution for this?
Update:
using the http://js2coffee.org/ site to translate my javascript code to CoffeScript I was able to find this solution:
Array::chunk = (chunkSize) ->
array = this
[].concat.apply [], @map((elem, i) ->
(if i % chunkSize then [] else [array.slice(i, i + chunkSize)])
)
It looks quite right. But that is a translator output and not a expert recommendation.
Disclaimer:
Even when CoffeeScript compiles to Javascript, I am looking for a CoffeeScript solution and not a Javascript solution. The idea that "if you know javascript then, you know coffeescript" is not true for me, at least not yet.