In Scala, instead of passing a Function<String, Integer>
, we pass
String => Integer
and instead of function.call(a)
we write
function(a)
.
scala> def reverseParse(s: String, f: String => Int) = f(s.reverse) reverseParse: (String,(String) => Int)Int scala> val c = reverseParse("5632", Integer.parseInt(_)) c: Int = 2365
Since functions are first-class we can do other clever things with them, like compose them.
scala> def compose[A, B, C](f: B => C, g: A => B): A => C = (a: A) => f(g(a)) compose: [A,B,C]((B) => C,(A) => B)(A) => C