javascriptlivescript

passing anonymous functions as parameters in livescript


What's the correct way to pass a function as a parameter in liveScript?

For instance, lets say I Want to use the array reduce function, in convectional javascript I would write it as the following

myArray.reduce(function (a,b) {return a + b});

This translates to liveScript quite nicely as:

myArray.reduce (a,b) -> a + b

Now, I want to set the initial value by providing a second parameter:

myArray.reduce(function (a,b) {return a + b},5);

How would I translate this to liveScript? It seems that the first function overrides any ability to pass additional parameters to reduce.

Apologies if I have missed something obvious, but I can't seem to find anything pertaining to this scenario in the docs


Solution

  • You have to wrap the closure in ()

    [1,2,3].reduce ((a,b) -> a + b), 0
    

    Compiles to

    [1, 2, 3].reduce(function(a, b){
      return a + b;
    }, 0);