I am new to scala..I came across a concept where it says like below:
{ val x = a; b.:::(x) }
In this block a is still evaluated before b, and then the result of this evaluation is passed as an operand to b’s ::: method
What is the meaning of above statement..
I tried like below:
var a =10
var b =20
What should be the result i should expect.
Can somebody please give me an example...
Thanks in advance....
The ::: operator in Scala is used to concatenate two or more lists. Then it returns the concatenated list. Example 1: Scala.
First thing is that, in the strict sense, Scala does not have operators. It only has methods defined on objects. And when a Symbol (that is, a method name) ends with a colon, it means it is actually defined on the right operand, instead of it being a method on the left operand.
The double colon ( :: ) is the cons operator; x represents the head of the list, and xs the tail. So the pattern match will first make a distinction as to whether the list is empty. But if the list was not empty, it will also name the head of the list x and the tail of the list xs .
++= 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 is defined on List
trait and concatenates two lists. Using it on Int
like in your example (var a=10
) shouldn't work (unless you define such operator yourself).
Here is how it works on lists:
val a = List(1, 2);
val b = List(3, 4);
val c1 = a ::: b // List(1, 2, 3, 4)
val c2 = a.:::(b) // List(3, 4, 1, 2)
Calling :::
with the infix syntax (c1
) and method call syntax (c2
) differ in the order in which lists are concatenated (see Jörg's comment).
The statement "a is still evaluated before b" means that a
is evaluated before passing it as an argument to the method call. Unless the method uses call by name, its arguments are evaluated before the call just like in Java.
This could give you some hint how to search for meaning of Scala operators and keywords.
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