Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiline function calls in Coffeescript

Hi everyone: Suppose I have a function "foo" that should receive two functions as parameters. If I have two lambda functions, I can call "foo" as follows:

foo (-> 1),(-> 2)

In this case, "foo" receives two functions, one that just returns 1 and another that returns 2.

However, usually lambda functions are more complicated, so putting both functions on a single line is impractical. Instead, I would like to write two multiline lambda functions. However, I can't figure out for the life of me how to accomplish this in coffeescript- Ideally, I would want to write it as follows, but it throws an error:

foo
    ->
        1
    ,
    ->
        2

The best I can come up with that works is super ugly:

foo.apply [
                ->
                        1
        ,
                ->
                        2
        ]

Can any Coffeescript guru show me how I can do this, without getting an error? Thanks!

like image 761
Conrad Barski Avatar asked Sep 23 '11 17:09

Conrad Barski


People also ask

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. log("Sum of the two numbers is: " + c); }; add(); }).

Should I use CoffeeScript?

CoffeeScript is something that makes even good JavaScript code better. CoffeeScript compiled code can do everything that natively written JavaScript code can, only the code produced by using CoffeeScript is way shorter, and much easier to read.

What can you do with CoffeeScript?

CoffeeScript is a programming language that compiles to JavaScript. It adds syntactic sugar inspired by Ruby, Python, and Haskell in an effort to enhance JavaScript's brevity and readability. Specific additional features include list comprehension and destructuring assignment.

How do I use CoffeeScript in HTML?

You simple need to add a <script type="text/coffeescript" src="app. coffee"></script> to execute coffee script code in an HTML file. In other cases, I've seen people use the attributes of type="coffeescript" and type="coffee" , so they might work for you as well. Save this answer.


3 Answers

I believe this is one situation where anonymous functions seem to not be the answer. They are very practical and idiomatic in a lot of situations but even they have limitations and can be less readable if used in extreme situations.

I would define the two functions in variables and then use them as parameters:

func1 = ->
    x = 2
    y = 3
    z = x+y
    return z+2*y

func2 = ->
    a = "ok"
    return a + " if you want this way"

foo func1, func2

But if you decide lambdas would be preferable, just use the parenthesis around the parameters of foo:

foo ((->
    x = 2
    y = 3
    z = x+y
    return z+2*y
  ),(->
    a = "ok"
    return a + " if you want this way"
  )
)

It is not because you are using CoffeScript that you should avoid parenthesis at any cost :)

like image 136
brandizzi Avatar answered Nov 15 '22 09:11

brandizzi


This should suffice (you could indent the second lamda if you want):

f (-> 
    x = 1
    1 + 2 * x),
-> 
    y = 2
    2 * y

given the function f:

f = (a,b) -> a() + b()

the result should give 3 + 4 = 7

like image 39
Rickard Avatar answered Nov 15 '22 10:11

Rickard


Functions are implicitly called if a variable or function follows them. That's why

foo
  ->
    2
  ,
  ->
    3

won't work; the coffeescript compiler only sees a variable followed by an unexpected indent on the next line. Explicitly calling it

foo(
  ->
    2
, ->
    3
)

will work.

You can implicitly call a function with multiple paramenters, you just need to line up the comma with the beginning of the function call

foo ->
  2
, ->
  3
like image 29
Cezary Wojtkowski Avatar answered Nov 15 '22 09:11

Cezary Wojtkowski