Scala
Tuples
scala> (1 -> "a")._1
res: Int = 1
scala> (1 -> "a")._2
res: String = a
scala> val t = ("hello", "world", 3)
t: (String, String, Int) = (hello,world,3)
scala> t._1
res: String = hello
Map
scala> Seq("hello", "world").map(x => "prefix_" + x).foreach(println)
prefix_hello
prefix_world
scala> a
res41: Array[String] = Array(COLMAR;68000, LILLE;59000)
scala> a.map(_ => "")
res0: Array[String] = Array("", "")
scala> a.map(_.split(";"))
res2: Array[Array[String]] = Array(Array(COLMAR, 68000), Array(LILLE, 59000))
scala> a.map(s => s.split(";"))
res3: Array[Array[String]] = Array(Array(COLMAR, 68000), Array(LILLE, 59000))
scala> a.map(_.split(";") match {
| case Array(code, city) => (city, code)
| })
res4: Array[(String, String)] = Array((68000,COLMAR), (59000,LILLE))
scala> a.map(_.split(";")).map { case Array(code, city) => (city, code) }
res5: Array[(String, String)] = Array((68000,COLMAR), (59000,LILLE))
Reduce
scala> a
res41: Array[String] = Array(COLMAR;68000, LILLE;59000)
scala> a.map(_.split(";")).map { case Array(code, city) => (city, code) }
.reduce((a ,b) => (a._1, b._1))
res39: (String, String) = (68000,59000)
FlatMap
scala> val fruits = Seq("apple", "banana", "orange")
fruits: Seq[java.lang.String] = List(apple, banana, orange)
scala> fruits.flatMap(_.toUpperCase)
res1: Seq[Char] = List(A, P, P, L, E, B, A, N, A, N, A, O, R, A, N, G, E)
Zipped
scala> ("hello", "world", "HERE").zipped.toList
res73: List[(Char, Char, Char)] = List((h,w,H), (e,o,E), (l,r,R), (l,l,E))
scala> ("hello", "world", "HI").zipped.toList
res74: List[(Char, Char, Char)] = List((h,w,H), (e,o,I))
ZippedWithIndex
scala> List("a", "b").ZippedWithIndex()
res: List[(String, Int)] = List(("a", 0), ("b", 1))