Reputation: 34099
I can merge two lists as follow together:
List(1,2,3) ::: List(4,5,6)
and the result is:
res2: List[Int] = List(1, 2, 3, 4, 5, 6)
the operator :::
is right associative, what does it mean?
In math, right associative is:
5 + ( 5 - ( 2 * 3 ) )
Upvotes: 1
Views: 275
Reputation: 5305
In the most general sense, right-associative means that if you don't put any parentheses, they will be assumed to be on the right:
a ::: b ::: c == a ::: (b ::: c)
whereas left-associative operators (such as +
) would have
a + b + c == (a + b) + c
However, according to the spec (6.12.3 Infix Operations),
A left-associative binary operation
e1 op e2
is interpreted ase1.op(e2)
. If op is rightassociative, the same operation is interpreted as{ val x=e1; e2.op(x ) }
, wherex
is a fresh name.
So right-associative operators in scala are considered as methods of their right operand with their left operand as parameter (as explained in @Yuval's answer).
Upvotes: 1
Reputation: 149598
Right associative means the operator (in our case, the :::
method) is applied on the right operand while using the left operand as the argument. This means that the actual method invocation is done like this:
List(4,5,6).:::(List(1,2,3))
Since :::
prepends the list, the result is List(1,2,3,4,5,6)
.
Upvotes: 2