The general way is:
someList.filter((x: Int) => x > 0)
However, Scala provides a number of ways to write function literals more briefly. One way is to leave off the parameter types.
someList.filter((x) => x > 0)
This is called
target typing, because the targeted usage of an expression (an argument to someList.filter()) is allowed to influence the typing of that expression (to determine the type of the x parameter).
The next way to remove useless characters is to leave out parentheses around a parameter whose type is inferred.
someList.filter(x => x > 0)
To make a function literal even more concise, you can use underscores as placeholders for parameters.
someList.filter( _ > 0)
Read more:
Short forms of function literals
Login in to like
Login in to comment