0

i'm looking for any resourse with scala syntax description. for example, right now i tried to understand what doing this function:

reduceByKey(_ ++ _)

but i don't able to find what does mean ++ character... i looked at: http://www.scala-lang.org/files/archive/spec/2.11/ but it doesn't answer on my question. possobly someone could advice good resource like "underastanding scala" with good detailed examples

thanks!

2 Answers 2

3

++ is just a method name in Scala, like + or *. You can find a list of all standard library methods starting with non-letters at http://www.scala-lang.org/api/current/index.html#index.index-_.

Sign up to request clarification or add additional context in comments.

Comments

0

In addition to @alexey-romanov answer

In general ++ is used for generating a new data structure.

We can use it in following forms(with example) :-

1. ++

++ is used to yield a new data structure.

Ex :-

val digits2=Map("One"  -> 1,"Two"  -> 2)
val digits = Map[String,Int]() ++ digits2 

digits: scala.collection.immutable.Map[String,Int] = Map(One -> 1, Two -> 2) 

OR

Note:- Below digits4's map is having a key One with a value 5

val digits4=Map("One"  -> 5,"Three"-> 3,"Four"  -> 4)
val Combined1 = digits2 ++digits4

Combined1: scala.collection.immutable.Map[String,Int] = Map(One -> 5, Two -> 2, Three -> 3, Four -> 4)

2. ++:

This operator does same as above ++ does. When used on two operands.

val Combined2 = digits2 ++:digits4
Combined2: scala.collection.immutable.Map[String,Int] = Map(One -> 5, Two -> 2, Three -> 3, Four -> 4)

3. ++= and ++=:

they do similar operations on other data types defined in http://www.scala-lang.org/api/current/index.html#index.index-_.

val buf = new StringBuilder
buf += 'a'
buf ++= "bcdef"
println("buf : "+buf)

results buf : abcdef

val buf1 = new ArrayBuffer[String]()
buf1 +="a"
val buf2 = new ArrayBuffer[String]()
buf2 +="bcde"
val buf3 = buf1 ++=: buf2
println("buf3 = "+buf3)

results :- buf3 = ArrayBuffer(a, bcde)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.