3 ms·
You have some nice functional stuff. You can define a function on a primitive type like an integer. This enables you to have code like 400.times(doX). doX is a
by jamra 12y ago
You have some nice functional stuff. You can define a function on a primitive type like an integer. This enables you to have code like 400.times(doX). doX is a function. Functions as first class citizens are nice.
If you have a function as a final argument, you can change this: something(a, b, { }) to something(a, b) { }. Basically, you just put your anonymous function on the outside of the parenthesis.
I think the book's example was something along the lines of sort(arr) { $1 > $2 }. I think that $1 is some kind of default specifier for the first param and $2 for the second, but I'm not sure.
- nextstep 12y agoYou're correct about the $1 syntax representing parameters, but the parameter indices start at 0: sort(arr) { $0 > $1 }