I am looking at a piece of code with the following:
graph.vertices.filter(!_._2._1)
I understand that _
are wildcard characters in scala
but I do not know what the !
is supposed to do.
What does !
mean in scala?
_1 is a method name. Specifically tuples have a method named _1 , which returns the first element of the tuple.
The ::: operator in Scala is used to concatenate two or more lists. Then it returns the concatenated list. Example 1: Scala.
It can denote a function or method that takes no parameters, such as: def foo() = "Hello world" Note that when writing an anonymous function, the () is by itself but still means a function with no parameters.
Symbols are used to establish bindings between a name and the entity it refers to, such as a class or a method. Anything you define and can give a name to in Scala has an associated symbol.
Scala doesn't have operators at the syntax level. All operations are methods.
For example, there is no add operator in the syntax, but numbers have a +
method:
2.+(3) // result is 5
When you write 2 + 3
, that's actually syntax sugar for the expression above.
Any type can define a unary_!
method, which is what !something
gets desugared to. Booleans implement it, with the obvious meaning of logical negation ("not") that the exclamation mark has in other languages with C heritage.
In your question, the expression is an abbreviated form of the following call:
graph.vertices.filter { t => !(t._2._1) }
where t
is a tuple-of-tuples, for which the first element of the second element has a type that implements unary_!
and (as required by .filter
) returns a Boolean
. I would bet the money in my pocket that the element itself is a Boolean
, in which case !
just means "not."
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With