scala [待整理]

Microsoft Windows [版本 10.0.16299.431]
(c) 2017 Microsoft Corporation。保留所有权利。

C:\Users\enmonster>scala
Welcome to Scala 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_77).
Type in expressions for evaluation. Or try :help.

scala> val it = Iterator(1,2,3)
it: Iterator[Int] = non-empty iterator

scala> it foreach println
1
2
3

scala> it foreach println

scala> it.toArray
res2: Array[Int] = Array()

scala> res2 foreach println

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

scala> for(2 <- a) yield e
<console>:13: error: not found: value e
       for(2 <- a) yield e
                         ^

scala> for(e <- a) println e
<console>:13: error: value e is not a member of Unit
       for(e <- a) println e
                           ^

scala> for(e <- a) println(e)
1
2
3
4
5

scala> for(e <- a)yield(e)
res7: Array[Int] = Array(1, 2, 3, 4, 5)

scala> for(i <- 0 until fruits.length) yield (i,fruits(i))
<console>:12: error: not found: value fruits
       for(i <- 0 until fruits.length) yield (i,fruits(i))
                        ^
<console>:12: error: not found: value fruits
       for(i <- 0 until fruits.length) yield (i,fruits(i))
                                                ^

scala> fruits
<console>:12: error: not found: value fruits
       fruits
       ^

scala> val fruits = scala.collection.mutable.ArrayBuffer("apple","banana")
fruits: scala.collection.mutable.ArrayBuffer[String] = ArrayBuffer(apple, banana)

scala> val x = for(e <- fruits) yield e.toUpperCase
x: scala.collection.mutable.ArrayBuffer[String] = ArrayBuffer(APPLE, BANANA)

scala> val fruits = "apple"::"banana":"orange"::Nil
<console>:1: error: identifier expected but string literal found.
val fruits = "apple"::"banana":"orange"::Nil
                               ^

scala> val fruits = "apple"::"banana"::"orange"::Nil
fruits: List[String] = List(apple, banana, orange)

scala> val x = for(e <- fruits) yield e.toUpperCase
x: List[String] = List(APPLE, BANANA, ORANGE)

scala> val nieces = List("Aleka","Christina","Molly")
nieces: List[String] = List(Aleka, Christina, Molly)

scala> val elems = nieces.map(niece => <li>{niece}<li>}
<console>:1: error: in XML literal: in XML content, please use '}}' to express '}'
val elems = nieces.map(niece => <li>{niece}<li>}
                                               ^
<console>:1: error:  I encountered a '}' where I didn't expect one, maybe this tag isn't closed <li>
val elems = nieces.map(niece => <li>{niece}<li>}
                                            ^

scala> val elems = nieces.map(niece => <li>{niece}<li>})
<console>:1: error: in XML literal: in XML content, please use '}}' to express '}'
val elems = nieces.map(niece => <li>{niece}<li>})
                                               ^
<console>:1: error:  I encountered a '}' where I didn't expect one, maybe this tag isn't closed <li>
val elems = nieces.map(niece => <li>{niece}<li>})
                                            ^

scala> val elems = nieces.map(niece => <li>{niece}</li>})
<console>:1: error: ')' expected but '}' found.
val elems = nieces.map(niece => <li>{niece}</li>})
                                                ^

scala> val elems = nieces.map(niece => <li>{niece}</li>)
elems: List[scala.xml.Elem] = List(<li>Aleka</li>, <li>Christina</li>, <li>Molly</li>)

scala> val ul = <ul>{nieces.map(i=><li>{i}</li>)}</ul>
<console>:12: error: not found: value i
       val ul = <ul>{nieces.map(i=><li>{i}</li>)}</ul>
                                ^
<console>:12: error: not found: value li
       val ul = <ul>{nieces.map(i=><li>{i}</li>)}</ul>
                                    ^
<console>:12: error: not found: value i
       val ul = <ul>{nieces.map(i=><li>{i}</li>)}</ul>
                                        ^
<console>:12: error: not found: value li
       val ul = <ul>{nieces.map(i=><li>{i}</li>)}</ul>
                                            ^

scala> val ul = <ul>{ nieces.map( i => <li> {i} </li> ) } </ul>
ul: scala.xml.Elem = <ul><li> Aleka </li><li> Christina </li><li> Molly </li> </ul>

scala> val ul = <ul>{nieces.map(i=><li>{i}</li>)}</ul>
<console>:12: error: not found: value i
       val ul = <ul>{nieces.map(i=><li>{i}</li>)}</ul>
                                ^
<console>:12: error: not found: value li
       val ul = <ul>{nieces.map(i=><li>{i}</li>)}</ul>
                                    ^
<console>:12: error: not found: value i
       val ul = <ul>{nieces.map(i=><li>{i}</li>)}</ul>
                                        ^
<console>:12: error: not found: value li
       val ul = <ul>{nieces.map(i=><li>{i}</li>)}</ul>
                                            ^

scala> val fruits = List("apple","banana","lemon","orange"}
<console>:1: error: ')' expected but '}' found.
val fruits = List("apple","banana","lemon","orange"}
                                                   ^

scala> val fruits = List("apple","banana","lemon","orange")
fruits: List[String] = List(apple, banana, lemon, orange)

scala> val newFruits = fruits.map(f=>
     | if(f.length<6) f.toUpperCase
     | }
<console>:3: error: ')' expected but '}' found.
}
^

scala> val newFruits = fruits.map(f=>
     | if(f.length<6) f.toUpperCase
     | )
newFruits: List[Any] = List(APPLE, (), LEMON, ())

scala> fruits.filter(_.length<6).map(_.toUpperCase)
res10: List[String] = List(APPLE, LEMON)

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

scala> val result = lol.flateen
<console>:12: error: value flateen is not a member of List[List[Int]]
       val result = lol.flateen
                        ^

scala> val result = lol.flateen
<console>:12: error: value flateen is not a member of List[List[Int]]
       val result = lol.flateen
                        ^

scala> val result = lol.flatten
result: List[Int] = List(1, 2, 3, 4)

scala> val a = Array(Array(1,2),Array(3,4))
a: Array[Array[Int]] = Array(Array(1, 2), Array(3, 4))

scala> a.flatten
res11: Array[Int] = Array(1, 2, 3, 4)

scala> val list = List("Hello","scala")
list: List[String] = List(Hello, scala)

scala> list.flatten
res12: List[Char] = List(H, e, l, l, o, s, c, a, l, a)

scala> val x = Vector(Some(1),None,Some(3),None)
x: scala.collection.immutable.Vector[Option[Int]] = Vector(Some(1), None, Some(3), None)

scala> x.flatten
res13: scala.collection.immutable.Vector[Int] = Vector(1, 3)

scala> val bag = List("1","2","three","4","one hundred")
bag: List[String] = List(1, 2, three, 4, one hundred)

scala> def toInt(i:String):Option[Int]={
     | try{
     | Some(Integer.parseInt(in.trim))
     | }catch{
     | case e:Exception =>None
     | }
     | }
<console>:13: error: not found: value in
       Some(Integer.parseInt(in.trim))
                             ^

scala> def toInt(i:String):Option[Int]={
     |       try{
     |       Some(Integer.parseInt(i.trim))
     |       }catch{
     |       case e:Exception =>None
     |       }
     |       }
toInt: (i: String)Option[Int]

scala> bag.flatMap(toInt).sum
res14: Int = 7

scala> bag.map(toInt)
res15: List[Option[Int]] = List(Some(1), Some(2), None, Some(4), None)

scala> bag foreach(Integer.parseInt(i.trim))
<console>:13: error: not found: value i
       bag foreach(Integer.parseInt(i.trim))
                                    ^

scala> bag foreach(Integer.parseInt(_.trim))
<console>:13: error: missing parameter type for expanded function ((x$1) => x$1.trim)
       bag foreach(Integer.parseInt(_.trim))
                                    ^

scala> bag foreach(i => Integer.parseInt(i.trim))
java.lang.NumberFormatException: For input string: "three"
  at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
  at java.lang.Integer.parseInt(Integer.java:580)
  at java.lang.Integer.parseInt(Integer.java:615)
  at $anonfun$1.apply(<console>:13)
  at $anonfun$1.apply(<console>:13)
  at scala.collection.immutable.List.foreach(List.scala:381)
  ... 32 elided

scala> val view = (1 to 10).view
view: scala.collection.SeqView[Int,scala.collection.immutable.IndexedSeq[Int]] = SeqView(...)

scala> val x = view.force
x: scala.collection.immutable.IndexedSeq[Int] = Vector(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

scala> view.println
<console>:13: error: value println is not a member of scala.collection.SeqView[Int,scala.collection.immutable.IndexedSeq[Int]]
       view.println
            ^

scala> view.println()
<console>:13: error: value println is not a member of scala.collection.SeqView[Int,scala.collection.immutable.IndexedSeq[Int]]
       view.println()
            ^

scala> (1 to 10).foreach(println)
1
2
3
4
5
6
7
8
9
10

scala> (1 to 10).view.foreach(println)
1
2
3
4
5
6
7
8
9
10

scala> (1 to 10).view.map(_ * 2)
res23: scala.collection.SeqView[Int,Seq[_]] = SeqViewM(...)

scala> (1 to 10).map(_ * 2)
res24: scala.collection.immutable.IndexedSeq[Int] = Vector(2, 4, 6, 8, 10, 12, 14, 16, 18, 20)

scala> val arr = (1 to 10).toArray
arr: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

scala> val view = arr.slice()
<console>:12: error: not enough arguments for method slice: (from: Int, until: Int)Array[Int].
Unspecified value parameters from, until.
       val view = arr.slice()
                           ^

scala> val view = arr.slice(1,10)
view: Array[Int] = Array(2, 3, 4, 5, 6, 7, 8, 9, 10)

scala> val view = arr.slice(1,9)
view: Array[Int] = Array(2, 3, 4, 5, 6, 7, 8, 9)

scala> val view = arr.slice(0,9)
view: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9)

scala> arr(2)
res25: Int = 3

scala> val view = arr.view.slice(2,5)
view: scala.collection.mutable.IndexedSeqView[Int,Array[Int]] = SeqViewS(...)

scala> arr(2)
res26: Int = 3

猜你喜欢

转载自blog.csdn.net/liu16659/article/details/81030583