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,
fis converted to an anonymous function(x: Int) => f(x).Why is
fnot a function value in the first place?fwas not defined. Acurried(parameterless) function value would work:Why is
faccepted as a function value?mapexpects a function type and becausecurried and uncurried versions offfandgboth do the same, the automatic conversion makes sense.map(f)has a cleaner look thanmap(f(_)).