1. 程式人生 > >Scala 例項wordcount

Scala 例項wordcount

                ^


scala> f(500)
res70: Int = 500


scala> f(500l)
<console>:10: error: type mismatch;
 found   : Long(500L)
 required: Int
              f(500l)
                ^


scala> f(500d)
<console>:10: error: type mismatch;
 found   : Double(500.0)
 required: Int
              f(500d)
                ^


scala> f(500s)
<console>:1: error: Invalid literal number
       f(500s)
         ^


scala> f(500)
res73: Int = 500


scala> Int =333;
<console>:7: error: reassignment to val
       Int =333;
           ^


scala> Int =333l;
<console>:7: error: reassignment to val
       Int =333l;
           ^


scala> def  f(x:Int) :Int =x
f: (x: Int)Int


scala> def  f(x:Int) :float =x
<console>:8: error: not found: type float
       def  f(x:Int) :float =x
                      ^


scala> def  f(x:Int) :float =x;
<console>:8: error: not found: type float
       def  f(x:Int) :float =x;
                      ^


scala> def  f(x:Int) :long =x;
<console>:8: error: not found: type long
       def  f(x:Int) :long =x;
                      ^


scala> def  f(x:Int) :Long =x;
f: (x: Int)Long


scala> def  f(x:Int) :Float =x;
f: (x: Int)Float


scala> def  f(x:Int) :String =x;
<console>:8: error: type mismatch;
 found   : Int
 required: String
       def  f(x:Int) :String =x;
                              ^


scala> def  f(x:Int) :string =x;
<console>:8: error: not found: type string
       def  f(x:Int) :string =x;
                      ^


scala> def  f(x:Int) :Float =x;
f: (x: Int)Float




scala> f(2);
res74: Float = 2.0


scala> f(2);
res75: Float = 2.0


scala> \
<console>:8: error: not found: value \
              \
    
scala> def h(x:Int):Unit =println(x);
h: (x: Int)Unit


scala> h(11)
11


scala> def h(x:Int):Unit
     | ;
<console>:7: error: only classes can have declared but undefined members
       def h(x:Int):Unit
           ^


scala> def h(x:Int):Unit;
<console>:7: error: only classes can have declared but undefined members
       def h(x:Int):Unit;
           ^


scala> def h(x:Int):Unit=x;
<console>:8: warning: a pure expression does nothing in statement position; you may be omitting necessary parentheses
       def h(x:Int):Unit=x;
                         ^
h: (x: Int)Unit


scala> var s=h(33)
s: Unit = ()


scala> var s=h(33)
s: Unit = ()


scala> var s=h(33):Unit=x;
<console>:1: error: ';' expected but '=' found.
       var s=h(33):Unit=x;
                       ^


scala> var s=h(33):Unit=println(x);
<console>:1: error: ';' expected but '=' found.
       var s=h(33):Unit=println(x);
                       ^


scala> h(x:Int):Unit=println(x);
<console>:1: error: ';' expected but '=' found.
       h(x:Int):Unit=println(x);
                    ^


scala> hh(x:Int):Unit=println(x);
<console>:1: error: ';' expected but '=' found.
       hh(x:Int):Unit=println(x);
                     ^


scala> def hh(x:Int):Unit=println(x);
hh: (x: Int)Unit


scala> hh(33)
33


scala> hh(2)
2


scala> hh(21)
21


scala> hh(214)
214


scala> hh(215)
215


scala> hh(2156)
2156


scala> hh(21567)
21567


scala> hh(215678)
215678


scala> hh(2156789)
2156789


scala> hh(21567890)
21567890


scala> hhd(x:Int):Unit=x;
<console>:1: error: ';' expected but '=' found.
       hhd(x:Int):Unit=x;
                      ^


scala> hhd(x:Int):Unit;
<console>:9: error: not found: value hhd
              hhd(x:Int):Unit;
              ^


scala> def hhd(x:Int):Unit=println(x*x);
hhd: (x: Int)Unit


scala> hhd(3)
9


scala> var hhd =hhd(3)
<console>:9: error: recursive method hhd needs type
       var hhd =hhd(3)
                ^


scala> var hhd = hhd(3)
<console>:9: error: recursive method hhd needs type
       var hhd = hhd(3)
                 ^


scala> val hhd = hhd(3)
<console>:9: error: recursive value hhd needs type
       val hhd = hhd(3)
                 ^


scala> def hhd(x:Int):Unit=println(x*x);
hhd: (x: Int)Unit


scala> var s =hhd(2);
4
s: Unit = ()


scala> hhd(2);
4


scala> hhd(2);
4




scala> ll
<console>:8: error: not found: value ll
              ll
              ^




scala> 


scala> 


scala> 


scala> 


scala> val list0 =List(1,2,3,4,5)
list0: List[Int] = List(1, 2, 3, 4, 5)


scala> list0.map(_*10)
res93: List[Int] = List(10, 20, 30, 40, 50)


scala> list0.filter(_%2==0)
res94: List[Int] = List(2, 4)


scala> list0.sorted
res95: List[Int] = List(1, 2, 3, 4, 5)


scala> list0.sorted.reverse
res96: List[Int] = List(5, 4, 3, 2, 1)


scala> list0.sortWith(_>_)
res97: List[Int] = List(5, 4, 3, 2, 1)


scala> list0.sortWith(_<_)
res98: List[Int] = List(1, 2, 3, 4, 5)


scala> list0.sortWith(_+_)
<console>:9: error: type mismatch;
 found   : Int
 required: Boolean
              list0.sortWith(_+_)
                              ^


scala> list0.sortWith(_>_)
res100: List[Int] = List(5, 4, 3, 2, 1)


scala> list0.sortWith(_>_).reverse
res101: List[Int] = List(1, 2, 3, 4, 5)


scala> list0.grouped(2)
res102: Iterator[List[Int]] = non-empty iterator


scala> list0.grouped(2).toList
res103: List[List[Int]] = List(List(1, 2), List(3, 4), List(5))


scala> list0.grouped(3).toList
res104: List[List[Int]] = List(List(1, 2, 3), List(4, 5))


scala> 


scala> list0.grouped(3).toList.patten
<console>:9: error: value patten is not a member of List[List[Int]]
              list0.grouped(3).toList.patten
                                      ^


scala> list0.grouped(3).toList.paltten
<console>:9: error: value paltten is not a member of List[List[Int]]
              list0.grouped(3).toList.paltten
                                      ^


scala> list0.grouped(3).toList
res107: List[List[Int]] = List(List(1, 2, 3), List(4, 5))


scala> list0.grouped(4).toList
res108: List[List[Int]] = List(List(1, 2, 3, 4), List(5))


scala> list0.grouped(1).toList
res109: List[List[Int]] = List(List(1), List(2), List(3), List(4), List(5))


scala> list0.grouped(2).toList
res110: List[List[Int]] = List(List(1, 2), List(3, 4), List(5))


scala> res110.flatten
res111: List[Int] = List(1, 2, 3, 4, 5)


scala> res110.flatten
res112: List[Int] = List(1, 2, 3, 4, 5)


scala> res110.flatten
res113: List[Int] = List(1, 2, 3, 4, 5)


scala> res110.flatten
res114: List[Int] = List(1, 2, 3, 4, 5)


scala> res110.flatten
res115: List[Int] = List(1, 2, 3, 4, 5)


scala> res110.flatten
res116: List[Int] = List(1, 2, 3, 4, 5)


scala> res110.flatten
res117: List[Int] = List(1, 2, 3, 4, 5)


scala> res110.flatten
res118: List[Int] = List(1, 2, 3, 4, 5)


scala> res110.flatten
res119: List[Int] = List(1, 2, 3, 4, 5)


scala> res110.flatten
res120: List[Int] = List(1, 2, 3, 4, 5)


scala> res110.flatten
res121: List[Int] = List(1, 2, 3, 4, 5)


scala> res110.flatten
res122: List[Int] = List(1, 2, 3, 4, 5)


scala> list0.grouped(2).toList
res123: List[List[Int]] = List(List(1, 2), List(3, 4), List(5))


scala> res123.flatMap(_*10)
<console>:10: error: value * is not a member of List[Int]
              res123.flatMap(_*10)
                              ^


scala> list0.flatMap(_*10)
<console>:9: error: type mismatch;
 found   : Int
 required: scala.collection.GenTraversableOnce[?]
              list0.flatMap(_*10)
                             ^


scala> res123.flatMap(_*10)
<console>:10: error: value * is not a member of List[Int]
              res123.flatMap(_*10)
                              ^


scala> 


scala> 


scala> res123.Map(_*10)
<console>:10: error: value Map is not a member of List[List[Int]]
              res123.Map(_*10)
                     ^


scala> res123.Map(_.map(_*10))
<console>:10: error: value Map is not a member of List[List[Int]]
              res123.Map(_.map(_*10))
                     ^


scala> res123.Map(_.Map(_*10))
<console>:10: error: value Map is not a member of List[List[Int]]
              res123.Map(_.Map(_*10))
                     ^


scala> list0.grouped(2).toList
res130: List[List[Int]] = List(List(1, 2), List(3, 4), List(5))


scala> res130.Map(_.Map(_*10))
<console>:10: error: value Map is not a member of List[List[Int]]
              res130.Map(_.Map(_*10))
                     ^


scala> res130.map(_.map(_*10))
res132: List[List[Int]] = List(List(10, 20), List(30, 40), List(50))


scala> res130.map(_.map(_*10)).flatten
res133: List[Int] = List(10, 20, 30, 40, 50)


scala> res130.flatMap(_*10)
<console>:10: error: value * is not a member of List[Int]
              res130.flatMap(_*10)
                              ^


scala> res130.flatMap(_.map(_*10))
res135: List[Int] = List(10, 20, 30, 40, 50)



scala> res130.flatten
res136: List[Int] = List(1, 2, 3, 4, 5)


scala> res130.flatMap
<console>:10: error: missing arguments for method flatMap in trait TraversableLike;
follow this method with `_' if you want to treat it as a partially applied function
              res130.flatMap
                     ^


scala> res130.flatMap()
<console>:10: error: not enough arguments for method flatMap: (f: List[Int] => scala.collection.GenTraversableOnce[B])(implicit bf: scala.collection.generic.CanBuildFrom[List[List[Int]],B,That])That.
Unspecified value parameter f.
              res130.flatMap()
                            ^






scala> var lines =Array("hello tom ","hello jerry tom")
lines: Array[String] = Array("hello tom ", hello jerry tom)


scala> var lines =Array("hello tom ","hello jerry tom","hello kitty")
lines: Array[String] = Array("hello tom ", hello jerry tom, hello kitty)


scala> lines.map(_.split(""))
res139: Array[Array[String]] = Array(Array(h, e, l, l, o, " ", t, o, m, " "), Array(h, e, l, l, o, " ", j, e, r, r, y, " ", t, o, m), Array(h, e, l, l, o, " ", k, i, t, t, y))


scala> lines.map(_.split(" "))
res140: Array[Array[String]] = Array(Array(hello, tom), Array(hello, jerry, tom), Array(hello, kitty))


scala> lines.map(_.split(" ")).flatten
res141: Array[String] = Array(hello, tom, hello, jerry, tom, hello, kitty)


scala> 


scala> 


scala> 


scala> 


scala> 


scala> lines.map(_.split(" ")).flatten
res142: Array[String] = Array(hello, tom, hello, jerry, tom, hello, kitty)


scala> lines.map(_.split(" "))
res143: Array[Array[String]] = Array(Array(hello, tom), Array(hello, jerry, tom), Array(hello, kitty))


scala> lines.map(_.split(" ")).flatten
res144: Array[String] = Array(hello, tom, hello, jerry, tom, hello, kitty)


scala> var lines =List("hello tom ","hello jerry tom","hello kitty")

lines: List[String] = List("hello tom ", hello jerry tom, hello kitty)


scala> lines.flatMap(_.split( " " ))
res145: List[String] = List(hello, tom, hello, jerry, tom, hello, kitty)


scala> lines.flatMap(_.split( " " )).map((_,1))
res146: List[(String, Int)] = List((hello,1), (tom,1), (hello,1), (jerry,1), (tom,1), (hello,1), (kitty,1))


scala> lines.flatMap(_.split( " " )).map((_,1)).groupby(_._1)
<console>:9: error: value groupby is not a member of List[(String, Int)]
              lines.flatMap(_.split( " " )).map((_,1)).groupby(_._1)
                                                       ^


scala> lines.flatMap(_.split( " " )).map((_,1)).groupedby(_._1)
<console>:9: error: value groupedby is not a member of List[(String, Int)]
              lines.flatMap(_.split( " " )).map((_,1)).groupedby(_._1)
                                                       ^


scala> lines.flatMap(_.split( " " )).map((_,1)).groupBy(_._1)
res149: scala.collection.immutable.Map[String,List[(String, Int)]] = Map(tom -> List((tom,1), (tom,1)), kitty -> List((kitty,1)), jerry -> List((jerry,1)), hello -> List((hello,1), (hello,1), (hello,1)))


scala> lines.flatMap(_.split( " " )).map((_,1)).groupBy(_._1).map(t=>(t._1,t._2)
     | lines.flatMap(_.split( " " )).map((_,1)).groupBy(_._1).map(t=>(t._1,t._2))
<console>:2: error: ')' expected but '.' found.
       lines.flatMap(_.split( " " )).map((_,1)).groupBy(_._1).map(t=>(t._1,t._2))
            ^


scala> lines.flatMap(_.split( " " )).map((_,1)).groupBy(_._1).map(t=>(t._1,t._2))
res150: scala.collection.immutable.Map[String,List[(String, Int)]] = Map(tom -> List((tom,1), (tom,1)), kitty -> List((kitty,1)), jerry -> List((jerry,1)), hello -> List((hello,1), (hello,1), (hello,1)))




scala> ;
<console>:2: error: illegal start of simple expression
              ;
              ^


scala> c
res152: scala.collection.immutable.Range.Inclusive = Range(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)




scala> lines.flatMap(_.split( " " )).map((_,1)).groupBy(_._1).map(t=>(t._1,t._2.size))
res153: scala.collection.immutable.Map[String,Int] = Map(tom -> 2, kitty -> 1, jerry -> 1, hello -> 3)


scala> lines.flatMap(_.split( " " )).map((_,1)).groupBy(_._1).map(t=>(t._1,t._2.size))
res154: scala.collection.immutable.Map[String,Int] = Map(tom -> 2, kitty -> 1, jerry -> 1, hello -> 3)


scala> lines.flatMap(_.split( " " )).map((_,1)).groupBy(_._1).map(t=>(t._1,t._2.size)).sortBy(t._2)
<console>:9: error: value sortBy is not a member of scala.collection.immutable.Map[String,Int]
              lines.flatMap(_.split( " " )).map((_,1)).groupBy(_._1).map(t=>(t._1,t._2.size)).sortBy(t._2)
                                                                                              ^


scala> lines.flatMap(_.split( " " )).map((_,1)).groupBy(_._1).map(t=>(t._1,t._2.size)).toList;
res156: List[(String, Int)] = List((tom,2), (kitty,1), (jerry,1), (hello,3))


scala> lines.flatMap(_.split( " " )).map((_,1)).groupBy(_._1).map(t=>(t._1,t._2.size)).toList
res157: List[(String, Int)] = List((tom,2), (kitty,1), (jerry,1), (hello,3))


scala> lines.flatMap(_.split( " " )).map((_,1)).groupBy(_._1).map(t=>(t._1,t._2.size)).toList.sortBy(_._2)
res158: List[(String, Int)] = List((kitty,1), (jerry,1), (tom,2), (hello,3))


scala> lines.flatMap(_.split( " " )).map((_,1)).groupBy(_._1).map(t=>(t._1,t._2.size)).toList.sortBy(_._2).reverse
res159: List[(String, Int)] = List((hello,3), (tom,2), (jerry,1), (kitty,1))


scala> for(c<-0 until res159.size)  println(res159(c))
(hello,3)
(tom,2)
(jerry,1)
(kitty,1)



scala> res159.map(_._2*10)
res161: List[Int] = List(30, 20, 10, 10)


scala> res159.map(t=>(t._2*10))
res162: List[Int] = List(30, 20, 10, 10)


scala> var f = (x:Int)=>x*x;
f: Int => Int = <function1>


scala> f(4)
res163: Int = 16


scala> res159.map(t=>(t))
res164: List[(String, Int)] = List((hello,3), (tom,2), (jerry,1), (kitty,1))


scala> res159.map(t)
<console>:10: error: not found: value t
              res159.map(t)
                         ^


scala> res159.map(t=>(t._1,t_2))
<console>:10: error: not found: value t_2
              res159.map(t=>(t._1,t_2))
                                  ^


scala> res159.map(t=>(t._1,t._2))
res167: List[(String, Int)] = List((hello,3), (tom,2), (jerry,1), (kitty,1))


scala> res159.map(t=>(t._1+"w",t._2))
res168: List[(String, Int)] = List((hellow,3), (tomw,2), (jerryw,1), (kittyw,1))


scala> res159.map(t=>(t._1+"w",t._2+“x”))
<console>:1: error: illegal character '\u201c'
       res159.map(t=>(t._1+"w",t._2+“x”))
                                    ^
<console>:1: error: illegal character '\u201d'
       res159.map(t=>(t._1+"w",t._2+“x”))
                                      ^


scala> res159.map(t=>(t._1+"w",t._2+"x"))
res169: List[(String, String)] = List((hellow,3x), (tomw,2x), (jerryw,1x), (kittyw,1x))


scala> res169.sortBy(_._2)
res170: List[(String, String)] = List((jerryw,1x), (kittyw,1x), (tomw,2x), (hellow,3x))