I'm trying to pick up some scala. Reading through examples I came across this impossible-to-google nugget:
case 3 => l ::: List(3)
What does the triple colon accomplish?
The double colon ( :: ) is the cons operator; x represents the head of the list, and xs the tail.
because the colon colon method :: adds an element to the front of a list.
++= can mean two different things in Scala: 1: Invoke the ++= method. In your example with flatMap , the ++= method of Builder takes another collection and adds its elements into the builder. Many of the other mutable collections in the Scala collections library define a similiar ++= method.
The ::: operator in Scala is used to concatenate two or more lists. Then it returns the concatenated list. Example 1: Scala.
In Scala almost anything can be a name a method and just like foo.bar (i) is calling the method bar on object foo with i as argument, foo.:: (i) is calling the colon colon method on object foo with i as an argument. Usually this is used as infix notation operator (in scala any method can be used as an operator).
(i) is calling the colon colon method on object foo with i as an argument. Usually this is used as infix notation operator (in scala any method can be used as an operator). Methods who end in a colon are special in Scala, they are right associative which mean they stick to their right hand argument Is Scala an easy programming language?
What do double colons (::) mean in the Scala programming language? - Quora What do double colons (::) mean in the Scala programming language? :: doesn’t mean anything in general.
The beauty of this is (a) it’s just the normal if/else syntax, so you don’t have to remember something else, and (b) it’s easy to read. To show a more real-world example, here’s an example of how you can use the Scala ternary operator syntax on the right hand side of the equation:
Concatenates two lists - javadoc
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