1

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 ) )
softshipper
  • 32,463
  • 51
  • 192
  • 400
  • take a look at http://stackoverflow.com/questions/1162924/what-good-are-right-associative-methods-in-scala – Yaneeve Apr 05 '17 at 07:34

2 Answers2

2

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).

Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
1

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 as e1.op(e2). If op is rightassociative, the same operation is interpreted as { val x=e1; e2.op(x ) }, where x 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).

Cyrille Corpet
  • 5,265
  • 1
  • 14
  • 31