Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does ":+" mean in Scala

Tags:

I saw some Scala code written as:

 def next(): Array[String] = someVariable.next() :+ iterator.key 

Where someVariable has a method next() to get the next line and the iterator is of type Iterator[String].

What does :+ mean here?

like image 319
jlp Avatar asked May 12 '16 14:05

jlp


People also ask

What is the meaning of => in Scala?

=> is syntactic sugar for creating instances of functions. Recall that every function in scala is an instance of a class. For example, the type Int => String , is equivalent to the type Function1[Int,String] i.e. a function that takes an argument of type Int and returns a String .

What does _ _1 mean in Scala?

_1 is a method name. Specifically tuples have a method named _1 , which returns the first element of the tuple.

What does () mean in Scala?

It can denote a function or method that takes no parameters, such as: def foo() = "Hello world" Note that when writing an anonymous function, the () is by itself but still means a function with no parameters.

What does double => mean in Scala?

In Scala, Double is a 64-bit floating point number, which is equivalent to Java's double primitive type. The >(x: Double) method is utilized to return true if this value is greater than x, false otherwise. Method Definition – def >(x: Double): Boolean.


1 Answers

On Scala Collections there is usually :+ and +:.
Both add an element to the collection. :+ appends +: prepends.
A good reminder is, : is where the Collection goes.

There is as well colA ++: colB to concat collections, where the : side collection determines the resulting type. If a :++ exists, it is the same as ++. In both cases the left side collection determines the type of result.

like image 167
Nabil A. Avatar answered Nov 12 '22 04:11

Nabil A.