Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass a function as a first argument in a function call in coffeescript

Tags:

coffeescript

In the following code

x= (f,n) -> f(n)
...
x( (n) -> n+1 , 5) #parse error

How can I fix the parse error above ?

Thanks

like image 636
Ali Salehi Avatar asked Jul 05 '11 05:07

Ali Salehi


People also ask

How do you pass a function as an argument?

We cannot pass the function as an argument to another function. But we can pass the reference of a function as a parameter by using a function pointer. This process is known as call by reference as the function parameter is passed as a pointer that holds the address of arguments.

How do you call a function in CoffeeScript?

You can simply invoke a function by placing parenthesis after its name as shown in the following example. // Generated by CoffeeScript 1.10. 0 (function() { var add; add = function() { var a, b, c; a = 20; b = 30; c = a + b; return console.

Can we pass function as a parameter in js?

Passing a function as an argument to the function is quite similar to the passing variable as an argument to the function. so variables can be returned from a function. The below examples describe passing a function as a parameter to another function.

Can you use a function as a parameter of another function?

Functions, like any other object, can be passed as an argument to another function.


1 Answers

A pair of parenthesis would fix this problem, just found the answer on IRC.

x( (n) -> n+1  , 5) #parse error
x ((n) -> n+1) , 5 #fixed
like image 56
Ali Salehi Avatar answered Oct 30 '22 02:10

Ali Salehi