filter(_)
is desugared into x => filter(x)
. Look to your map usage: map(println(_))
, it is desugared into map(x => println(x))
but not into map(println(x => x))
, that not right and wont work
Anonymous function pecularities
-
02-06-2022 - |
Question
I am puzzled by the difference between
Array(true,false).filter(x=>x).map(println(_))
(runs fine)
and
Array(true,false).filter(_).map(println(_))
(throws error)
Notice the filter arguments: x=>x
versus _
. I was under the expression that x=>x
and _
were synonymous. How to explain this?
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow