Author | Message |
---|---|
G190852562
Posts: 162
|
Posted 19:58 Dec 08, 2014 |
xs :+ x Create a new vector with trailing element x, preceded by all elements of xs What do they mean by "trailing element x"?
|
303496263
Posts: 68
|
Posted 20:03 Dec 08, 2014 |
Here is an example for you to visualize this. val xs = Vector(1, 2, 3, 4, 5) //> xs : scala.collection.immutable.Vector[Int] = Vector(1, 2, 3, 4, 5) Trailing meaning pretty much followed by/in the end/last one. So the code above takes the vector xs and appends 6 to it putting 6 in the end of the vector. |
G190852562
Posts: 162
|
Posted 20:18 Dec 08, 2014 |
Then what's the difference between doing that and xs +: 6? |
303496263
Posts: 68
|
Posted 20:31 Dec 08, 2014 |
xs +: 6 is indeed and invalid statement in Scala. You need to keep in mind that the colon : always points to the the sequence! Here, the sequence is obviously xs. Note that 6 +: xs would return a new vector which has 6 as its head element. Note that the colon : again points to the sequence which is xs. Makes sense? Last edited by 303496263 at
20:33 Dec 08, 2014.
|
G190852562
Posts: 162
|
Posted 20:48 Dec 08, 2014 |
Oh okay. So the sequence must be the list. How about if this was done? xs +: xs |
303496263
Posts: 68
|
Posted 20:52 Dec 08, 2014 |
Well, if you run that in a Scala worksheet you can see what happens: val xs = Vector(1, 2, 3, 4, 5) //> xs : scala.collection.immutable.Vector[Int] = Vector(1, 2, 3, 4, 5) As you can see, it creates a new vector which contains the first vector xs followed by the elements of the second vector ys. Last edited by 303496263 at
20:54 Dec 08, 2014.
|
G190852562
Posts: 162
|
Posted 21:09 Dec 08, 2014 |
Interesting. |
303496263
Posts: 68
|
Posted 21:10 Dec 08, 2014 |
Very :) |