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?
=> 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 .
_1 is a method name. Specifically tuples have a method named _1 , which returns the first element of the tuple.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With