1. 程式人生 > >SCALA練習

SCALA練習

scala> val lines = List("hello tom hello jerry","hello tom hello hello hello")
lines: List[String] = List(hello tom hello jerry, hello tom hello hello hello)

scala> lines.map(_.split(" "))
res0: List[Array[String]] = List(Array(hello, tom, hello, jerry), Array(hello, tom, hello, hello, hello))
scala> lines.map(_.split(" ")).flatten res1: List[String] = List(hello, tom, hello, jerry, hello, tom, hello, hello, hello) scala> lines.flatMap(_.split(" ")) res3: List[String] = List(hello, tom, hello, jerry, hello, tom, hello, hello, hello) scala> lines.flatMap(_.split(" ")).map((_,1
)) res4: List[(String, Int)] = List((hello,1), (tom,1), (hello,1), (jerry,1), (hello,1), (tom,1), (hello,1), (hello,1), (hello,1)) scala> lines.flatMap(_.split(" ")).map((_,1)).groupBy(_._1) res5: scala.collection.immutable.Map[String,List[(String, Int)]] = Map(tom -> List((tom,1), (tom,1)), jerry -
> List((jerry,1)), hello -> List((hello,1), (hello,1), (hello,1), (hello,1), (hello,1), (hello,1))) scala> lines.flatMap(_.split(" ")).map((_,1)).groupBy(_._1).map(_._1) res6: scala.collection.immutable.Iterable[String] = List(tom, jerry, hello) scala> lines.flatMap(_.split(" ")).map((_,1)).groupBy(_._1).map(t=>(t._1,t._2.size)) res8: scala.collection.immutable.Map[String,Int] = Map(tom -> 2, jerry -> 1, hello -> 6) scala> lines.flatMap(_.split(" ")).map((_,1)).groupBy(_._1).map(t=>(t._1,t._2.size)).toList res15: List[(String, Int)] = List((tom,2), (jerry,1), (hello,6)) scala> lines.flatMap(_.split(" ")).map((_,1)).groupBy(_._1).map(t=>(t._1,t._2.size)).toList.sortBy(_._2) res16: List[(String, Int)] = List((jerry,1), (tom,2), (hello,6)) scala> lines.flatMap(_.split(" ")).map((_,1)).groupBy(_._1).map(t=>(t._1,t._2.size)).toList.sortBy(_._2).reverse res17: List[(String, Int)] = List((hello,6), (tom,2), (jerry,1)) scala> lines.flatMap(_.split(" ")).map((_,1)).groupBy(_._1).mapValues(_.size) res18: scala.collection.immutable.Map[String,Int] = Map(tom -> 2, jerry -> 1, hello -> 6) scala> lines.flatMap(_.split(" ")).map((_,1)).groupBy(_._1).mapValues(_.size).toList res19: List[(String, Int)] = List((tom,2), (jerry,1), (hello,6)) scala> lines.flatMap(_.split(" ")).map((_,1)).groupBy(_._1).mapValues(_.size).toList.reverse res20: List[(String, Int)] = List((hello,6), (jerry,1), (tom,2)) scala> val a= Array(1,2,3,4,5,6,7,8) a: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8) scala> a.sum res21: Int = 36 scala> a.reduce(_+_) res23: Int = 36 scala> a.reduce(_-_) res24: Int = -34 scala> a.par.reduce(_+_) res25: Int = 36 scala> a.fold(10)(_+_) res26: Int = 46 scala> a.par.fold(10)(_+_) res27: Int = 76 scala> a.par.fold(0)(_+_) res30: Int = 36 scala> a.par.foldLeft(0)(_+_) res31: Int = 36 scala> a.par.foldRight(0)(_+_) res32: Int = 36 scala> a.foldRight(0)(_+_) res33: Int = 36 scala> a.foldLeft(0)(_+_) res34: Int = 36 scala> lines.flatMap(_.split(" ")).map((_,1)).groupBy(_._1) res36: scala.collection.immutable.Map[String,List[(String, Int)]] = Map(tom -> List((tom,1), (tom,1)), jerry -> List((jerry,1)), hello -> List((hello,1), (hello,1), (hello,1), (hello,1), (hello,1), (hello,1))) scala> lines.flatMap(_.split(" ")).map((_,1)).groupBy(_._1).mapValues(_.foldLeft(0)(_+_._2)) res38: scala.collection.immutable.Map[String,Int] = Map(tom -> 2, jerry -> 1, hello -> 6) scala> lines.flatMap(_.split(" ")).map((_,1)).groupBy(_._1).mapValues(_.foldLeft(0)(_+_._2)).toList res39: List[(String, Int)] = List((tom,2), (jerry,1), (hello,6)) scala> lines.flatMap(_.split(" ")).map((_,1)).groupBy(_._1).mapValues(_.foldLeft(0)(_+_._2)).toList.reverse res40: List[(String, Int)] = List((hello,6), (jerry,1), (tom,2))