reset password
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)
 xs :+ 6                                               //> res9: scala.collection.immutable.Vector[Int] = Vector(1, 2, 3, 4, 5, 6)

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 |
303496263 wrote:

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)
 xs :+ 6                                               //> res9: scala.collection.immutable.Vector[Int] = Vector(1, 2, 3, 4, 5, 6)

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.

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 |
303496263 wrote:

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?

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)
  val ys = Vector(6, 7, 8, 9, 0)                  //> ys  : scala.collection.immutable.Vector[Int] = Vector(6, 7, 8, 9, 0)
  xs +:ys                                         //> res10: scala.collection.immutable.Vector[Any] = Vector(Vector(1, 2, 3, 4, 5)
                                                  //| , 6, 7, 8, 9, 0)

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 |
303496263 wrote:

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)
  val ys = Vector(6, 7, 8, 9, 0)                  //> ys  : scala.collection.immutable.Vector[Int] = Vector(6, 7, 8, 9, 0)
  xs +:ys                                         //> res10: scala.collection.immutable.Vector[Any] = Vector(Vector(1, 2, 3, 4, 5)
                                                  //| , 6, 7, 8, 9, 0)

As you can see, it creates a new vector which contains the first vector xs followed by the elements of the second vector ys.

Interesting.

303496263
Posts: 68
Posted 21:10 Dec 08, 2014 |

Very :)