This code
nums = [1..10].map (i) -> i*2
Runs
Whereas this
nums = [1..10].map(i) -> i*2
is broken
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The reason for this is that parentheses for a function call (invocation) are optional. I find this a constant confusion in my own code and have a general policy of always including parentheses to make it clear.
In coffee script, if you leave out the parentheses it assumes that the argument list goes to the end of the line. Your first example coffee script is actually the same as this:
where the first argument of the call to map is a function
(i)->i*2If you remove the space between the map and the (i) then coffee script implies parentheses around the rest of the line. Your second example coffee script is actually the same as this:
Here you can see that map is being called with
ias the only argument and then coffee script is expecting themap(i)call to return a function which is then being called passing->i*2or to be more explicit()->i*2as an argument.Given that coffee script is designed to remove the potential coding hazards of javascript, I think that it would have been much safer if they had not included this implied parenthesis.