Can functional value accepts another functional value as argument?

for instance:

scala> def mytest(x:String, f:String=>Int):Int = f(x)
def mytest(x: String, f: String => Int): Int

f is a functional value I know. It accepts String as its argument for now.
Can f accepts another functional value as its argument? if so how to write?
(I tried to write it which doesn’t work).

Thank you.

def foo[A, B, C](ff: (A => B) => C)

?

Note, while this is valid is kind of weird.
The only valid use case I can think of for this kind of signature is a callback.

1 Like

When you say “I tried to write it which doesn’t work”, please show us what you wrote and what error message or other unexpected behavior you got.

2 Likes

Is this the correct syntax for the purpose?

scala> def mytest(x:String,foo:String=>Int=>Int):Unit = {}
def mytest(x: String, foo: String => (Int => Int)): Unit

Thank you

That gives you a function that returns a function instead of a function that accepts a function, look how the parenthesis were associated.

1 Like

It’s pretty sure. The function is the first class value, so it can be used where a value can used. The following is a sample to encode the succ function of Church’s Numerals:

val succ1 = (n: (Int=>Int)=>Int=>Int)=>(s:Int=>Int)=>(z:Int)=>s(n(s)(z))

def succ2(n: (Int=>Int)=>Int=>Int)(s:Int=>Int)(z:Int): Int = s(n(s)(z))