Reputation: 225
I confuse why the stringop3 is error. If I want to define value stringop3 has two params, one is a:String, the other is f:String=>String , what should I do.
// right
def stringop (a:String)(f:String=>String) = f(a)
// right
val stringop2=((a:String),(f:String=>String))=>f(a)
// error
val stringop3=(a:String)(f:String=>String)=>f(a)
the error is : error: not a legal formal paramete r.
Note: Tuples cannot be directly destructured in method or function parameters.
Either create a single parameter accepting the Tuple1,
or consider a pattern matching anonymous function: `{ case (param1, param1 ) => ... }
val stringop3=(a:String)(f:String=>String)=>f(a)
^
one error found
Upvotes: 0
Views: 101
Reputation: 15074
Curried function definitions (eg: fn(a: A)(b: B): C
) can be visualised as fn: A => B => C.
So, you can define your stringops3 like this:
scala> val stringops3 = (a:String) => (f: String => String) => f(a)
stringops3: String => ((String => String) => String) = <function1>
scala> stringops3("foo")(_.toUpperCase)
res1: String = FOO
Upvotes: 4