Is it possible to create a function as an output to a function in Scala?

Good afternoon,

In Erlang it is possible to write code like :

mon_id() →

fun(_Env) →
?DEBUG(“mon_id no match.”)
end.

Is it possible to do something similar in Scala please, i.e. make a function return an in built function( Function that had been composed in the original function) ?

Thanks a lot and good day :slight_smile:

Sure:

Welcome to Scala 2.13.4 (OpenJDK 64-Bit Server VM, Java 11.0.9.1).
Type in expressions for evaluation. Or try :help.

> val f: Int => Int => Int = x => y => x+y
val f: Int => (Int => Int) = $Lambda$1076/0x00000008405ee040@288f173f

> f(1)
val res0: Int => Int = $Lambda$1088/0x00000008405ff040@d499c13

> f(1)(2)
val res1: Int = 3
1 Like

Thanks for your help.

And how would this be written in a Scala IDE ?

Similarly I would presume right, i.e.
def f: Int => Int => Int = x => y => x+y

Thanks a lot and good day :slight_smile:

Written the same.

You said you wanted a function that returns a function. If instead you want a method that returns a function, you would do:

> def f(x: Int): Int => Int = y => x+y
def f(x: Int): Int => Int
2 Likes

Sorry, I meant a method. Error from my end.

Thanks a lot for your understanding and help :slight_smile: