10 CoffeeScript One Liners to Impress Your Friends
You may have read “10 Scala One Liners to Impress Your Friends” at Marcus Kazmierczak’s blog recently featured on HN. Although I don’t know Scala (or Java), it all looks quite nice, so I decided to impress my friends too - folks go from Java to Scala, we go from Javascript to CoffeeScript. Assume node.js as the environment for all examples.
-
Multiply each item in a list by 2
Marcus starts by showing off the map
function. We can do exactly the same using a range literal and an anonymous function:
but we can also write the more expressive form
-
Sum a list of numbers
Javascript (and CoffeeScript by extension) also has native map and reduce functions:
(reduce
== reduceLeft
, reduceRight
is also available)
-
Verify if word exists in string
Too easy since we have the some
method. It returns true if any of the elements in the array satisfies the function:
This will return the matched words instead:
~
is not a special operator in CoffeeScript, just a dirty trick. It is the bitwise NOT operator, which inverts the bits of it’s operand. In practice it equates to -x-1
. Here it works on the basis that we want to check for an index greater than -1
, and -(-1)-1 == 0
evaluates to false.
-
Read in a File
Users of client-side javascript frameworks will be familiar with this idea:
You could also use the synchronous version:
In node.js land this is only acceptable for application start-up routines. You should use the async version in your code.
-
Happy Birthday
First, a 1 to 1 mapping to the Scala version with a bit of string interpolation thrown in the mix:
But it get’s better. This reads almost like pseudo-code:
-
Filter list of numbers
Filter a list of numbers into two categories. The literate way:
(thanks @giacecco for shortening this)
And the functional way:
-
Fetch and Parse a XML web service
XML what? Haven’t heard of it. Let’s fetch a JSON instead, using the request library:
-
Find minimum (or maximum) in a List
The apply
function comes handy here. It allows you to call a function passing an array as the list of arguments: Math.max
and Math.min
both receive a variable number of arguments, i.e. Math.max 30, 10, 20
returns 30
. Let’s put it to work with an array:
-
Paralell Processing
Not there yet. You can create child processes on your own and communicate with them, or use the WebWorkers API implementation. Skipping over.
-
Sieve of Eratosthenes
update: @dionyziz sent me this compact version:
which we can then use for a real one-liner like the original:
or the somewhat more efficient
-
Bonus
Most readable fizzbuzz version you’ll ever see:
edit: even simpler, but trickier, with a little hint by satyr:
When you use the +
operator on an Array, it converts it to a string. [].toString()
is the same as [].join(',')
, which gives an empty string in case the array value is undefined
or null
. This also works in Javascript ([undefined] + "b" === "b"
).
Conclusions
Modern languages are amazingly expressive. I’m also surprised that some of the syntax in these map so closely to Scala, given they’re oceans apart.
You can learn more about CoffeeScript here, see a few more CoffeeScript snippets on rosettacode, and follow me on Twitter @ricardobeat.