Skip to Content

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 = (person.name 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]...

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

maxAge = Math.max (person.age for person in persons)...
comments powered by Disqus