20110115


==, eq

scala> val x = "a"; var y = new String("a"); println( x eq y)
false
x: java.lang.String = a
y: java.lang.String = a

scala> val x = "a"; var y = new String("a"); println( x.eq(y))
false
x: java.lang.String = a
y: java.lang.String = a



Nil

scala> List(1, 2, 3)
res23: List[Int] = List(1, 2, 3)

scala> 1::2::3::Nil
res24: List[Int] = List(1, 2, 3)

scala> Nil.::(2).::(3)
res30: List[Int] = List(3, 2)

scala> Nil.::(2)
res29: List[Int] = List(2)

scala> Nil.::(2).::(3)
res30: List[Int] = List(3, 2)


assert

scala> assert( 2 == 2)

scala> assert( 2 eq 2)
<console>:6: error: value eq is not a member of Int
       assert( 2 eq 2)
               ^


foreach

scala> res10.foreach(e => println(e))
Range(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

scala> List(1,2,3)        
res14: List[Int] = List(1, 2, 3)

scala> for (e <- res14) println(e)
1
2
3

scala> List(1,2,3,4,5).foreach{ println }
1
2
3
4
5

scala> res10.foreach(println(_))         
Range(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

scala> res14.foreach(println(_))
1
2
3

scala> res14.foreach(println)   
1
2
3

scala> res14 foreach { println }
1
2
3

scala> res14 foreach  println   
1
2
3
--------------------

scala> res22.filter(e => e > 1)
res23: List[Int] = List(2, 3)

scala> res22.filter(_ > 1)      
res24: List[Int] = List(2, 3)

scala> res22.filter(1 < _)
res25: List[Int] = List(2, 3)

scala> res42.map( e => e + 2)
res45: List[Int] = List(3, 4, 5)


scala> res42.map( _ + 2)     
res46: List[Int] = List(3, 4, 5)

scala> res1 filter{ _ > 1 } map { _ + 2 } foreach { println }
4
5

scala> List(1,2,3) ::: 4::5::6::Nil  
res10: List[Int] = List(1, 2, 3, 4, 5, 6)

scala> List(1,2,3).:::(4::5::6::Nil)
res11: List[Int] = List(4, 5, 6, 1, 2, 3)

scala> List(1,2,3).:::(Nil::6::5::4)
<console>:6: error: value :: is not a member of Int
       List(1,2,3).:::(Nil::6::5::4)
                                  ^

scala> List(1,2,3).::: (Nil.:: 6 .::5 .::4)
<console>:1: error: ')' expected but integer literal found.
       List(1,2,3).::: (Nil.:: 6 .::5 .::4)
                               ^

scala> List(1,2,3).::: (Nil.:: (6) .::(5) .::(4))   
res13: List[Int] = List(4, 5, 6, 1, 2, 3)

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

scala> List(1,2,3) ::: List(4,5,6) map(_ + 3) foreach println
4
5
6
7
8
9

scala> List(1,2,3) ::: List(4,5,6) map(_ > 3) foreach println
false
false
false
true
true
true


scala> val a = Array(1,3); a(1) = 123; a(0) // update & apply
a: Array[Int] = Array(1, 123)

scala> val a = Array(1,3); a(1) = 123; println(a(0)) // update & apply
1
a: Array[Int] = Array(1, 123)

scala> (1,2)                                                          
res17: (Int, Int) = (1,2)

scala> res17.getClass
res18: java.lang.Class[_] = class scala.Tuple2$mcII$sp

scala> res17._

_1   _2

scala> res17._1
res19: Int = 1

scala> (1,1.1,"1.1")
res20: (Int, Double, java.lang.String) = (1,1.1,1.1)

scala> (1,List(1,2,3))
res21: (Int, List[Int]) = (1,List(1, 2, 3))

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

scala> a(0) = 3

scala> a
res23: Array[Int] = Array(3, 2, 3)

scala> a(2)
res24: Int = 3

scala> a.update(0,3)

scala> a.update(0,4)

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

scala> a.update(0,4) // a(0) = 4

scala> for(1 to 4) print(_.update(a,3))
<console>:1: error: '<-' expected but ')' found.
       for(1 to 4) print(_.update(a,3))
                 ^

scala> for(1 <- 4) print(_.update(a,3))
<console>:7: error: value filter is not a member of Int
       for(1 <- 4) print(_.update(a,3))
                ^

scala> for(e <- 1 to 4) print(a.update(e,3))
()()java.lang.ArrayIndexOutOfBoundsException: 3
at $anonfun$1.apply$mcVI$sp(<console>:7)
at scala.collection.immutable.Range$ByOne$class.foreach$mVc$sp(Range.scala:282)
at scala.collection.immutable.Range$$anon$1.foreach$mVc$sp(Range.scala:274)
at .<init>(<console>:7)
at .<clinit>(<console>)
at RequestResult$.<init>(<console>:9)
at RequestResult$.<clinit>(<console>)
at RequestResult$scala_repl_result(<console>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at scala.tools.nsc.Interpreter$Request$$anonfun$loadAndRun$1$$anonfun$apply$17.apply(Int...
scala> for(e <- 0 to 2) print(a.update(e,3))
()()()
scala> for(e <- 0 to 2) a.update(e,3)       

scala> a
res33: Array[Int] = Array(3, 3, 3)

scala> var x = 1
x: Int = 1

scala> x = 123
x: Int = 123

scala> x =323412512351235
<console>:1: error: integer number too large
       x =323412512351235
          ^

scala> x =323412512      
x: Int = 323412512

scala> val y = 123
y: Int = 123

scala> val y = 1  
y: Int = 1

scala> y = 1     
<console>:6: error: reassignment to val
       y = 1
         ^

scala> Array(1,2,3)
res34: Array[Int] = Array(1, 2, 3)

scala> res34(0)
res35: Int = 1

scala> res34.apply(0)
res36: Int = 1

scala> def a(a: Int, b: Int) = a + b;                         
a: (a: Int,b: Int)Int

scala> def a(a: Int, b: Int):Int = { a + b}   
a: (a: Int,b: Int)Int

scala> def a(a: Int, b: Int):Int = a + b   
a: (a: Int,b: Int)Int

scala> def a(a: Int, b: Int) = a + b    
a: (a: Int,b: Int)Int

scala> def a(a: Int, b: Int) {a + b}
a: (a: Int,b: Int)Unit

scala> def a(a: Int, b: Int) = a + b    
a: (a: Int,b: Int)Int

scala> for( a <- 1 to 10 if a > 6; b <- 1 to 10 if b > 6) yield a*b
res69: scala.collection.immutable.IndexedSeq[Int] = Vector(49, 56, 63, 70, 56, 64, 72, 80, 63, 72, 81, 90, 70, 80, 90, 100)

scala> for( a <- 1 to ; b <- 1 to 10 if b > 6) yield a*b           
<console>:11: error: ambiguous reference to overloaded definition,
both method to in class RichInt of type (end: Int,step: Int)scala.collection.immutable.Range.Inclusive
and  method to in class RichInt of type (end: Int)scala.collection.immutable.Range.Inclusive with scala.collection.immutable.Range.ByOne
match expected type ?
       for( a <- 1 to ; b <- 1 to 10 if b > 6) yield a*b
                   ^

scala> for( a <- 1.to(10) ) println(a)
1
2
3
4
5
6
7
8
9
10

scala> for( a <- 1 to 10 ) println(a) 
1
2
3
4
5
6
7
8
9
10

scala> for( a <- 1 to 10 if a > 10 ) println(a)

scala> for( a <- 1 to 10 if a > 5 ) println(a) 
6
7
8
9
10

scala> for( a <- 1 to 10; b <- 1 to 10 ) println(a*b) 
1
2
3
4
5
6
7
8
9
10
2
4
6
8
10
12
14
16
18
20
3
6
9
12
15
18
21
24
27
30
4
8
12
16
20
24
28
32
36
40
5
10
15
20
25
30
35
40
45
50
6
12
18
24
30
36
42
48
54
60
7
14
21
28
35
42
49
56
63
70
8
16
24
32
40
48
56
64
72
80
9
18
27
36
45
54
63
72
81
90
10
20
30
40
50
60
70
80
90
100

scala> for( a <- 1 to 10 if a > 7; b <- 1 to 10 if b > 8) println(a*b)
72
80
81
90
90
100






Comments