scala - Is there any difference between this syntax of curried functions? -


another scala newbie question.

trying find difference between:

def _1_sumuntil(n: int) = (f: int => int) => (0 n).tolist.foldleft(0){(a,b) => + f(b)} 

and

def _2_sumuntil(n: int)(f: int => int) = (0 n).tolist.foldleft(0){(a,b) => + f(b)} 

what advantage of 1 on other (if @ all) ?

the first method 1 parameter list returns function int => int int, second method 2 parameter lists returns int.

technically, means of called eta-expansion—a method can transparently converted function value—, second method can partially applied, yielding same function first method:

val = _1_sumuntil(33)    // (int => int) => int val b = _2_sumuntil(33) _  // (int => int) => int   via eta-expansion 

my advise use second variant , avoid explicit function values. advantage of second that—unless do use eta-expansion—no function value instantiated (apart function passed foldleft) applied. arguably easier read.

i use first version if main purpose of method give function int => int int pass around.


see this question , this question.


Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -