The Coffeescript Keyword Nobody's Heard Of: 'by'

Coffeescript is an amazing language. As a matter of fact, it’s one of my favorite languages, second only to JavaScript. Here’s some recent code I used to split up one array into a multidimensional array with a certain number of columns.

1
matrix.push arr[i..(i + cols - 1)] for i in [0..arr.length - 1] by cols

Notice the second to last keyword: by.

The by keyword basically changes the increment of the generated for loop. Without the by, you’d have i++ as the increment of your for loop. However, with the by, you’d instead have i += 3 if you had by 3.

Here’s a simple example where I am getting money out of my bank account in the form of 5 dollar bills.

1
console.log "I have #{i} dollars." for i in [0..100] by 5

This translates to the following Javascript:

1
2
3
4
var i;
for (i = 0; i <= 100; i += 5) {
console.log("I have " + i + " dollars.");
}

As you can see, the increment of the loop is now 5 rather than just 1.

Now the question is, are there any other hidden features of Coffeescript like this?


Thanks for reading my post! If you enjoyed it or it helped you, please consider liking/tweeting this page, commenting, or following me on GitHub or Twitter!