val a = list.map(_.split(','))
// the above is a shorthand for
val a = list.map({ case i => i.split(',') })
// Now lets consider this
val a = list.map { case (a, b) => a + b }
// or this
val a = list map { case (a, b) => a + b }
// some people like the above ways of writing
// because they consider of less brackets as cleaner code.
// but the above two are another way to write
val a = list.map({ case (a, b) => a + b })
The thing to understand here is that in Scala you can use spaces instead of .
to access instance methods.
So basically,
// lets say you have a class A
case class A(i: Int) {
def merged[B](b: B): String = i.toString + " :: " + b.toString
}
//Now, lets say you have an instance of A
val a = A(5)
// and an instance of String
val s: String = "abcd"
// Now, if you write
val merged = a merged s
// it is syntactic sugar for
val merged = a.merged(s)
Similarly, List[A]
has a method map[B](f: A => B): List[B]
.
val list = List[Int](1, 2, 3)
// so when you write
val list2 = list map { case i => i + 1}
// its syntactic sugar for,
val list2 = list.map({ case i => i + 1 })
// There is a lot going on here
// if we were to do everything step by step
// first you are creating a partial function
val pf: PartialFunction[Int, Int] = { case i => i + 1 }
// Now, PartialFunction[Int, Int] is a subtype of Int => Int
// so we can refer to it as a Int => Int
val f: Int => Int = pf
// Now we pass it as an argument to map
val list2 = list.map(f)