Question

i am beginner in scala and i stuck in one line. i am working in scala worksheet

     type Bit = Int

     var acc: List[Bit]=List()                        //> acc  : List[Listpr.Bit] = List()

     acc:+1                                           //> res0: List[Int] = List(1)

     "ins"+acc:+1                                     //> res1: scala.collection.immutable.IndexedSeq[AnyVal] = Vector(i, n, s, L, i, 
                                                  //| s, t, (, ), 1)
     println("ins"+acc:+1)                         //> Vector(i, n, s, L, i, s, t, (, ), 1)

i am not getting what does mean of :+ in scala and if i add "ins"+acc:+1 it is converting into vector collection in scala

anyone please help me with this?

Was it helpful?

Solution

I think the scala docs explains it pretty well.

A copy of this list with an element appended.

scala> acc
res3: List[Bit] = List()

scala> acc :+ 1
res4: List[Int] = List(1)

scala> acc
res5: List[Bit] = List() 

Basically when you do acc:+1 it creates a new list copying elements of acc and adding 1 to it. No change is made to acc.

Well I think its a typo. I think you means ("ins" :: acc ):+1

scala> "ins"+acc:+1
res8: scala.collection.immutable.IndexedSeq[AnyVal] = Vector(i, n, s, L, i, s, t
, (, ), 1)

scala> "ins":+acc:+1
res9: scala.collection.immutable.IndexedSeq[Any] = Vector(i, n, s, List(), 1)

scala> ("ins" :: acc) :+ 1
res10: List[Any] = List(ins, 1)

"ins"+acc:+1 prints as shown because String in scala implicitly is an IndexedSeq. Doing "ins"+acc is basically "ins"+acc.toString which is "insList()". Now "insList()" is of type IndexedSeq[Char]. Doing :+ 1 to it will make it convert to IndexedSeq[AnyVal] as (String <: AnyVal and Int <:AnyVal). Hence it prints a sequence as shown.

OTHER TIPS

I'm learning Scala too, and there are so many crazy operators.

Basically, :+ appends an item to a copy of the list. The :+ operator for List is defined here:

def
:+(elem: A): List[A]
[use case]
A copy of this list with an element appended.

A mnemonic for +: vs. :+ is: the COLon goes on the COLlection side.

Example:

scala> import scala.collection.mutable.LinkedList
import scala.collection.mutable.LinkedList

scala> val a = LinkedList(1)
a: scala.collection.mutable.LinkedList[Int] = LinkedList(1)

scala> val b = a :+ 2
b: scala.collection.mutable.LinkedList[Int] = LinkedList(1, 2)

scala> println(a)
LinkedList(1)
elem
the appended element
returns
a new list consisting of all elements of this list followed by elem.
Definition Classes
SeqLike → GenSeqLike
Full Signature
def :+[B >: A, That](elem: B)(implicit bf: CanBuildFrom[List[A], B, That]): That

Now with your String example, it gets tricky. Scala has a Type called String (defined in scala.Predef) which is an alias for java.lang.String. I don't see where :+ is defined for String, but it seems to do analogous behavior as best it can. Append the thing on the right of the operator to the String but produce a Vector as a general-purpose seq. That sort of makes sense. Keep in mind also Scala's Vector isn't the same as Java's.

Hope that helps.

Straight from the docs :+ returns "A copy of this list with an element appended". i.e. append.

acc:+1 is straight forward returning List(1) which is what you would expect. Append 1 to the empty list.

"ins"+acc:+1 is trickier since "ins"+acc is evaluated first and concatenates the string "acc" and the string representation of acc which is `insList(). Then :+ is called on the string insList() which auto-magically converts it to a List of characters and appends 1. Finally this gives the result scala.collection.immutable.IndexedSeq[AnyVal] = Vector(i, n, s, L, i, s, t, (, ), 1)

In addition to the other good answers, I would like to encourage you to browse the scaladoc yourself:

http://www.scala-lang.org/api/current/index.html#scala.collection.immutable.List

You will discover several things for yourself, and this will increase your confidence on the language and the standard library. In the amount of time that it took you to post this on stackoverflow, you might

  • notice that just as :+ appends to Sequences, there is a +: that returns a copy of the list with an element prepended!

  • notice "See also" that asks you to see "Scala's Collection Library overview" section on Lists, which turns out to be quite eye-opening, too.

  • read this gem that, "Note that :-ending operators are right associative (see example). A mnemonic for +: vs. :+ is: the COLon goes on the COLlection side"

All the best with this wonderful, terse, expressive (and confusing!) language.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top