def f(x: Int): Boolean = (x >= 0 && x < 4)
List(1, 3, 5).map(f) // List(true, true, false)
f // does not compile
Why can f
be used where a function value is expected, even if it is not a function value itself?
def f(x: Int): Boolean = (x >= 0 && x < 4)
List(1, 3, 5).map(f) // List(true, true, false)
f // does not compile
Why can f
be used where a function value is expected, even if it is not a function value itself?
Copyright © 2021 Jogjafile Inc.
What is happening?
In places where a function type is expected,
f
is converted to an anonymous function(x: Int) => f(x)
.Why is
f
not a function value in the first place?f
was not defined. Acurried(parameterless) function value would work:Why is
f
accepted as a function value?map
expects a function type and becausecurried and uncurried versions off
f
andg
both do the same, the automatic conversion makes sense.map(f)
has a cleaner look thanmap(f(_))
.