scala - Invoking curried function -


below implementation of curried function :

scala> def multiply(x: int, y: int) = x * y multiply: (x: int, y: int)int  scala> def multiplycurried = (multiply _).curried multiplycurried: int => (int => int) 

when attempt implement multiplycurried receive exception :

<console>:10: error: missing parameter type               multiplycurried(a => b => * b) 

what correct implementation invoke multiplycurried ?

from wikipedia: currying technique of translating evaluation of function takes multiple arguments (or tuple of arguments) evaluating sequence of functions, each single argument (partial application)

def multiply(x: int, y: int) = x * y          //> multiply: (x: int, y: int)int  def multiplycurried = (multiply _).curried    //> multiplycurried: => int => (int => int)  def multiplycurried2(x: int)(y: int) = x * y  //> multiplycurried2: (x: int)(y: int)int  def multiplycurried3(x:int) = (y:int) => x * y //> multiplycurried3: (x: int)int => int  def multiplycurried4 = multiplycurried2 _ //> multiplycurried4: => int => (int => int)  multiply(1, 2)                                //> res1: int = 2 multiplycurried(1)(2)                         //> res2: int = 2 multiplycurried2(1)(2)                        //> res3: int = 2 multiplycurried3(1)(2)                        //> res4: int = 2 

multiplycurried2 syntactic sugar multiplycurried3.

as victor correctly points out in comments, multiplycurried , multiplycurried2 not same. implementation of multiplycurried4 contains recipe turn multiplycurried2 function equal multiplycurried.

what's nice currying partial application:

val m = multiplycurried(1)                    //> m  : int => int = <function1> m(2)                                           //> res5: int = 2 val m2 = multiplycurried2(1)_                 //> m  : int => int = <function1> m2(2)                                       //> res6: int = 2 

Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -