In Scala, operators are just special-named methods. Here ' '.!=
fetches but does not apply the !=
to the ' '
object. This method takes one parameter and returns a boolean value.
For any function f
that takes a single argument, (x => f(x))
and f
are equivalent expressions – so there is no need to wrap such a function into another lambda.
The span
method takes a predicate, and partitions a sequence (here: String) into a prefix that satisfies the predicate, and a suffix that doesn't. Here we want to get the string up to the first space.
- In the first line, we use the predicate
(_ != ' ')
, more clearly (c => c != ' ')
.
- We can now swap the arguments of the inequality “operator”, as this operation should be reflexive. We now have:
(c => ' ' != c)
.
- We can now use the explicit method call syntax:
(c => ' '.!=(c))
.
Next, we save that method in another variable:
val f: (Char => Boolean) = ' '.!=
...
(c => f(c))
As stated above, f
and (c => f(c))
are equivalent, therefore (_ != ' ')
and ' '.!=
must be equivalent too.