CoffeeScript: Fun with Comprehensions and Splats

I love CoffeeScript. Some think it’s an ersatz to pure Javascript. I think it make writing nice code easier. Here are two concepts that helped me today.


Comprehensions, is the way CoffeeScript makes for loops very expressive. For example, the following piece of code takes a persons array, filters it and maps it to its name attribute.

womens = ( for person in persons when person.gender is 'F')


With Splats I can pass an array as argument to a function that normally takes multiple arguments. For examle Math.max(). Here’s a code sample:

max = Math.max [1,2,3,4,5,6]...

Combine both features

Got an array of persons? Need to compute the max age? Write this:

maxAge = Math.max (person.age for person in persons)...

One thought on “CoffeeScript: Fun with Comprehensions and Splats”

  1. Maybe it’s more readable but not necessary to use parenthesis in comprehension (i.e. opened after equal and closed at the end)…

Comments are closed.