scala 基本语法-ok

8 篇文章 0 订阅
[root@cdh2 network-scripts]# scala
Welcome to Scala version 2.9.3 (Java HotSpot(TM) Client VM, Java 1.7.0_67).
Type in expressions to have them evaluated.
Type :help for more information.

scala> object HelloWorld {
     |           def main(args: Array[String]) {
     |             println("Hello, world!")
     |           }
     |         }
defined module HelloWorld

scala> HelloWorld.main(null);
Hello, world!

scala> 

[root@cdh2 network-scripts]# cd /user/local/
[root@cdh2 local]# ll
total 1134428
drwxr-xr-x 11 root   root         4096 Jun 30 16:22 apache-storm-0.9.2-incubating
drwxr-xr-x  5 root   root         4096 Apr 21  2012 examples-ch02-getting_started-master
drwxr-xr-x  5   1000   1000       4096 Jun 30 20:23 ez_setup-0.9
-rwxr-xr-x  1 root   root         6577 Jun 30 17:59 ez_setup-0.9.tar.gz
-rw-r--r--  1 root   root        12402 Jun  2 06:15 ez_setup.py
drwxr-xr-x 10 root   root         4096 Jun 16 16:19 hadoop-2.6.0
drwxr-xr-x  9 root   root         4096 Jun 27 08:40 hbase-0.98.20-hadoop2
drwxr-xr-x  8 root   root         4096 Jun 16 02:50 jdk
drwxr-xr-x 10 root   root         4096 Jun 27 23:49 jzmq
drwxr-xr-x  6 root   root         4096 Jun 29 03:24 kafka_2.9.2-0.8.1.1
drwxrwxr-x 16   1000   1000       4096 Jul  1 00:09 Python-3.4.3
-rw-r--r--  1 root   root     19554643 Feb 25  2015 Python-3.4.3.tgz
drwxr-xr-x  8    119    129       4096 Jun 17 08:35 scala-2.9.3
drwxr-xr-x  9 root   root         4096 Jun 30 22:34 setuptools-23.1.0
-rw-r--r--  1 root   root       648277 Jun 30 22:02 setuptools-23.1.0.zip
drwxr-xr-x 13 hadoop hadoop       4096 Jun 17 09:03 spark-1.4.0-bin-hadoop2.6
drwxrwxrwx  4   1000 users        4096 Jun 30 23:49 sqlite-3.6.20
-rwxr-xr-x  1 root   root      1407934 Jun 30 23:43 sqlite-amalgamation-3.6.20.tar.gz
drwxr-xr-x  4 root   root         4096 Jun 28 18:07 src_bak
drwxr-xr-x  2 root   root         4096 Jun 28 23:01 src_old
-rwxr-xr-x  1 root   root   1203927040 Jun 28 18:03 src_old.tar
drwxrwxr-x 11   1000   1000       4096 Jun 27 23:44 zeromq-4.1.2
drwxr-xr-x 11   1000   1000       4096 Jun 29 23:31 zookeeper-3.4.6
drwxr-xr-x 11 root   root         4096 Jun 29 23:30 zookeeper-3.4.6_1
drwxr-xr-x 11 root   root         4096 Jun 29 23:30 zookeeper-3.4.6_2
drwxr-xr-x 11 root   root         4096 Jun 29 22:14 zookeeper-3.4.6_3
-rw-r--r--  1 root   root        19117 Jul  1 00:03 zookeeper.out
[root@cdh2 local]# mkdir test
[root@cdh2 local]# cd test/
[root@cdh2 test]# ll
total 0
[root@cdh2 test]# pwd
/user/local/test
[root@cdh2 test]# touch script.sh
[root@cdh2 test]# vim script.sh
[root@cdh2 test]# ./script.sh
bash: ./script.sh: Permission denied
[root@cdh2 test]# ll
total 4
-rw-r--r-- 1 root root 176 Jul  4 03:54 script.sh
[root@cdh2 test]# chmod +x script.sh 
[root@cdh2 test]# ll
total 4
-rwxr-xr-x 1 root root 176 Jul  4 03:54 script.sh
[root@cdh2 test]# ./script.sh
error: script file does not close its header with !# or ::!#
one error found
[root@cdh2 test]# cat script.sh
#!/bin/sh
  exec scala "$0" "$@"
  !#
  object HelloWorld {
    def main(args: Array[String]) {
      println("Hello, world! " + args.toList)
    }
  }
  HelloWorld.main(args)
[root@cdh2 test]# touch person.sh
[root@cdh2 test]# vim person.sh
[root@cdh2 test]# chmod +x person.sh 
[root@cdh2 test]# ll
total 8
-rwxr-xr-x 1 root root 193 Jul  4 04:06 person.sh
-rwxr-xr-x 1 root root 176 Jul  4 03:54 script.sh
[root@cdh2 test]# pe
peekfd      perl        perl5.10.1  perlbug     perldoc     perlthanks  perror      pethtool    
[root@cdh2 test]# ll
total 8
-rwxr-xr-x 1 root root 193 Jul  4 04:06 person.sh
-rwxr-xr-x 1 root root 176 Jul  4 03:54 script.sh
[root@cdh2 test]# ./person.sh 
Person(Al)
[root@cdh2 test]# cat script.sh
#!/bin/sh
  exec scala "$0" "$@"
  !#
  object HelloWorld {
    def main(args: Array[String]) {
      println("Hello, world! " + args.toList)
    }
  }
  HelloWorld.main(args)
[root@cdh2 test]# cat person.sh 
#!/bin/sh
exec scala "$0" "$@"
!#

case class Person(name: String)

object HelloWorld {
  def main(args: Array[String]) {
    val al = Person("Al")
    println(al)
  }
}

HelloWorld.main(args)
[root@cdh2 test]# vim script.sh
[root@cdh2 test]# ./script.sh 
Hello, world! List()
[root@cdh2 test]# ./script.sh hahah
Hello, world! List(hahah)
[root@cdh2 test]# ./script.sh haha
Hello, world! List(haha)
[root@cdh2 test]# cat person.sh 
#!/bin/sh
exec scala "$0" "$@"
!#

case class Person(name: String)

object HelloWorld {
  def main(args: Array[String]) {
    val al = Person("Al")
    println(al)
  }
}

HelloWorld.main(args)
[root@cdh2 test]# 

============================
 history

 1105  ls -l
 1106  vi __init__.py 
 1107  cd ..
 1108  pwd
 1109  python manage.py runserver 0.0.0.0:8000
 1110  cat HelloWorld/urls.py 
 1111  vim HelloWorld/urls.py 
 1112  cat HelloWorld/urls.py 
 1113  python manage.py runserver 0.0.0.0:8000
 1114  pwd
 1115  vim HelloWorld/urls.py
 1116  pwd
 1117  tree
 1118  cd HelloWorld/
 1119  ls -l
 1120  rm -rf __pycache__/
 1121  ls -l
 1122  python manage.py runserver 0.0.0.0:8000
 1123  cd ..
 1124  python manage.py runserver 0.0.0.0:8000
 1125  vim HelloWorld/urls.py
 1126  cd HelloWorld/
 1127  rm -rf __pycache__/
 1128  cd ..
 1129  python manage.py runserver 0.0.0.0:8000
 1130  cd HelloWorld/
 1131  pwd
 1132  cd HelloWorld/
 1133  ll
 1134  pwd
 1135  ll- l
 1136   cd ..
 1137  ll
 1138  ls -lre
 1139  ls -ltr
 1140  django-admin.py startproject HelloWorld
 1141  ll
 1142  python
 1143  ifconfig
 1144  service network restart
 1145  ifconfig
 1146  cat /etc/profile
 1147  cat /etc/hosts
 1148  service network restart
 1149  cd /etc/sysconfig/
 1150  ll
 1151  cd network-scripts
 1152  ll
 1153  ls -ltr
 1154  cat ifcfg-eth0
 1155  ifconfig
 1156  service network restart
 1157  ifconfig
 1158  service network restart
 1159  ifconfig
 1160  vim ifcfg-eth0
 1161  cat /etc/hosts
 1162  vim /etc/hosts
 1163  cat /etc/hosts
 1164  cat ifcfg-eth0
 1165  service network restart
 1166  ping  192.168.3.120
 1167  ifconfig
 1168  service network restart
 1169  ifconfig
 1170  scala
 1171  ll
 1172  pwd
 1173  cd /user/local/
 1174  ll
 1175  mkdir test
 1176  cd test/
 1177  ll
 1178  pwd
 1179  touch script.sh
 1180  vim script.sh
 1181  ./script.sh
 1182  ll
 1183  chmod +x script.sh 
 1184  ll
 1185  ./script.sh
 1186  cat script.sh
 1187  touch person.sh
 1188  vim person.sh
 1189  chmod +x person.sh 
 1190  ll
 1191  ./person.sh 
 1192  cat script.sh
 1193  cat person.sh 
 1194  vim script.sh
 1195  ./script.sh 
 1196  ./script.sh hahah
 1197  ./script.sh haha
 1198  cat person.sh 
 1199  ll
 1200  mkdir classes
 1201  ll
 1202  touch HelloWorld.scala
 1203  vim HelloWorld.scala
 1204  ls -l classes/
 1205  scalac -d classes HelloWorld.scala
 1206  ls -l classes/
 1207  scala HelloWorld
 1208  scala -classpath classes HelloWorld
 1209  touch application.scala
 1210  vim application.scala
 1211  cat application.scala
 1212  vim application.scala
 1213  cat application.scala
 1214  scalac -d classes application.scala 
 1215  ls -l classes/
 1216  mv application.scala HelloWorld2.scala
 1217  ls -l classes/
 1218  ll
 1219  ls -l classes/
 1220  rm -rf *2*
 1221  ll
 1222  ls -l classes/
 1223  rm -rf classes/*2*
 1224  ll
 1225  ls -l classes/
 1226  touch application.scala
 1227  vim application.scala
 1228  cat application.scala
 1229  mv application.scala HelloWorld2.scala
 1230  ls -l classes/
 1231  scalac -d classes HelloWorld2.scala 
 1232  LL
 1233  ll
 1234  ls -l classes/
 1235  scala classes/HelloWorld2
 1236  scala HelloWorld2
 1237  ll
 1238  scala -d classes HelloWorld2
 1239  scala -d classes HelloWorld2.scala 
 1240  scala -classpath classes HelloWorld2.scala 
 1241  ls -l classes/
 1242  rm -rf classes/*2*
 1243  ls -l classes/
 1244  scalac -d classes HelloWorld2.scala 
 1245  ls -l classes/
 1246  vim HelloWorld2.scala 
 1247  rm -rf classes/*2*
 1248  ls -l classes/
 1249  scalac -d classes HelloWorld2.scala 
 1250  history
[root@cdh2 test]# 
[root@cdh2 test]# 
[root@cdh2 test]# ll
total 20
drwxr-xr-x 2 root root 4096 Jul  4 18:44 classes
-rw-r--r-- 1 root root  104 Jul  4 18:44 HelloWorld2.scala
-rw-r--r-- 1 root root   88 Jul  4 04:18 HelloWorld.scala
-rwxr-xr-x 1 root root  193 Jul  4 04:06 person.sh
-rwxr-xr-x 1 root root  166 Jul  4 04:13 script.sh
[root@cdh2 test]# vim Test.scala
[root@cdh2 test]# scalac -d classes HelloWorld2.scala  
HelloWorld2.scala:1: error: expected class or object definition
#!/bin/sh
^
HelloWorld2.scala:2: error: expected class or object definition
exec scala "$0" "$@"
^
HelloWorld2.scala:3: error: expected class or object definition
!#
^
three errors found
[root@cdh2 test]# scalac -d classes Test.scala.scala  
error: source file 'Test.scala.scala' could not be found
one error found
[root@cdh2 test]# ls -l classes/
total 8
-rw-r--r-- 1 root root 637 Jul  4 04:18 HelloWorld.class
-rw-r--r-- 1 root root 606 Jul  4 04:18 HelloWorld$.class
[root@cdh2 test]# scalac -d classes Test.scala
[root@cdh2 test]# scala Test.scala
[root@cdh2 test]# scala -classpath classes Test
Yiibai x location : 20
Yiibai y location : 30
[root@cdh2 test]# vim Test.scala
[root@cdh2 test]# ls -l classes/
total 20
-rw-r--r-- 1 root root  637 Jul  4 04:18 HelloWorld.class
-rw-r--r-- 1 root root  606 Jul  4 04:18 HelloWorld$.class
-rw-r--r-- 1 root root  599 Jul  4 18:58 Test.class
-rw-r--r-- 1 root root  567 Jul  4 18:58 Test$.class
-rw-r--r-- 1 root root 1897 Jul  4 18:58 Yiibai.class
[root@cdh2 test]# scala -d classes Test.scala 
[root@cdh2 test]# scalac -classpath classes Test
error: source file 'Test' could not be found
one error found
[root@cdh2 test]# ls -l classes/
total 20
-rw-r--r-- 1 root root  637 Jul  4 04:18 HelloWorld.class
-rw-r--r-- 1 root root  606 Jul  4 04:18 HelloWorld$.class
-rw-r--r-- 1 root root  599 Jul  4 18:58 Test.class
-rw-r--r-- 1 root root  567 Jul  4 18:58 Test$.class
-rw-r--r-- 1 root root 1897 Jul  4 18:58 Yiibai.class
[root@cdh2 test]# scalac -d classes Test.scala 
[root@cdh2 test]# scala -classpath classes Test
Yiibai x-0 location : 10
Yiibai y-0 location : 20
Yiibai x location : 20
Yiibai y location : 30
[root@cdh2 test]# vim Test2.scala
[root@cdh2 test]# scalac -d classes Test2.scala 
[root@cdh2 test]# scala -classpath classes Test2
Yiibai x location : 20
Yiibai y location : 30
Yiibai z location : 20
[root@cdh2 test]# cat Test2.scala
import java.io._

class Yiibai(val xc: Int, val yc: Int) {
   var x: Int = xc
   var y: Int = yc
   def move(dx: Int, dy: Int) {
      x = x + dx
      y = y + dy
      println ("Yiibai x location : " + x);
      println ("Yiibai y location : " + y);
   }
}

class Location(override val xc: Int, override val yc: Int,
   val zc :Int) extends Yiibai(xc, yc){
   var z: Int = zc

   def move(dx: Int, dy: Int, dz: Int) {
      x = x + dx
      y = y + dy
      z = z + dz
      println ("Yiibai x location : " + x);
      println ("Yiibai y location : " + y);
      println ("Yiibai z location : " + z);
   }
}

object Test2 {
   def main(args: Array[String]) {
      val loc = new Location(10, 20, 15);

      // Move to a new location
      loc.move(10, 10, 5);
   }
}
[root@cdh2 test]# scala -classpath classes Test2
Yiibai x location : 20
Yiibai y location : 30
Yiibai z location : 20
[root@cdh2 test]# scala -classpath classes Test
Yiibai x location : 20
Yiibai y location : 30
[root@cdh2 test]# vim Test3.scala
[root@cdh2 test]# cat Test3.scala
import java.io._

class Yiibai(val xc: Int, val yc: Int) {
   var x: Int = xc
   var y: Int = yc
   def move(dx: Int, dy: Int) {
      x = x + dx
      y = y + dy
   }
}

object Test3 {
   def main(args: Array[String]) {
      val yiibai = new Yiibai(10, 20)
      printYiibai

      def printYiibai{
         println ("Yiibai x location : " + yiibai.x);
         println ("Yiibai y location : " + yiibai.y);
      }
   }
}
[root@cdh2 test]# scalac -d classes Test3
error: source file 'Test3' could not be found
one error found
[root@cdh2 test]# scalac -d classes Test3.scala 
[root@cdh2 test]# scala -classpath classes Test3
Yiibai x location : 10
Yiibai y location : 20
[root@cdh2 test]# vim Test3.scala
[root@cdh2 test]# cat Test3.scala
import java.io._

class Yiibai(val xc: Int, val yc: Int) {
   var x: Int = xc
   var y: Int = yc
   def move(dx: Int, dy: Int) {
      x = x + dx
      y = y + dy
   }
}

object Test3 {
   def main(args: Array[String]) {
      val yiibai = new Yiibai(10, 20)
      printYiibai

      def printYiibai{
         println ("Yiibai--x location : " + yiibai.x);
         println ("Yiibai--y location : " + yiibai.y);
      }
   }
}
[root@cdh2 test]# scalac -d classes Test3.scala 
SC[root@cdh2 test]# scala -classpath classes Test3
Yiibai--x location : 10
Yiibai--y location : 20
[root@cdh2 test]# vim TraitTest.scala
[root@cdh2 test]# scalac -d classes TraitTest.scala
[root@cdh2 test]# scala -classpath classes TraitTest
false
true
true
[root@cdh2 test]# cat TraitTest.scala
trait Equal {
  def isEqual(x: Any): Boolean
  def isNotEqual(x: Any): Boolean = !isEqual(x)
}

class Yiibai(xc: Int, yc: Int) extends Equal {
  var x: Int = xc
  var y: Int = yc
  def isEqual(obj: Any) =
    obj.isInstanceOf[Yiibai] &&
    obj.asInstanceOf[Yiibai].x == x
}

object TraitTest {
   def main(args: Array[String]) {
      val p1 = new Yiibai(2, 3)
      val p2 = new Yiibai(2, 4)
      val p3 = new Yiibai(3, 3)

      println(p1.isNotEqual(p2))
      println(p1.isNotEqual(p3))
      println(p1.isNotEqual(2))
   }
}
[root@cdh2 test]# vim TraitTest.scala
[root@cdh2 test]# scalac -d classes TraitTest.scala
TraitTest.scala:30: error: ')' expected but '(' found.
      println(p1.isEqual(new(Yiibai(2,5))))
                                   ^
TraitTest.scala:30: error: ';' expected but ')' found.
      println(p1.isEqual(new(Yiibai(2,5))))
                                          ^
two errors found
[root@cdh2 test]# vim TraitTest.scala
[root@cdh2 test]# scalac -d classes TraitTest.scala
[root@cdh2 test]# scala -classpath classes TraitTest
false
true
true

true
false
false

true
false
true

true
[root@cdh2 test]# cat TraitTest.scala
trait Equal {
  def isEqual(x: Any): Boolean
  def isNotEqual(x: Any): Boolean = !isEqual(x)
}

class Yiibai(xc: Int, yc: Int) extends Equal {
  var x: Int = xc
  var y: Int = yc
  def isEqual(obj: Any) =
    obj.isInstanceOf[Yiibai] &&
    obj.asInstanceOf[Yiibai].x == x
}

object TraitTest {
   def main(args: Array[String]) {
      val p1 = new Yiibai(2, 3)
      val p2 = new Yiibai(2, 4)
      val p3 = new Yiibai(3, 3)

      println(p1.isNotEqual(p2))
      println(p1.isNotEqual(p3))
      println(p1.isNotEqual(2))
      println()
      println(p1.isEqual(p2))
      println(p1.isEqual(p3))
      println(p1.isEqual(2))
      println()
      println(p1.isEqual(p2))
      println(p1.isEqual(p3))
      println(p1.isEqual(new Yiibai(2,5)))
      println()
      println(p3.isEqual(new Yiibai(3,5)))
   }
}
[root@cdh2 test]# pwd
/user/local/test
[root@cdh2 test]# scala -classpath classes TraitTest
false
true
true

true
false
false

true
false
true

true
[root@cdh2 test]# vim CaseTest.scala
[root@cdh2 test]# scalac -d classes CaseTest.scala
[root@cdh2 test]# scala -classpath classes CaseTest
many
two
one
[root@cdh2 test]# cat CaseTest.scala 
object CaseTest {
   def main(args: Array[String]) {
      println(matchTest(3))
      println(matchTest(2))
      println(matchTest(1))


   }
   def matchTest(x: Int): String = x match {
      case 1 => "one"
      case 2 => "two"
      case _ => "many"
   }
}
[root@cdh2 test]# vim CaseTest2.scala
[root@cdh2 test]# scalac -d classes CaseTest2.scala
[root@cdh2 test]# scala -classpath classes CaseTest2
Exception in thread "main" java.lang.RuntimeException: Cannot figure out how to run target: CaseTest2
	at scala.sys.package$.error(package.scala:27)
	at scala.tools.nsc.GenericRunnerCommand.scala$tools$nsc$GenericRunnerCommand$$guessHowToRun(GenericRunnerCommand.scala:38)
	at scala.tools.nsc.GenericRunnerCommand$$anonfun$2.apply(GenericRunnerCommand.scala:48)
	at scala.tools.nsc.GenericRunnerCommand$$anonfun$2.apply(GenericRunnerCommand.scala:48)
	at scala.Option.getOrElse(Option.scala:108)
	at scala.tools.nsc.GenericRunnerCommand.<init>(GenericRunnerCommand.scala:48)
	at scala.tools.nsc.GenericRunnerCommand.<init>(GenericRunnerCommand.scala:17)
	at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:33)
	at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:89)
	at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)
[root@cdh2 test]# vim CaseTest2.scala
[root@cdh2 test]# scalac -d classes CaseTest2.scala
[root@cdh2 test]# scala -classpath classes CaseTest2
2
many
one
[root@cdh2 test]# cat CaseTest2.scala
object CaseTest2 {
   def main(args: Array[String]) {
      println(matchTest("two"))
      println(matchTest("test"))
      println(matchTest(1))

   }
   def matchTest(x: Any): Any = x match {
      case 1 => "one"
      case "two" => 2
      case y: Int => "scala.Int"
      case _ => "many"
   }
}
[root@cdh2 test]# vim CaseTest2.scala
[root@cdh2 test]# scalac -d classes CaseTest2.scala
^[[A[root@cdh2 tesll
total 44
-rw-r--r-- 1 root root  330 Jul  4 19:45 CaseTest2.scala
-rw-r--r-- 1 root root  263 Jul  4 19:38 CaseTest.scala
drwxr-xr-x 2 root root 4096 Jul  4 19:43 classes
-rw-r--r-- 1 root root  104 Jul  4 18:44 HelloWorld2.scala
-rw-r--r-- 1 root root   88 Jul  4 04:18 HelloWorld.scala
-rwxr-xr-x 1 root root  193 Jul  4 04:06 person.sh
-rwxr-xr-x 1 root root  166 Jul  4 04:13 script.sh
-rw-r--r-- 1 root root  769 Jul  4 19:07 Test2.scala
-rw-r--r-- 1 root root  426 Jul  4 19:13 Test3.scala
-rw-r--r-- 1 root root  493 Jul  4 19:00 Test.scala
-rw-r--r-- 1 root root  815 Jul  4 19:31 TraitTest.scala
[root@cdh2 test]# scala -classpath classes CaseTest2
2
many
one
scala.Int
[root@cdh2 test]# cat CaseTest2.scala
object CaseTest2 {
   def main(args: Array[String]) {
      println(matchTest("two"))
      println(matchTest("test"))
      println(matchTest(1))
      println(matchTest(4))

   }
   def matchTest(x: Any): Any = x match {
      case 1 => "one"
      case "two" => 2
      case y: Int => "scala.Int"
      case _ => "many"
   }
}
[root@cdh2 test]# 


[root@cdh2 test]# jps
2675 QuorumPeerMain
32348 MainGenericRunner
32504 Jps
[root@cdh2 test]# zkServer.sh status
JMX enabled by default
Using config: /user/local/zookeeper-3.4.6/bin/../conf/zoo.cfg
Mode: standalone
[root@cdh2 test]# scala
Welcome to Scala version 2.9.3 (Java HotSpot(TM) Client VM, Java 1.7.0_67).
Type in expressions to have them evaluated.
Type :help for more information.

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

scala> x
res0: List[Int] = List(1, 2, 3, 4)

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

scala> x
res1: List[Int] = List(1, 2, 3, 4, 1, 2, 3)

scala> var x = Set(1,3,5,7,1,3,5,7)
x: scala.collection.immutable.Set[Int] = Set(1, 3, 5, 7)

scala> x
res2: scala.collection.immutable.Set[Int] = Set(1, 3, 5, 7)

scala> var x = Set(1,3,5,7)
x: scala.collection.immutable.Set[Int] = Set(1, 3, 5, 7)

scala> x
res3: scala.collection.immutable.Set[Int] = Set(1, 3, 5, 7)

scala> val x = Map("one" -> 1, "two" -> 2, "three" -> 3)
x: scala.collection.immutable.Map[java.lang.String,Int] = Map(one -> 1, two -> 2, three -> 3)

scala> x
res4: scala.collection.immutable.Map[java.lang.String,Int] = Map(one -> 1, two -> 2, three -> 3)

scala> val x = (10, "Scala")
x: (Int, java.lang.String) = (10,Scala)

scala> x
res5: (Int, java.lang.String) = (10,Scala)

scala> val x:Option[Int] = Some(5)
x: Option[Int] = Some(5)

scala> x
res6: Option[Int] = Some(5)

scala> 


[root@cdh2 test]# ll
total 44
-rw-r--r-- 1 root root  330 Jul  4 19:45 CaseTest2.scala
-rw-r--r-- 1 root root  263 Jul  4 19:38 CaseTest.scala
drwxr-xr-x 2 root root 4096 Jul  4 19:43 classes
-rw-r--r-- 1 root root  104 Jul  4 18:44 HelloWorld2.scala
-rw-r--r-- 1 root root   88 Jul  4 04:18 HelloWorld.scala
-rwxr-xr-x 1 root root  193 Jul  4 04:06 person.sh
-rwxr-xr-x 1 root root  166 Jul  4 04:13 script.sh
-rw-r--r-- 1 root root  769 Jul  4 19:07 Test2.scala
-rw-r--r-- 1 root root  426 Jul  4 19:13 Test3.scala
-rw-r--r-- 1 root root  493 Jul  4 19:00 Test.scala
-rw-r--r-- 1 root root  815 Jul  4 19:31 TraitTest.scala
[root@cdh2 test]# vim CaseTest3.scala
[root@cdh2 test]# scalac -d classes CaseTest3.scala
[root@cdh2 test]# scala -classpath classes CaseTest3
()
()
()
()
[root@cdh2 test]# ls -l classes/
total 80
-rw-r--r-- 1 root root  779 Jul  4 19:45 CaseTest2.class
-rw-r--r-- 1 root root 1138 Jul  4 19:45 CaseTest2$.class
-rw-r--r-- 1 root root  762 Jul  4 19:56 CaseTest3.class
-rw-r--r-- 1 root root 1184 Jul  4 19:56 CaseTest3$.class
-rw-r--r-- 1 root root  773 Jul  4 19:42 CaseTest.class
-rw-r--r-- 1 root root 1118 Jul  4 19:42 CaseTest$.class
-rw-r--r-- 1 root root  559 Jul  4 19:31 Equal.class
-rw-r--r-- 1 root root  465 Jul  4 19:31 Equal$class.class
-rw-r--r-- 1 root root  637 Jul  4 04:18 HelloWorld.class
-rw-r--r-- 1 root root  606 Jul  4 04:18 HelloWorld$.class
-rw-r--r-- 1 root root 1967 Jul  4 19:07 Location.class
-rw-r--r-- 1 root root  605 Jul  4 19:07 Test2.class
-rw-r--r-- 1 root root  579 Jul  4 19:07 Test2$.class
-rw-r--r-- 1 root root  605 Jul  4 19:14 Test3.class
-rw-r--r-- 1 root root 1137 Jul  4 19:14 Test3$.class
-rw-r--r-- 1 root root  599 Jul  4 19:04 Test.class
-rw-r--r-- 1 root root  567 Jul  4 19:04 Test$.class
-rw-r--r-- 1 root root  630 Jul  4 19:31 TraitTest.class
-rw-r--r-- 1 root root 1163 Jul  4 19:31 TraitTest$.class
-rw-r--r-- 1 root root 1480 Jul  4 19:31 Yiibai.class
[root@cdh2 test]# scalac -d classes CaseTest3.scala
[root@cdh2 test]# scala -classpath classes CaseTest3
()
()
()
()
[root@cdh2 test]# vim CaseTest3.scala
[root@cdh2 test]# scalac -d classes CaseTest3.scala
CaseTest3.scala:9: error: illegal start of declaration (possible cause: missing `=' in front of current method body)
      x match {
      ^
one error found
[root@cdh2 test]# vim CaseTest3.scala
[root@cdh2 test]# scalac -d classes CaseTest3.scala
[root@cdh2 test]# scala -classpath classes CaseTest3
2
many
one
scala.Int
[root@cdh2 test]# cat CaseTest3.scala
object CaseTest3 {
   def main(args: Array[String]) {
      println(matchTest("two"))
      println(matchTest("test"))
      println(matchTest(1))
      println(matchTest(5))
   }
   def matchTest(x: Any):Any={
      x match {
         case 1 => "one"
         case "two" => 2
         case y: Int => "scala.Int"
         case _ => "many"
      }
   }
}
[root@cdh2 test]# ok
bash: ok: command not found
[root@cdh2 test]# vim ForTest.scala
[root@cdh2 test]# scalac -d classes/ ForTest.scala
[root@cdh2 test]# scala -classpath classes/ ForTest
Hi Alice! 25
Age: 32 year, name: Bob?
Age: 32 year, name: Charlie?
[root@cdh2 test]# CAT ForTest.scala
bash: CAT: command not found
[root@cdh2 test]# cat ForTest.scala
object ForTest {
   def main(args: Array[String]) {
   	val alice = new Person("Alice", 25)
        val bob = new Person("Bob", 32)
   	val charlie = new Person("Charlie", 32)
   
      for (person <- List(alice, bob, charlie)) {
         person match {
            case Person("Alice", 25) => println("Hi Alice! 25")
            case Person("Bob", 30) => println("Hi Bob! 30")
            case Person(name, age) =>
               println("Age: " + age + " year, name: " + name + "?")
         }
      }
   }
   // case class, empty one.
   case class Person(name: String, age: Int)
}
[root@cdh2 test]# vim ForTest.scala
[root@cdh2 test]# scalac -d classes/ ForTest.scala
[root@cdh2 test]# scala -classpath classes/ ForTest
Hi Alice! 25
Hi Bob! 30
Age: 32 year, name: Charlie?
[root@cdh2 test]# cat ForTest.scala
object ForTest {
   def main(args: Array[String]) {
   	val alice = new Person("Alice", 25)
        val bob = new Person("Bob", 30)
   	val charlie = new Person("Charlie", 32)
   
      for (person <- List(alice, bob, charlie)) {
         person match {
            case Person("Alice", 25) => println("Hi Alice! 25")
            case Person("Bob", 30) => println("Hi Bob! 30")
            case Person(name, age) =>
               println("Age: " + age + " year, name: " + name + "?")
         }
      }
   }
   // case class, empty one.
   case class Person(name: String, age: Int)
}
[root@cdh2 test]# ok 30
bash: ok: command not found
[root@cdh2 test]# scala -classpath classes/ ForTest
Hi Alice! 25
Hi Bob! 30
Age: 32 year, name: Charlie?
[root@cdh2 test]# cat ForTest.scala
object ForTest {
   def main(args: Array[String]) {
   	val alice = new Person("Alice", 25)
        val bob = new Person("Bob", 30)
   	val charlie = new Person("Charlie", 32)
   
      for (person <- List(alice, bob, charlie)) {
         person match {
            case Person("Alice", 25) => println("Hi Alice! 25")
            case Person("Bob", 30) => println("Hi Bob! 30")
            case Person(name, age) =>
               println("Age: " + age + " year, name: " + name + "?")
         }
      }
   }
   // case class, empty one.
   case class Person(name: String, age: Int)
}
[root@cdh2 test]# 


[root@cdh2 test]# 
[root@cdh2 test]# scalac -d classes/ RegexTest1.scala
[root@cdh2 test]# scala -classpath classes/ RegexTest1
Some(Scala)
[root@cdh2 test]# vim RegexTest2.scala
[root@cdh2 test]# scalac -d classes/ RegexTest2.scala
[root@cdh2 test]# scala -classpath classes/ RegexTest2
Exception in thread "main" java.lang.RuntimeException: Cannot figure out how to run target: RegexTest2
	at scala.sys.package$.error(package.scala:27)
	at scala.tools.nsc.GenericRunnerCommand.scala$tools$nsc$GenericRunnerCommand$$guessHowToRun(GenericRunnerCommand.scala:38)
	at scala.tools.nsc.GenericRunnerCommand$$anonfun$2.apply(GenericRunnerCommand.scala:48)
	at scala.tools.nsc.GenericRunnerCommand$$anonfun$2.apply(GenericRunnerCommand.scala:48)
	at scala.Option.getOrElse(Option.scala:108)
	at scala.tools.nsc.GenericRunnerCommand.<init>(GenericRunnerCommand.scala:48)
	at scala.tools.nsc.GenericRunnerCommand.<init>(GenericRunnerCommand.scala:17)
	at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:33)
	at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:89)
	at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)
[root@cdh2 test]# vim RegexTest2.scala

[9]+  Stopped                 vim RegexTest2.scala
[root@cdh2 test]# vim RegexTest2.scala
[root@cdh2 test]# scalac -d classes/ RegexTest2.scala
[root@cdh2 test]# scala -classpath classes/ RegexTest2
Scala,scala
[root@cdh2 test]# cat RegexTest2.scala
import scala.util.matching.Regex

object RegexTest2{
   def main(args: Array[String]) {
      val pattern = new Regex("(S|s)cala")
      val str = "Scala is scalable and cool"
      
      println((pattern findAllIn str).mkString(","))
   }
}
[root@cdh2 test]# scala -classpath classes/ RegexTest
Exception in thread "main" java.lang.RuntimeException: Cannot figure out how to run target: RegexTest
	at scala.sys.package$.error(package.scala:27)
	at scala.tools.nsc.GenericRunnerCommand.scala$tools$nsc$GenericRunnerCommand$$guessHowToRun(GenericRunnerCommand.scala:38)
	at scala.tools.nsc.GenericRunnerCommand$$anonfun$2.apply(GenericRunnerCommand.scala:48)
	at scala.tools.nsc.GenericRunnerCommand$$anonfun$2.apply(GenericRunnerCommand.scala:48)
	at scala.Option.getOrElse(Option.scala:108)
	at scala.tools.nsc.GenericRunnerCommand.<init>(GenericRunnerCommand.scala:48)
	at scala.tools.nsc.GenericRunnerCommand.<init>(GenericRunnerCommand.scala:17)
	at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:33)
	at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:89)
	at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)
[root@cdh2 test]# scalac -d classes/ RegexTest1.scala
[root@cdh2 test]# scala -classpath classes/ RegexTest1
Some(Scala)
[root@cdh2 test]# cat RegexTest1.scala
import scala.util.matching.Regex

object RegexTest1 {
   def main(args: Array[String]) {
      val pattern = "Scala".r
      val str = "Scala is Scalable and cool"
      
      println(pattern findFirstIn str)
   }
}
[root@cdh2 test]# 



import scala.util.matching.Regex

object RegexTest1 {
   def main(args: Array[String]) {
      val pattern = "Scala".r
      val str = "Scala is Scalable and cool"
      
      println(pattern findFirstIn str)
   }
}
[root@cdh2 test]# scala -classpath classes/ RegexTest1
Some(Scala)
[root@cdh2 test]# vim RegexTest1.scala
[root@cdh2 test]# scalac -d classes/ RegexTest1.scala
[root@cdh2 test]# scala -classpath classes/ RegexTest1
Some(Scala)
non-empty iterator
[root@cdh2 test]# scala -classpath classes/ RegexTest2
Scala,scala
[root@cdh2 test]# scalac -d classes/ RegexTest2.scala
[root@cdh2 test]# scala -classpath classes/ RegexTest2
Scala,scala
[root@cdh2 test]# cat RegexTest2.scala
import scala.util.matching.Regex

object RegexTest2{
   def main(args: Array[String]) {
      val pattern = new Regex("(S|s)cala")
      val str = "Scala is scalable and cool"
      
      println((pattern findAllIn str).mkString(","))
   }
}
[root@cdh2 test]# vim RegexTest1.scala
[root@cdh2 test]# scalac -d classes/ RegexTest1.scala
[root@cdh2 test]# scala -classpath classes/ RegexTest1
Some(Scala)
Scala,Scala
[root@cdh2 test]# scala -classpath classes/ RegexTest2
Scala,scala
[root@cdh2 test]# cat RegexTest1.scala
import scala.util.matching.Regex

object RegexTest1 {
   def main(args: Array[String]) {
      val pattern = "Scala".r
      val str = "Scala is Scalable and cool"
      
      println(pattern findFirstIn str)
      println((pattern findAllIn str).mkString(","))
   }
}
[root@cdh2 test]# cat RegexTest2.scala
import scala.util.matching.Regex

object RegexTest2{
   def main(args: Array[String]) {
      val pattern = new Regex("(S|s)cala")
      val str = "Scala is scalable and cool"
      
      println((pattern findAllIn str).mkString(","))
   }
}
[root@cdh2 test]# vim RegexReplace.scala
[root@cdh2 test]# vim RegexReplace.scala
[root@cdh2 test]# scalac -d classes/ RegexReplace.scala 
[root@cdh2 test]# scala -classpath classes/ RegexReplace
old str Scala is scalable and cool
Java is scalable and cool
[root@cdh2 test]# cat RegexReplace.scala
object RegexReplace {
   def main(args: Array[String]) {
      val pattern = "(S|s)cala".r
      val str = "Scala is scalable and cool"
      
      println("old str "+str)
      println(pattern replaceFirstIn(str, "Java"))
   }
}
[root@cdh2 test]# vim RegexD.scala
[root@cdh2 test]# scalac -d classes/ RegexD.scala

[root@cdh2 test]# 
[root@cdh2 test]# scala -classpath classes/ RegexD


[root@cdh2 test]# scalac -d classes/ RegexD.scala
[root@cdh2 test]# scala -classpath classes RegexD


[root@cdh2 test]# cat RegexD.scala
import scala.util.matching.Regex

object RegexD{
   def main(args: Array[String]) {
      val pattern = new Regex("abl[ae]d+")
      val str  = "ablaw is able1 and cool"
      val str1 = "ablaw is able1 and cool abla1 abla0 ablaw "
      println((pattern findAllIn str).mkString(","))
      println((pattern findAllIn str1).mkString(","))
   }
}
[root@cdh2 test]# vim RegexD.scala
[root@cdh2 test]# vim RegexD.scala
[root@cdh2 test]# scalac -d classes/ RegexD.scala
[root@cdh2 test]# scala -classpath classes RegexD

[root@cdh2 test]# scalac -d classes/ RegexReplace.scala 
[root@cdh2 test]# scala -classpath classes/ RegexReplace
old str Scala is scalable and cool
Java is scalable and cool
[root@cdh2 test]# scalac -d classes/ RegexD.scala 
[root@cdh2 test]# scala -classpath classes/ RegexD

[root@cdh2 test]# vim RegexD.scala
[root@cdh2 test]# scalac -d classes/ RegexD.scala 
[root@cdh2 test]# scala -classpath classes/ RegexD

[root@cdh2 test]# vim RegexD1.scala
[root@cdh2 test]# scalac -d classes/ RegexD1.scala 
[root@cdh2 test]# scala -classpath classes/ RegexD1

[root@cdh2 test]# vim RegexD1.scala
[root@cdh2 test]# scalac -d classes/ RegexD1.scala 
[root@cdh2 test]# scala -classpath classes/ RegexD1
abla,able
[root@cdh2 test]# cat RegexD1.scala
import scala.util.matching.Regex

object RegexD1 {
   def main(args: Array[String]) {
      val pattern = new Regex("abl[ae]")
      val str = "abla is able and cool"
      
      println((pattern findAllIn str).mkString(","))
   }
}
[root@cdh2 test]# cp RegexD1.scala RegexD2.scala
[root@cdh2 test]# vim RegexD2.scala
[root@cdh2 test]# scalac -d classes/ RegexD2.scala 
[root@cdh2 test]# scala -classpath classes/ RegexD2

[root@cdh2 test]# vim RegexD2.scala
[root@cdh2 test]# scalac -d classes/ RegexD2.scala 
[root@cdh2 test]# scala -classpath classes/ RegexD2

[root@cdh2 test]# 正则表达式 没完全搞定


[root@cdh2 test]# ll
total 76
-rw-r--r-- 1 root root  330 Jul  4 19:45 CaseTest2.scala
-rw-r--r-- 1 root root  354 Jul  4 20:07 CaseTest3.scala
-rw-r--r-- 1 root root  263 Jul  4 19:38 CaseTest.scala
drwxr-xr-x 2 root root 4096 Jul  4 22:15 classes
-rw-r--r-- 1 root root  585 Jul  4 20:15 ForTest.scala
-rw-r--r-- 1 root root  104 Jul  4 18:44 HelloWorld2.scala
-rw-r--r-- 1 root root   88 Jul  4 04:18 HelloWorld.scala
-rwxr-xr-x 1 root root  193 Jul  4 04:06 person.sh
-rw-r--r-- 1 root root  234 Jul  4 22:12 RegexD1.scala
-rw-r--r-- 1 root root  250 Jul  4 22:18 RegexD2.scala
-rw-r--r-- 1 root root  349 Jul  4 22:07 RegexD.scala
-rw-r--r-- 1 root root  231 Jul  4 21:54 RegexReplace.scala
-rw-r--r-- 1 root root  270 Jul  4 21:48 RegexTest1.scala
-rw-r--r-- 1 root root  243 Jul  4 20:49 RegexTest2.scala
-rwxr-xr-x 1 root root  166 Jul  4 04:13 script.sh
-rw-r--r-- 1 root root  769 Jul  4 19:07 Test2.scala
-rw-r--r-- 1 root root  426 Jul  4 19:13 Test3.scala
-rw-r--r-- 1 root root  493 Jul  4 19:00 Test.scala
-rw-r--r-- 1 root root  815 Jul  4 19:31 TraitTest.scala
[root@cdh2 test]# vim File.scala
[root@cdh2 test]# scalac -d classes/ F
File.scala     ForTest.scala  
[root@cdh2 test]# scalac -d classes/ F
File.scala     ForTest.scala  
[root@cdh2 test]# scalac -d classes/ File.scala 
[root@cdh2 test]# scala -classpath classes/ File
Missing file exception
[root@cdh2 test]# cat File.scala
import java.io.FileReader
import java.io.FileNotFoundException
import java.io.IOException

object File {
   def main(args: Array[String]) {
      try {
         val f = new FileReader("input.txt")
      } catch {
         case ex: FileNotFoundException =>{
            println("Missing file exception")
         }
         case ex: IOException => {
            println("IO Exception")
         }
      }
   }
}
[root@cdh2 test]# vim File1.scala

[10]+  Stopped                 vim File1.scala
[root@cdh2 test]# cp File.scala File1.scala
[root@cdh2 test]# ll
total 84
-rw-r--r-- 1 root root  330 Jul  4 19:45 CaseTest2.scala
-rw-r--r-- 1 root root  354 Jul  4 20:07 CaseTest3.scala
-rw-r--r-- 1 root root  263 Jul  4 19:38 CaseTest.scala
drwxr-xr-x 2 root root 4096 Jul  4 23:33 classes
-rw-r--r-- 1 root root  411 Jul  4 23:35 File1.scala
-rw-r--r-- 1 root root  411 Jul  4 23:32 File.scala
-rw-r--r-- 1 root root  585 Jul  4 20:15 ForTest.scala
-rw-r--r-- 1 root root  104 Jul  4 18:44 HelloWorld2.scala
-rw-r--r-- 1 root root   88 Jul  4 04:18 HelloWorld.scala
-rwxr-xr-x 1 root root  193 Jul  4 04:06 person.sh
-rw-r--r-- 1 root root  234 Jul  4 22:12 RegexD1.scala
-rw-r--r-- 1 root root  250 Jul  4 22:18 RegexD2.scala
-rw-r--r-- 1 root root  349 Jul  4 22:07 RegexD.scala
-rw-r--r-- 1 root root  231 Jul  4 21:54 RegexReplace.scala
-rw-r--r-- 1 root root  270 Jul  4 21:48 RegexTest1.scala
-rw-r--r-- 1 root root  243 Jul  4 20:49 RegexTest2.scala
-rwxr-xr-x 1 root root  166 Jul  4 04:13 script.sh
-rw-r--r-- 1 root root  769 Jul  4 19:07 Test2.scala
-rw-r--r-- 1 root root  426 Jul  4 19:13 Test3.scala
-rw-r--r-- 1 root root  493 Jul  4 19:00 Test.scala
-rw-r--r-- 1 root root  815 Jul  4 19:31 TraitTest.scala
[root@cdh2 test]# vim File1.scala
[root@cdh2 test]# scalac -d classes/ File1.scala 
[root@cdh2 test]# scala -classpath classes/ File1
Exception in thread "main" java.lang.RuntimeException: Cannot figure out how to run target: File1
	at scala.sys.package$.error(package.scala:27)
	at scala.tools.nsc.GenericRunnerCommand.scala$tools$nsc$GenericRunnerCommand$$guessHowToRun(GenericRunnerCommand.scala:38)
	at scala.tools.nsc.GenericRunnerCommand$$anonfun$2.apply(GenericRunnerCommand.scala:48)
	at scala.tools.nsc.GenericRunnerCommand$$anonfun$2.apply(GenericRunnerCommand.scala:48)
	at scala.Option.getOrElse(Option.scala:108)
	at scala.tools.nsc.GenericRunnerCommand.<init>(GenericRunnerCommand.scala:48)
	at scala.tools.nsc.GenericRunnerCommand.<init>(GenericRunnerCommand.scala:17)
	at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:33)
	at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:89)
	at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)
[root@cdh2 test]# vim File1.scala
[root@cdh2 test]# scalac -d classes/ File1.scala 
[root@cdh2 test]# scala -classpath classes/ File1
Missing file exception
Exiting finally...
[root@cdh2 test]# cat File1.scala
import java.io.FileReader
import java.io.FileNotFoundException
import java.io.IOException

object File1 {
   def main(args: Array[String]) {
      try {
         val f = new FileReader("input.txt")
      } catch {
         case ex: FileNotFoundException =>{
            println("Missing file exception")
         }
         case ex: IOException => {
            println("IO Exception")
         }
      }
      finally {
         println("Exiting finally...")
      }
   }
}
[root@cdh2 test]# cp File1.scala File2.scala
[root@cdh2 test]# vim File2.scala
[root@cdh2 test]# scalac -d classes/ File2.scala 
[root@cdh2 test]# scala -classpath classes/ File2
Missing file exception
---Missing file exception--java.io.FileNotFoundException: input.txt (No such file or directory)
Exiting finally...
[root@cdh2 test]# cat File2.scala
import java.io.FileReader
import java.io.FileNotFoundException
import java.io.IOException

object File2 {
   def main(args: Array[String]) {
      try {
         val f = new FileReader("input.txt")
      } catch {
         case ex: FileNotFoundException =>{
            println("Missing file exception")
            println("---Missing file exception--"+ex)
         }
         case ex: IOException => {
            println("IO Exception")
         }
[root@cdh2 test]# scalac -d classes/ Some.scala 
Some.scala:18: error: type mismatch;
 found   : java.lang.String
 required: Option[(String, String)]
         Some(parts(0), parts(1)) 
             ^
one error found
[root@cdh2 test]# less Some.scala
[root@cdh2 test]# cat Some.scala
object Some {
   def main(args: Array[String]) {
      
      println ("Apply method : " + apply("Zara", "gmail.com"));
      println ("Unapply method : " + unapply("Zara@gmail.com"));
      println ("Unapply method : " + unapply("Zara Ali"));

   }
   // The injection method (optional)
   def apply(user: String, domain: String) = {
      user +"@"+ domain
   }

   // The extraction method (mandatory)
   def unapply(str: String): Option[(String, String)] = {
      val parts = str split "@"
      if (parts.length == 2){
         Some(parts(0), parts(1)) 
      }else{
         None
      }
   }
}
[root@cdh2 test]# cp Some.scala
cp: missing destination file operand after `Some.scala'
Try `cp --help' for more information.
[root@cdh2 test]# cp Some.scala Some1.scala 
[root@cdh2 test]# vim Some.scala
[root@cdh2 test]# scalac -d classes/ Some.scala
[root@cdh2 test]# scala -classpath classes/ Some 
Apply method : Zara@gmail.com
Unapply method : Some((Zara,gmail.com))
Unapply method : None
[root@cdh2 test]# scalac -d classes/ Some.scala
[root@cdh2 test]# scala -classpath classes/ Some 
Apply method : Zara@gmail.com
Unapply method : Some((Zara,gmail.com))
Unapply method : None
[root@cdh2 test]# cat Some.scala
object Some {
   def main(args: Array[String]) {
      
      println ("Apply method : " + apply("Zara", "gmail.com"));
      println ("Unapply method : " + unapply("Zara@gmail.com"));
      println ("Unapply method : " + unapply("Zara Ali"));

   }
   // The injection method (optional)
   def apply(user: String, domain: String) = {
      user +"@"+ domain
   }

   // The extraction method (mandatory)
   def unapply(str: String): Option[(String, String)] = {
      val parts = str split "@"
      if (parts.length == 2){
         //Some(parts(0), parts(1))
	     Option(parts(0), parts(1)) 
      }else{
         None
      }
   }
}
[root@cdh2 test]# 



[root@cdh2 test]# scala -classpath classes/ Some 
Apply method : Zara@gmail.com
Unapply method : Some((Zara,gmail.com))
Unapply method : None
[root@cdh2 test]# vim TestExtractor.scala
[root@cdh2 test]# scalac -d classes/ TestExtractor.scala
[root@cdh2 test]# scala -classpath classes/ TestExtractor
10
10 is bigger two times than 5
[root@cdh2 test]# cat TestExtractor.scala
object TestExtractor {
   def main(args: Array[String]) {
      
      val x = TestExtractor(5)
      println(x)

      x match
      {
         case TestExtractor(num) => println(x+" is bigger two times than "+num)
         //unapply is invoked
         case _ => println("i cannot calculate")
      }

   }
   def apply(x: Int) = x*2
   def unapply(z: Int): Option[Int] = if (z%2==0) Some(z/2) else None
}
[root@cdh2 test]#  这个怎么运行的没搞定诶 


Hello Scala[root@cdh2 test]# 
[root@cdh2 test]# cat test.txt
Hello Scala[root@cdh2 test]# 
[root@cdh2 test]# cat FileWR.scala
import java.io._

object FileWR{
   def main(args: Array[String]) {
      val writer = new PrintWriter(new File("test.txt" ))

      writer.write("Hello Scala")
      writer.close()
   }
}
[root@cdh2 test]# 
[root@cdh2 test]# 
[root@cdh2 test]# vim Console.scala
[root@cdh2 test]# scalac -d classes/ Console.scala
Console.scala:4: error: value readLine is not a member of object Console
      val line = Console.readLine
                         ^
one error found
[root@cdh2 test]# vim Console.scala
[root@cdh2 test]# scalac -d classes/ Console.scala
Console.scala:4: error: value readLine is not a member of object Console
      val line = Console.readLine()
                         ^
one error found
[root@cdh2 test]# vim Console.scala

[11]+  Stopped                 vim Console.scala
[root@cdh2 test]# vim Source.scala
[root@cdh2 test]# scalac -d classes/ Source.scala 
Source.scala:1: warning: imported `Source' is permanently hidden by definition of object Source
import scala.io.Source
       ^
Source.scala:7: error: value fromFile is not a member of object Source
      Source.fromFile("test.txt" ).foreach{ 
             ^
one warning found
one error found
[root@cdh2 test]# scala -version
Scala code runner version 2.9.3 -- Copyright 2002-2011, LAMP/EPFL
[root@cdh2 test]# scalac -d classes/ Console.scala
Console.scala:4: error: value readLine is not a member of object Console
      val line = Console.readLine()
                         ^
one error found
[root@cdh2 test]# vim Console.scala
[root@cdh2 test]# mv Console.scala ConsoleTest.scala 
[root@cdh2 test]# scalac -d classes/ ConsoleTest.scala
[root@cdh2 test]# scala -classpath classes/ ConsoleTest
Please enter your input : cons test
Thanks, you just typed: cons test
[root@cdh2 test]# scala -classpath classes/ ConsoleTest
Please enter your input : objectName shouldn't the same as Console.API                        
Thanks, you just typed: objectName shouldn't the same as Console.API
[root@cdh2 test]# cat ConsoleTest.scala
object ConsoleTest{
   def main(args: Array[String]) {
      print("Please enter your input : " )
      val line = Console.readLine()
      
      println("Thanks, you just typed: " + line)
   }
}
[root@cdh2 test]# vim Source.scala 
[root@cdh2 test]# mv Source.scala SourceTest.scala 
[root@cdh2 test]# scalac -d classes/ SourceTest.scala
[root@cdh2 test]# scala -classpath classes/ SourceTest
Following is the content read:
Hello Scala[root@cdh2 test]# ll
total 120
drwxr-xr-x 2 root root 4096 Jul  5 00:58 bak
-rw-r--r-- 1 root root  330 Jul  4 19:45 CaseTest2.scala
-rw-r--r-- 1 root root  354 Jul  4 20:07 CaseTest3.scala
-rw-r--r-- 1 root root  263 Jul  4 19:38 CaseTest.scala
drwxr-xr-x 2 root root 4096 Jul  5 01:56 classes
-rw-r--r-- 1 root root  197 Jul  5 01:51 ConsoleTest.scala
-rw-r--r-- 1 root root  475 Jul  4 23:37 File1.scala
-rw-r--r-- 1 root root  529 Jul  4 23:40 File2.scala
-rw-r--r-- 1 root root  411 Jul  4 23:32 File.scala
-rw-r--r-- 1 root root  189 Jul  5 01:26 FileWR.scala
-rw-r--r-- 1 root root  585 Jul  4 20:15 ForTest.scala
-rw-r--r-- 1 root root  104 Jul  4 18:44 HelloWorld2.scala
-rw-r--r-- 1 root root   88 Jul  4 04:18 HelloWorld.scala
-rwxr-xr-x 1 root root  193 Jul  4 04:06 person.sh
-rw-r--r-- 1 root root  234 Jul  4 22:12 RegexD1.scala
-rw-r--r-- 1 root root  250 Jul  4 22:18 RegexD2.scala
-rw-r--r-- 1 root root  349 Jul  4 22:07 RegexD.scala
-rw-r--r-- 1 root root  231 Jul  4 21:54 RegexReplace.scala
-rw-r--r-- 1 root root  270 Jul  4 21:48 RegexTest1.scala
-rw-r--r-- 1 root root  243 Jul  4 20:49 RegexTest2.scala
-rwxr-xr-x 1 root root  166 Jul  4 04:13 script.sh
-rw-r--r-- 1 root root  602 Jul  5 00:00 Some1.scala
-rw-r--r-- 1 root root  633 Jul  5 00:01 Some.scala
-rw-r--r-- 1 root root  205 Jul  5 01:55 SourceTest.scala
-rw-r--r-- 1 root root  769 Jul  4 19:07 Test2.scala
-rw-r--r-- 1 root root  426 Jul  4 19:13 Test3.scala
-rw-r--r-- 1 root root  408 Jul  5 00:08 TestExtractor.scala
-rw-r--r-- 1 root root  493 Jul  4 19:00 Test.scala
-rw-r--r-- 1 root root   11 Jul  5 01:27 test.txt
-rw-r--r-- 1 root root  815 Jul  4 19:31 TraitTest.scala
[root@cdh2 test]# cat SourceTest.scala 
import scala.io.Source

object SourceTest {
   def main(args: Array[String]) {
      println("Following is the content read:" )

      Source.fromFile("test.txt" ).foreach{ 
         print 
      }
   }
}
[root@cdh2 test]# cat test.txt
Hello Scala[root@cdh2 test]# vim test.txt
[root@cdh2 test]# cat test.txt
Hello Scala
lyc  sty scala
[root@cdh2 test]# scala -classpath classes/ SourceTest
Following is the content read:
Hello Scala
lyc  sty scala
[root@cdh2 test]# cat SourceTest.scala 
import scala.io.Source

object SourceTest {
   def main(args: Array[String]) {
      println("Following is the content read:" )

      Source.fromFile("test.txt" ).foreach{ 
         print 
      }
   }
}
[root@cdh2 test]# cat ConsoleTest.scala
object ConsoleTest{
   def main(args: Array[String]) {
      print("Please enter your input : " )
      val line = Console.readLine()
      
      println("Thanks, you just typed: " + line)
   }
}
[root@cdh2 test]# cp ConsoleTestcala ConsoleTes.scala 
cp: cannot stat `ConsoleTestcala': No such file or directory
[root@cdh2 test]# cp ConsoleTest.cala ConsoleTes.scala 
cp: cannot stat `ConsoleTest.cala': No such file or directory
[root@cdh2 test]# cp ConsoleTest.scala ConsoleTes.scala 
[root@cdh2 test]# vim ConsoleTes.scala
[root@cdh2 test]# scala -classpath classes/ ConsoleTes
Exception in thread "main" java.lang.RuntimeException: Cannot figure out how to run target: ConsoleTes
	at scala.sys.package$.error(package.scala:27)
	at scala.tools.nsc.GenericRunnerCommand.scala$tools$nsc$GenericRunnerCommand$$guessHowToRun(GenericRunnerCommand.scala:38)
	at scala.tools.nsc.GenericRunnerCommand$$anonfun$2.apply(GenericRunnerCommand.scala:48)
	at scala.tools.nsc.GenericRunnerCommand$$anonfun$2.apply(GenericRunnerCommand.scala:48)
	at scala.Option.getOrElse(Option.scala:108)
	at scala.tools.nsc.GenericRunnerCommand.<init>(GenericRunnerCommand.scala:48)
	at scala.tools.nsc.GenericRunnerCommand.<init>(GenericRunnerCommand.scala:17)
	at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:33)
	at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:89)
	at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)
[root@cdh2 test]# scalac -d classes/ ConsoleTes.scala
[root@cdh2 test]# scala -classpath classes/ ConsoleTes
Please enter your input in readLine() : read line minus ()  
Thanks, you just typed: read line minus ()
[root@cdh2 test]# cat ConsoleTes.scala
object ConsoleTes{
   def main(args: Array[String]) {
      print("Please enter your input in readLine() : " )
      //val line = Console.readLine()
      val line = Console.readLine
      println("Thanks, you just typed: " + line)
   }
}
[root@cdh2 test]# vim ArrayTest.scala
[root@cdh2 test]# scalac -d classes/ ArrayTest.scala 
[root@cdh2 test]# scala -classpath classes/ ArrayTest
1.9
2.9
3.4
3.5

1.9
2.9
3.4
3.5
Total is 11.7
Max is 3.5
[root@cdh2 test]# cat ArrayTest.scala
object ArrayTest {
   def main(args: Array[String]) {
      var myList = Array(1.9, 2.9, 3.4, 3.5)
      
      // Print all the array elements
      for ( x <- myList ) {
         println( x )
      }
	println
      myList.foreach{
	println
      }

      // Summing all elements
      var total = 0.0;
      for ( i <- 0 to (myList.length - 1)) {
         total += myList(i);
      }
      println("Total is " + total);

      // Finding the largest element
      var max = myList(0);
      for ( i <- 1 to (myList.length - 1) ) {
         if (myList(i) > max) max = myList(i);
      }
      println("Max is " + max);
    
   }
}
[root@cdh2 test]# vim Matrix.scala
[root@cdh2 test]# mv Matrix.scala MatrixTest.scala 
[root@cdh2 test]# scalac -d classes/ MatrixTest.scala 
[root@cdh2 test]# scala -classpath classes/ MatrixTest
 0 1 2
 0 1 2
 0 1 2
[root@cdh2 test]# scala -classpath classes/ MatrixTest
 0 1 2
 0 1 2
 0 1 2
[root@cdh2 test]# cat Matrix.scala
cat: Matrix.scala: No such file or directory
[root@cdh2 test]# cat MatrixTest.scala 
import Array._

object MatrixTest {
   def main(args: Array[String]) {
      var myMatrix = ofDim[Int](3,3)
      
      // build a matrix
      for (i <- 0 to 2) {
         for ( j <- 0 to 2) {
            myMatrix(i)(j) = j;
         }
      }
      
      // Print two dimensional array
      for (i <- 0 to 2) {
         for ( j <- 0 to 2) {
            print(" " + myMatrix(i)(j));
         }
         println();
      }
    
   }
}
[root@cdh2 test]# vim ConcatArray.scala
[root@cdh2 test]# scalac -d classes/ Con
ConcatArray.scala  ConsoleTes.scala   ConsoleTest.scala  
[root@cdh2 test]# scalac -d classes/ ConcatArray.scala 
[root@cdh2 test]# scala -classpath classes/ ConcatArray
1.9
2.9
3.4
3.5
8.9
7.9
0.4
1.5
8
[root@cdh2 test]# cat ConcatArray.scala
import Array._

object ConcatArray {
   def main(args: Array[String]) {
      var myList1 = Array(1.9, 2.9, 3.4, 3.5)
      var myList2 = Array(8.9, 7.9, 0.4, 1.5)

      var myList3 =  concat( myList1, myList2)
      
      // Print all the array elements
      for ( x <- myList3 ) {
         println( x )
      }
      println(myList3.size)
   }
}
[root@cdh2 test]# vim RangeArray.scala
[root@cdh2 test]# scalac -d classes/ RangeArray.scala 
RangeArray.scala:3: error: not found: value range
      var myList1 = range(10, 20, 2)
                    ^
RangeArray.scala:4: error: not found: value range
      var myList2 = range(10,20)
                    ^
RangeArray.scala:5: error: not found: value range
	  var myList3 = range(10, 20, 3)
                        ^
three errors found
[root@cdh2 test]# scalac -d classes/ RangeArray.scala 
RangeArray.scala:3: error: not found: value range
      var myList1 = range(10, 20, 2)
                    ^
RangeArray.scala:4: error: not found: value range
      var myList2 = range(10,20)
                    ^
RangeArray.scala:5: error: not found: value range
	  var myList3 = range(10, 20, 3)
                        ^
three errors found
[root@cdh2 test]# vim RangeArray.scala
[root@cdh2 test]# scalac -d classes/ RangeArray.scala
[root@cdh2 test]# scalac -classpath classes/ RangeArray
error: source file 'RangeArray' could not be found
one error found
[root@cdh2 test]# ls -l classes/
total 304
-rw-r--r-- 1 root root 1184 Jul  5 02:16 ArrayTest$$anonfun$main$1.class
-rw-r--r-- 1 root root 1325 Jul  5 02:16 ArrayTest$$anonfun$main$2.class
-rw-r--r-- 1 root root 1338 Jul  5 02:16 ArrayTest$$anonfun$main$3.class
-rw-r--r-- 1 root root  899 Jul  5 02:16 ArrayTest$$anonfun$main$4.class
-rw-r--r-- 1 root root  809 Jul  5 02:16 ArrayTest.class
-rw-r--r-- 1 root root 2322 Jul  5 02:16 ArrayTest$.class
-rw-r--r-- 1 root root  779 Jul  4 19:45 CaseTest2.class
-rw-r--r-- 1 root root 1138 Jul  4 19:45 CaseTest2$.class
-rw-r--r-- 1 root root  779 Jul  4 20:07 CaseTest3.class
-rw-r--r-- 1 root root 1138 Jul  4 20:07 CaseTest3$.class
-rw-r--r-- 1 root root  773 Jul  4 19:42 CaseTest.class
-rw-r--r-- 1 root root 1118 Jul  4 19:42 CaseTest$.class
-rw-r--r-- 1 root root 1192 Jul  5 02:27 ConcatArray$$anonfun$main$1.class
-rw-r--r-- 1 root root  707 Jul  5 02:27 ConcatArray.class
-rw-r--r-- 1 root root 1744 Jul  5 02:27 ConcatArray$.class
-rw-r--r-- 1 root root  637 Jul  5 02:03 ConsoleTes.class
-rw-r--r-- 1 root root 1004 Jul  5 02:03 ConsoleTes$.class
-rw-r--r-- 1 root root  643 Jul  5 01:51 ConsoleTest.class
-rw-r--r-- 1 root root  993 Jul  5 01:51 ConsoleTest$.class
-rw-r--r-- 1 root root  559 Jul  4 19:31 Equal.class
-rw-r--r-- 1 root root  465 Jul  4 19:31 Equal$class.class
-rw-r--r-- 1 root root  605 Jul  4 23:38 File1.class
-rw-r--r-- 1 root root  952 Jul  4 23:38 File1$.class
-rw-r--r-- 1 root root  605 Jul  4 23:40 File2.class
-rw-r--r-- 1 root root 1189 Jul  4 23:40 File2$.class
-rw-r--r-- 1 root root  600 Jul  4 23:37 File.class
-rw-r--r-- 1 root root  950 Jul  4 23:37 File$.class
-rw-r--r-- 1 root root  611 Jul  5 01:27 FileWR.class
-rw-r--r-- 1 root root  728 Jul  5 01:27 FileWR$.class
-rw-r--r-- 1 root root 1815 Jul  4 20:15 ForTest$$anonfun$main$1.class
-rw-r--r-- 1 root root 1707 Jul  4 20:15 ForTest.class
-rw-r--r-- 1 root root 1274 Jul  4 20:15 ForTest$.class
-rw-r--r-- 1 root root 2423 Jul  4 20:15 ForTest$Person.class
-rw-r--r-- 1 root root 1562 Jul  4 20:15 ForTest$Person$.class
-rw-r--r-- 1 root root  637 Jul  4 04:18 HelloWorld.class
-rw-r--r-- 1 root root  606 Jul  4 04:18 HelloWorld$.class
-rw-r--r-- 1 root root 1967 Jul  4 19:07 Location.class
-rw-r--r-- 1 root root 1398 Jul  5 02:22 MatrixTest$$anonfun$main$1$$anonfun$apply$mcVI$sp$1.class
-rw-r--r-- 1 root root 1618 Jul  5 02:22 MatrixTest$$anonfun$main$1.class
-rw-r--r-- 1 root root 1755 Jul  5 02:22 MatrixTest$$anonfun$main$2$$anonfun$apply$mcVI$sp$2.class
-rw-r--r-- 1 root root 1648 Jul  5 02:22 MatrixTest$$anonfun$main$2.class
-rw-r--r-- 1 root root  740 Jul  5 02:22 MatrixTest.class
-rw-r--r-- 1 root root 1420 Jul  5 02:22 MatrixTest$.class
-rw-r--r-- 1 root root 1385 Jul  5 03:19 RangeArray$$anonfun$main$1.class
-rw-r--r-- 1 root root 1385 Jul  5 03:19 RangeArray$$anonfun$main$2.class
-rw-r--r-- 1 root root  901 Jul  5 03:19 RangeArray$$anonfun$main$3.class
-rw-r--r-- 1 root root  780 Jul  5 03:19 RangeArray.class
-rw-r--r-- 1 root root 1282 Jul  5 03:19 RangeArray$.class
-rw-r--r-- 1 root root  618 Jul  4 22:12 RegexD1.class
-rw-r--r-- 1 root root 1262 Jul  4 22:12 RegexD1$.class
-rw-r--r-- 1 root root  618 Jul  4 22:18 RegexD2.class
-rw-r--r-- 1 root root 1278 Jul  4 22:18 RegexD2$.class
-rw-r--r-- 1 root root  611 Jul  4 22:07 RegexD.class
-rw-r--r-- 1 root root 1263 Jul  4 22:07 RegexD$.class
-rw-r--r-- 1 root root  649 Jul  4 22:05 RegexReplace.class
-rw-r--r-- 1 root root 1284 Jul  4 22:05 RegexReplace$.class
-rw-r--r-- 1 root root  637 Jul  4 21:48 RegexTest1.class
-rw-r--r-- 1 root root 1324 Jul  4 21:48 RegexTest1$.class
-rw-r--r-- 1 root root  637 Jul  4 21:46 RegexTest2.class
-rw-r--r-- 1 root root 1278 Jul  4 21:46 RegexTest2$.class
-rw-r--r-- 1 root root 1052 Jul  5 00:02 Some.class
-rw-r--r-- 1 root root 1762 Jul  5 00:02 Some$.class
-rw-r--r-- 1 root root  901 Jul  5 01:56 SourceTest$$anonfun$main$1.class
-rw-r--r-- 1 root root  700 Jul  5 01:56 SourceTest.class
-rw-r--r-- 1 root root 1101 Jul  5 01:56 SourceTest$.class
-rw-r--r-- 1 root root  605 Jul  4 19:07 Test2.class
-rw-r--r-- 1 root root  579 Jul  4 19:07 Test2$.class
-rw-r--r-- 1 root root  605 Jul  4 19:14 Test3.class
-rw-r--r-- 1 root root 1137 Jul  4 19:14 Test3$.class
-rw-r--r-- 1 root root  605 Jul  4 20:48 Test.class
-rw-r--r-- 1 root root 1266 Jul  4 20:48 Test$.class
-rw-r--r-- 1 root root  949 Jul  5 00:09 TestExtractor.class
-rw-r--r-- 1 root root 1540 Jul  5 00:09 TestExtractor$.class
-rw-r--r-- 1 root root  630 Jul  4 19:31 TraitTest.class
-rw-r--r-- 1 root root 1163 Jul  4 19:31 TraitTest$.class
-rw-r--r-- 1 root root 1480 Jul  4 19:31 Yiibai.class
[root@cdh2 test]# vim RangeArray.scala
[root@cdh2 test]# scalac -d classes/ RangeArray.scala
[root@cdh2 test]# scala -classpath classes/ RangeArray
 10 12 14 16 18
 10 11 12 13 14 15 16 17 18 19
10131619[root@cdh2ls -l classes/ngeArray
total 304
-rw-r--r-- 1 root root 1184 Jul  5 02:16 ArrayTest$$anonfun$main$1.class
-rw-r--r-- 1 root root 1325 Jul  5 02:16 ArrayTest$$anonfun$main$2.class
-rw-r--r-- 1 root root 1338 Jul  5 02:16 ArrayTest$$anonfun$main$3.class
-rw-r--r-- 1 root root  899 Jul  5 02:16 ArrayTest$$anonfun$main$4.class
-rw-r--r-- 1 root root  809 Jul  5 02:16 ArrayTest.class
-rw-r--r-- 1 root root 2322 Jul  5 02:16 ArrayTest$.class
-rw-r--r-- 1 root root  779 Jul  4 19:45 CaseTest2.class
-rw-r--r-- 1 root root 1138 Jul  4 19:45 CaseTest2$.class
-rw-r--r-- 1 root root  779 Jul  4 20:07 CaseTest3.class
-rw-r--r-- 1 root root 1138 Jul  4 20:07 CaseTest3$.class
-rw-r--r-- 1 root root  773 Jul  4 19:42 CaseTest.class
-rw-r--r-- 1 root root 1118 Jul  4 19:42 CaseTest$.class
-rw-r--r-- 1 root root 1192 Jul  5 02:27 ConcatArray$$anonfun$main$1.class
-rw-r--r-- 1 root root  707 Jul  5 02:27 ConcatArray.class
-rw-r--r-- 1 root root 1744 Jul  5 02:27 ConcatArray$.class
-rw-r--r-- 1 root root  637 Jul  5 02:03 ConsoleTes.class
-rw-r--r-- 1 root root 1004 Jul  5 02:03 ConsoleTes$.class
-rw-r--r-- 1 root root  643 Jul  5 01:51 ConsoleTest.class
-rw-r--r-- 1 root root  993 Jul  5 01:51 ConsoleTest$.class
-rw-r--r-- 1 root root  559 Jul  4 19:31 Equal.class
-rw-r--r-- 1 root root  465 Jul  4 19:31 Equal$class.class
-rw-r--r-- 1 root root  605 Jul  4 23:38 File1.class
-rw-r--r-- 1 root root  952 Jul  4 23:38 File1$.class
-rw-r--r-- 1 root root  605 Jul  4 23:40 File2.class
-rw-r--r-- 1 root root 1189 Jul  4 23:40 File2$.class
-rw-r--r-- 1 root root  600 Jul  4 23:37 File.class
-rw-r--r-- 1 root root  950 Jul  4 23:37 File$.class
-rw-r--r-- 1 root root  611 Jul  5 01:27 FileWR.class
-rw-r--r-- 1 root root  728 Jul  5 01:27 FileWR$.class
-rw-r--r-- 1 root root 1815 Jul  4 20:15 ForTest$$anonfun$main$1.class
-rw-r--r-- 1 root root 1707 Jul  4 20:15 ForTest.class
-rw-r--r-- 1 root root 1274 Jul  4 20:15 ForTest$.class
-rw-r--r-- 1 root root 2423 Jul  4 20:15 ForTest$Person.class
-rw-r--r-- 1 root root 1562 Jul  4 20:15 ForTest$Person$.class
-rw-r--r-- 1 root root  637 Jul  4 04:18 HelloWorld.class
-rw-r--r-- 1 root root  606 Jul  4 04:18 HelloWorld$.class
-rw-r--r-- 1 root root 1967 Jul  4 19:07 Location.class
-rw-r--r-- 1 root root 1398 Jul  5 02:22 MatrixTest$$anonfun$main$1$$anonfun$apply$mcVI$sp$1.class
-rw-r--r-- 1 root root 1618 Jul  5 02:22 MatrixTest$$anonfun$main$1.class
-rw-r--r-- 1 root root 1755 Jul  5 02:22 MatrixTest$$anonfun$main$2$$anonfun$apply$mcVI$sp$2.class
-rw-r--r-- 1 root root 1648 Jul  5 02:22 MatrixTest$$anonfun$main$2.class
-rw-r--r-- 1 root root  740 Jul  5 02:22 MatrixTest.class
-rw-r--r-- 1 root root 1420 Jul  5 02:22 MatrixTest$.class
-rw-r--r-- 1 root root 1385 Jul  5 03:20 RangeArray$$anonfun$main$1.class
-rw-r--r-- 1 root root 1385 Jul  5 03:20 RangeArray$$anonfun$main$2.class
-rw-r--r-- 1 root root  901 Jul  5 03:20 RangeArray$$anonfun$main$3.class
-rw-r--r-- 1 root root  780 Jul  5 03:20 RangeArray.class
-rw-r--r-- 1 root root 1282 Jul  5 03:20 RangeArray$.class
-rw-r--r-- 1 root root  618 Jul  4 22:12 RegexD1.class
-rw-r--r-- 1 root root 1262 Jul  4 22:12 RegexD1$.class
-rw-r--r-- 1 root root  618 Jul  4 22:18 RegexD2.class
-rw-r--r-- 1 root root 1278 Jul  4 22:18 RegexD2$.class
-rw-r--r-- 1 root root  611 Jul  4 22:07 RegexD.class
-rw-r--r-- 1 root root 1263 Jul  4 22:07 RegexD$.class
-rw-r--r-- 1 root root  649 Jul  4 22:05 RegexReplace.class
-rw-r--r-- 1 root root 1284 Jul  4 22:05 RegexReplace$.class
-rw-r--r-- 1 root root  637 Jul  4 21:48 RegexTest1.class
-rw-r--r-- 1 root root 1324 Jul  4 21:48 RegexTest1$.class
-rw-r--r-- 1 root root  637 Jul  4 21:46 RegexTest2.class
-rw-r--r-- 1 root root 1278 Jul  4 21:46 RegexTest2$.class
-rw-r--r-- 1 root root 1052 Jul  5 00:02 Some.class
-rw-r--r-- 1 root root 1762 Jul  5 00:02 Some$.class
-rw-r--r-- 1 root root  901 Jul  5 01:56 SourceTest$$anonfun$main$1.class
-rw-r--r-- 1 root root  700 Jul  5 01:56 SourceTest.class
-rw-r--r-- 1 root root 1101 Jul  5 01:56 SourceTest$.class
-rw-r--r-- 1 root root  605 Jul  4 19:07 Test2.class
-rw-r--r-- 1 root root  579 Jul  4 19:07 Test2$.class
-rw-r--r-- 1 root root  605 Jul  4 19:14 Test3.class
-rw-r--r-- 1 root root 1137 Jul  4 19:14 Test3$.class
-rw-r--r-- 1 root root  605 Jul  4 20:48 Test.class
-rw-r--r-- 1 root root 1266 Jul  4 20:48 Test$.class
-rw-r--r-- 1 root root  949 Jul  5 00:09 TestExtractor.class
-rw-r--r-- 1 root root 1540 Jul  5 00:09 TestExtractor$.class
-rw-r--r-- 1 root root  630 Jul  4 19:31 TraitTest.class
-rw-r--r-- 1 root root 1163 Jul  4 19:31 TraitTest$.class
-rw-r--r-- 1 root root 1480 Jul  4 19:31 Yiibai.class
[root@cdh2 test]# vim RangeArray.scala

[12]+  Stopped                 vim RangeArray.scala
[root@cdh2 test]# cat RangeArray.scala
object RangeArray {
   def main(args: Array[String]) {
      var myList1 = Range(10, 20, 2)
      var myList2 = Range(10,20)
	  var myList3 = Range(10, 20, 3)
      // Print all the array elements
      for ( x <- myList1 ) {
         print( " " + x )
      }
      println()
      for ( x <- myList2 ) {
         print( " " + x )
      }
	  println()
	  myList3.foreach{
	    print
	  }
   }
}
[root@cdh2 test]# vim RangeArray.scala
[root@cdh2 test]# scalac -d classes/ RangeArray.scala
[root@cdh2 test]# scala -classpath classes/ RangeArray
 10 12 14 16 18
 10 11 12 13 14 15 16 17 18 19
10131619[root@cdh2vim RangeArray.scalangeArray
[root@cdh2 test]# scalac -d classes/ RangeArray.scala
[root@cdh2 test]# scala -classpath classes/ RangeArray
 10 12 14 16 18
 10 11 12 13 14 15 16 17 18 19
 10131619
[root@cdh2 test]# 注意Range字母的大小写    objectName 和 保留字 别重复哦


[root@cdh2 test]# vim Str.scala
[root@cdh2 test]# scalac -d classes/ Str.scala
[root@cdh2 test]# scala -classpath classes/Str
Welcome to Scala version 2.9.3 (Java HotSpot(TM) Client VM, Java 1.7.0_67).
Type in expressions to have them evaluated.
Type :help for more information.

scala> [root@cdh2 test]# 
[root@cdh2 test]# scala -classpath classes/ Str
Hello, world!
[root@cdh2 test]# cat Str.scala
object Str {
   val greeting: String = "Hello, world!"

   def main(args: Array[String]) {
      println( greeting )
   }
}
[root@cdh2 test]# vim Str.scala
[root@cdh2 test]# scalac -d classes/ Str.scala
[root@cdh2 test]# scala -classpath classes/ Str
Hello, world!
heheha
String Length is : 17
[root@cdh2 test]# cat Str.scala
object Str {
   val greeting: String = "Hello, world!"
   val greet="heheha"
   def main(args: Array[String]) {
      println( greeting )
      println( greet )
   var palindrome = "Dot saw I was Tod";
   var len = palindrome.length();
   println( "String Length is : " + len );
   
   }
}
[root@cdh2 test]# vim Str.scala
[root@cdh2 test]# scalac -d classes/ Str.scala
[root@cdh2 test]# scala -classpath classes/ Str
Hello, world!
heheha
String Length is : 17
My name is Zara
Dot Dot saw I was Tod
[root@cdh2 test]# vim FormatTest.scala
[root@cdh2 test]# scalac -d classes/ FormatTest.scala 
[root@cdh2 test]# scala -classpath classes/ FormatTest
The value of the float variable is 12.456000, while the value of the integer variable is 2000, and the string is Hello, Scala!()
[root@cdh2 test]# 
[root@cdh2 test]# cat FormatTest.scala
object FormatTest {
   def main(args: Array[String]) {
      var floatVar = 12.456
      var intVar = 2000
      var stringVar = "Hello, Scala!"
      var fs = printf("The value of the float variable is " +
                   "%f, while the value of the integer " +
                   "variable is %d, and the string " +
                   "is %s", floatVar, intVar, stringVar)
      println(fs)
   }
}
[root@cdh2 test]# 最后一步字符串 为什么 自动填充了 () ??


[root@cdh2 test]# vim Factor.scala
[root@cdh2 test]# cp Factor.scala FactorTest.scala 
[root@cdh2 test]# scalac -d classes/ FactorTest.scala 
[root@cdh2 test]# scala -classpath classes/ FactorTest
muliplier(1) value = 4
muliplier(2) value = 8
[root@cdh2 test]# cat FactorTest.scala 
object FactorTest {
   def main(args: Array[String]) {
      println( "muliplier(1) value = " +  multiplier(1) )
      println( "muliplier(2) value = " +  multiplier(2) )
   }
   var factor = 4
   val multiplier = (i:Int) => i * factor
}
[root@cdh2 test]# vim Fun.scala
[root@cdh2 test]# vim Fun.scala
[root@cdh2 test]# scalac -d classes/ Fun.scala 
[root@cdh2 test]# scala -classpath classes/ Fun
Returned Value : 12
[root@cdh2 test]# cat Fun.scala
object Fun {
   def main(args: Array[String]) {
        println( "Returned Value : " + addInt(5,7) );
   }
   def addInt( a:Int, b:Int ) : Int = {
      var sum:Int = 0
      sum = a + b

      return sum
   }
   /*object Hello{
     def printMe( ) : Unit = {
      println("Hello, Scala!")
     }
   }*/
}
object Hello{
   def printMe( ) : Unit = {
      println("Hello, Scala!")
   }
}
[root@cdh2 test]# cp Fun.scala Fun2.scala
[root@cdh2 test]# vim Fun2.scala
[root@cdh2 test]# scalac -d classes/ Fun2.scala 
Fun2.scala:4: error: not found: type Hello
   new Hello().printMe()
       ^
one error found
[root@cdh2 test]# vim Fun2.scala
[root@cdh2 test]# scalac -d classes/ Fun2.scala 
Fun2.scala:4: error: not found: type Hello
      (new Hello).printMe()
           ^
one error found
[root@cdh2 test]# vim Fun2.scala
[root@cdh2 test]# vim Fun2.scala
[root@cdh2 test]# scalac -d classes/ Fun2.scala 
[root@cdh2 test]# scala -classpath classes/ Fun2
Exception in thread "main" java.lang.RuntimeException: Cannot figure out how to run target: Fun2
	at scala.sys.package$.error(package.scala:27)
	at scala.tools.nsc.GenericRunnerCommand.scala$tools$nsc$GenericRunnerCommand$$guessHowToRun(GenericRunnerCommand.scala:38)
	at scala.tools.nsc.GenericRunnerCommand$$anonfun$2.apply(GenericRunnerCommand.scala:48)
	at scala.tools.nsc.GenericRunnerCommand$$anonfun$2.apply(GenericRunnerCommand.scala:48)
	at scala.Option.getOrElse(Option.scala:108)
	at scala.tools.nsc.GenericRunnerCommand.<init>(GenericRunnerCommand.scala:48)
	at scala.tools.nsc.GenericRunnerCommand.<init>(GenericRunnerCommand.scala:17)
	at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:33)
	at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:89)
	at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)
[root@cdh2 test]# vim Fun2.scala
[root@cdh2 test]# scalac -d classes/ Fun2.scala 
[root@cdh2 test]# scala -classpath classes/ Fun2
Exception in thread "main" java.lang.RuntimeException: Cannot figure out how to run target: Fun2
	at scala.sys.package$.error(package.scala:27)
	at scala.tools.nsc.GenericRunnerCommand.scala$tools$nsc$GenericRunnerCommand$$guessHowToRun(GenericRunnerCommand.scala:38)
	at scala.tools.nsc.GenericRunnerCommand$$anonfun$2.apply(GenericRunnerCommand.scala:48)
	at scala.tools.nsc.GenericRunnerCommand$$anonfun$2.apply(GenericRunnerCommand.scala:48)
	at scala.Option.getOrElse(Option.scala:108)
	at scala.tools.nsc.GenericRunnerCommand.<init>(GenericRunnerCommand.scala:48)
	at scala.tools.nsc.GenericRunnerCommand.<init>(GenericRunnerCommand.scala:17)
	at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:33)
	at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:89)
	at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)
[root@cdh2 test]# vim Fun2.scala
[root@cdh2 test]# scalac -d classes/ Fun2.scala 
[root@cdh2 test]# scala -classpath classes/ Fun2
Returned Value : 12
Hello, Scala!
[root@cdh2 test]# 
[root@cdh2 test]# scala -classpath classes/ Fun2
Returned Value : 12
Hello, Scala!
[root@cdh2 test]# cat Fun2.scala
object Fun2 {
   def main(args: Array[String]) {
      println( "Returned Value : " + addInt(5,7) );
      (new Hello).printMe()
   }
   def addInt( a:Int, b:Int ) : Int = {
      var sum:Int = 0
      sum = a + b

      return sum
   }
   /*object Hello{
     def printMe( ) : Unit = {
      println("Hello, Scala!")
     }
   }*/
}
class Hello{
   def printMe( ) : Unit = {
      println("Hello, Scala!")
   }
}
[root@cdh2 test]# object 调用 外面别的方法,Unit 相当于 void.


[root@cdh2 test]# vim Outer.scala
[root@cdh2 test]# scalac -d classes/ Outer.scala 
Outer.scala:8: error: method f in class Inner cannot be accessed in Outer.this.Inner
   (new Inner).f() // Error: f is not accessible
               ^
one error found
[root@cdh2 test]# cat Outer.scala
class Outer {
   class Inner {
      private def f() { println("f") }
      class InnerMost {
         f() // OK
      }
   }
   (new Inner).f() // Error: f is not accessible
}
[root@cdh2 test]# vim Outer.scala
[root@cdh2 test]# scalac -d classes/ Outer.scala 
Outer.scala:3: error: ';' expected but 'def' found.
      public def f() { println("f") }
             ^
one error found
[root@cdh2 test]# vim Outer.scala
[root@cdh2 test]# scalac -d classes/ Outer.scala 
[root@cdh2 test]# cat Outer.scala
class Outer {
   class Inner {
      def f() { println("f") }
      class InnerMost {
         f() // OK
      }
   }
   (new Inner).f() // Error: f is not accessible
}
[root@cdh2 test]# vim Outer.scala
[root@cdh2 test]# scalac -d classes/ Outer.scala 
Outer.scala:3: error: '=' expected but identifier found.
      def public f() { println("f") }
                 ^
Outer.scala:4: error: illegal start of simple expression
      class InnerMost {
^
two errors found
[root@cdh2 test]# cat Outer.scala
class Outer {
   class Inner {
      def public f() { println("f") }
      class InnerMost {
         f() // OK
      }
   }
   (new Inner).f() // Error: f is not accessible
}
[root@cdh2 test]# vim Outer.scala
[root@cdh2 test]# scalac -d classes/ Outer.scala
[root@cdh2 test]# scala -classpath classes/ Outer
java.lang.NoSuchMethodException: Outer.main([Ljava.lang.String;)
	at java.lang.Class.getMethod(Class.java:1665)
	at scala.tools.nsc.util.ScalaClassLoader$class.run(ScalaClassLoader.scala:74)
	at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.run(ScalaClassLoader.scala:101)
	at scala.tools.nsc.ObjectRunner$.run(ObjectRunner.scala:33)
	at scala.tools.nsc.ObjectRunner$.runAndCatch(ObjectRunner.scala:40)
	at scala.tools.nsc.MainGenericRunner.runTarget$1(MainGenericRunner.scala:56)
	at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:80)
	at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:89)
	at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)
[root@cdh2 test]# cat Outer.scala
class Outer {
   class Inner {
      def f() { println("f") }
      class InnerMost {
         f() // OK
      }
   }
   (new Inner).f() // Error: f is not accessible
}
[root@cdh2 test]# cp Outer.scala Outer1.scala 
[root@cdh2 test]# scalac -d classes/ Outer1.scala
[root@cdh2 test]# vim Outer1.scala
[root@cdh2 test]# scalac -d classes/ Outer1.scala
[root@cdh2 test]# scala -classpath classes/ Outer1
Exception in thread "main" java.lang.RuntimeException: Cannot figure out how to run target: Outer1
	at scala.sys.package$.error(package.scala:27)
	at scala.tools.nsc.GenericRunnerCommand.scala$tools$nsc$GenericRunnerCommand$$guessHowToRun(GenericRunnerCommand.scala:38)
	at scala.tools.nsc.GenericRunnerCommand$$anonfun$2.apply(GenericRunnerCommand.scala:48)
	at scala.tools.nsc.GenericRunnerCommand$$anonfun$2.apply(GenericRunnerCommand.scala:48)
	at scala.Option.getOrElse(Option.scala:108)
	at scala.tools.nsc.GenericRunnerCommand.<init>(GenericRunnerCommand.scala:48)
	at scala.tools.nsc.GenericRunnerCommand.<init>(GenericRunnerCommand.scala:17)
	at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:33)
	at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:89)
	at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)
[root@cdh2 test]# vim Outer1.scala
[root@cdh2 test]# scalac -d classes/ Outer1.scala
[root@cdh2 test]# scala -classpath classes/ Outer1
java.lang.NoSuchMethodException: Outer1.main is not static
	at scala.tools.nsc.util.ScalaClassLoader$class.run(ScalaClassLoader.scala:76)
	at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.run(ScalaClassLoader.scala:101)
	at scala.tools.nsc.ObjectRunner$.run(ObjectRunner.scala:33)
	at scala.tools.nsc.ObjectRunner$.runAndCatch(ObjectRunner.scala:40)
	at scala.tools.nsc.MainGenericRunner.runTarget$1(MainGenericRunner.scala:56)
	at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:80)
	at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:89)
	at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)
[root@cdh2 test]# vim Outer1.scala
[root@cdh2 test]# scalac -d classes/ Outer1.scala
Outer1.scala:8: error: '=' expected but identifier found.
   def static main(args: Array[String]) {
              ^
Outer1.scala:11: error: illegal start of simple expression
}
^
two errors found
[root@cdh2 test]# cat Outer1.scala
class Outer1 {
   class Inner {
      def f() { println("f") }
      class InnerMost {
         f() // OK
      }
   }
   def static main(args: Array[String]) {
     (new Inner).f() // Error: f is not accessible
   }
}
[root@cdh2 test]# vim Outer1.scala
[root@cdh2 test]# scalac -d classes/ Outer1.scala
[root@cdh2 test]# scala -classpath classes/ Outer1
java.lang.NoSuchMethodException: Outer1.main([Ljava.lang.String;)
	at java.lang.Class.getMethod(Class.java:1665)
	at scala.tools.nsc.util.ScalaClassLoader$class.run(ScalaClassLoader.scala:74)
	at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.run(ScalaClassLoader.scala:101)
	at scala.tools.nsc.ObjectRunner$.run(ObjectRunner.scala:33)
	at scala.tools.nsc.ObjectRunner$.runAndCatch(ObjectRunner.scala:40)
	at scala.tools.nsc.MainGenericRunner.runTarget$1(MainGenericRunner.scala:56)
	at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:80)
	at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:89)
	at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)
[root@cdh2 test]# class 里面 怎么写 main 函数??



[root@cdh2 test]# scalac -d classes/ ProtectPub.scala
[root@cdh2 test]# scalac -d classes/ Other.scala
[root@cdh2 test]# scalac -d classes/ Outer1.scala

Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10[root@cdh2 test]# 
[root@cdh2 test]# cat WhileTest1.scala
object WhileTest1 {
   def main(args: Array[String]) {
      var a = 10;
      // An infinite loop.
      while( true ){
         println( "Value of a: " + a );
      }
   }
}
[root@cdh2 test]# scalac -d classes/ WhileTest.scala 
[root@cdh2 test]# scala -classpath classes/ WhileTest
Value of a: 10
Value of a: 11
Value of a: 12
Value of a: 13
Value of a: 14
Value of a: 15
Value of a: 16
Value of a: 17
Value of a: 18
Value of a: 19
[root@cdh2 test]# cat WhileTest.scala
object WhileTest {
   def main(args: Array[String]) {
      // Local variable declaration:
      var a = 10;

      // while loop execution
      while( a < 20 ){
         println( "Value of a: " + a );
         a = a + 1;
      }
   }
}
[root@cdh2 test]# scalac -d classes/ Outer.scala 
[root@cdh2 test]# cat Outer.scala
class Outer {
   class Inner {
      def f() { println("f") }
      class InnerMost {
         f() // OK
      }
   }
   (new Inner).f() // Error: f is not accessible
}
[root@cdh2 test]# scalac -d classes/ ProtectPub.scala
[root@cdh2 test]# scalac -d classes/ Other.scala
[root@cdh2 test]# scalac -d classes/ Outer1.scala
[root@cdh2 test]# scalac -d classes/ Fun2.scala 
[root@cdh2 test]# scalac -d classes/ Fun1.scala 
error: source file 'Fun1.scala' could not be found
one error found
[root@cdh2 test]# scalac -d classes/ Fun.scala 
[root@cdh2 test]# cat Fun.scala 
object Fun {
   def main(args: Array[String]) {
        println( "Returned Value : " + addInt(5,7) );
   }
   def addInt( a:Int, b:Int ) : Int = {
      var sum:Int = 0
      sum = a + b

      return sum
   }
   /*object Hello{
     def printMe( ) : Unit = {
      println("Hello, Scala!")
     }
   }*/
}
object Hello{
   def printMe( ) : Unit = {
      println("Hello, Scala!")
   }
}
[root@cdh2 test]# cat Fun1.scala 
cat: Fun1.scala: No such file or directory
[root@cdh2 test]# cat Fun2.scala 
object Fun2 {
   def main(args: Array[String]) {
      println( "Returned Value : " + addInt(5,7) );
      (new Hello).printMe()
   }
   def addInt( a:Int, b:Int ) : Int = {
      var sum:Int = 0
      sum = a + b

      return sum
   }
   /*object Hello{
     def printMe( ) : Unit = {
      println("Hello, Scala!")
     }
   }*/
}
class Hello{
   def printMe( ) : Unit = {
      println("Hello, Scala!")
   }
}
[root@cdh2 test]# 
[root@cdh2 test]# cat ProtectPub.scala
package society {
   package professional {
      class Executive {
         private[professional] var workDetails = null
         private[society] var friends = null
         private[this] var secrets = null

         def help(another : Executive) {
            println(another.workDetails)
            //println(another.secrets) //ERROR
         }
      }
   }
}
[root@cdh2 test]# cat Outer1.scala
class Outer1 {
   class Inner {
      def f() { println("f") }
      class InnerMost {
         f() // OK
      }
   }
   //def static main(args: Array[String]) {
     (new Inner).f() // Error: f is not accessible
   //}
}
[root@cdh2 test]# cat Outer2.scala
cat: Outer2.scala: No such file or directory
[root@cdh2 test]# cat Outer.scala
class Outer {
   class Inner {
      def f() { println("f") }
      class InnerMost {
         f() // OK
      }
   }
   (new Inner).f() // Error: f is not accessible
}
[root@cdh2 test]# cat Other.scala
package p {
   class Super {
      def f() { println("f")}
   }
   class Sub extends Super {
      f()
   }
   class Other {
     (new Super).f() // Error: f is not accessible
   }
}
[root@cdh2 test]# cat Other1.scala
cat: Other1.scala: No such file or directory
[root@cdh2 test]# 


[root@cdh2 test]# vim BreakTest.scala
[root@cdh2 test]# scalac -d classes/ BreakTest.scala
[root@cdh2 test]# scala -classpath classes/ BreakTest
Value of a: 1
Value of a: 2
Value of a: 3
Value of a: 4
After the loop
[root@cdh2 test]# cat BreakTest.scala
import scala.util.control._

object BreakTest {
   def main(args: Array[String]) {
      var a = 0;
      val numList = List(1,2,3,4,5,6,7,8,9,10);

      val loop = new Breaks;
      loop.breakable {
         for( a <- numList){
            println( "Value of a: " + a );
            if( a == 4 ){
               loop.break;
            }
         }
      }
      println( "After the loop" );
   }
}
[root@cdh2 test]# vim BreakTest1.scala
[root@cdh2 test]# scalac -d classes/ BreakTest1.scala
[root@cdh2 test]# scala -classpath classes/ BreakTest1
Value of a: 1
Value of b: 11
Value of b: 12
Value of a: 2
Value of b: 11
Value of b: 12
Value of a: 3
Value of b: 11
Value of b: 12
Value of a: 4
Value of b: 11
Value of b: 12
Value of a: 5
Value of b: 11
Value of b: 12
[root@cdh2 test]# cat BreakTest1.scala
import scala.util.control._

object BreakTest1 {
   def main(args: Array[String]) {
      var a = 0;
      var b = 0;
      val numList1 = List(1,2,3,4,5);
      val numList2 = List(11,12,13);

      val outer = new Breaks;
      val inner = new Breaks;

      outer.breakable {
         for( a <- numList1){
            println( "Value of a: " + a );
            inner.breakable {
               for( b <- numList2){
                  println( "Value of b: " + b );
                  if( b == 12 ){
                     inner.break;
                  }
               }
            } // inner breakable
         }
      } // outer breakable.
   }
}
[root@cdh2 test]# vim DoWhileTest.scala
[root@cdh2 test]# scalac -d classes/ DoWhileTest.scala
[root@cdh2 test]# scala -classpath classes/ DoWhileTest
Value of a: 10
Value of a: 11
Value of a: 12
Value of a: 13
Value of a: 14
Value of a: 15
Value of a: 16
Value of a: 17
Value of a: 18
Value of a: 19
[root@cdh2 test]# cat DoWhileTest.scala
object DoWhileTest {
   def main(args: Array[String]) {
      // Local variable declaration:
      var a = 10;

      // do loop execution
      do{
         println( "Value of a: " + a );
         a = a + 1;
      }while( a < 20 )
   }
}
[root@cdh2 test]# vim IfTest.scala
[root@cdh2 test]# scalac -d classes/ IfTest.scala
[root@cdh2 test]# scala -classpath classes/ IfTest
This is else statement
[root@cdh2 test]# cat IfTest.scala
object IfTest {
   def main(args: Array[String]) {
      var x = 30;

      if( x < 20 ){
         println("This is if statement");
      }else{
         println("This is else statement");
      }
   }
}
[root@cdh2 test]# scala
Welcome to Scala version 2.9.3 (Java HotSpot(TM) Client VM, Java 1.7.0_67).
Type in expressions to have them evaluated.
Type :help for more information.

scala> A = 0011 1100
<console>:1: error: ';' expected but integer literal found.
       A = 0011 1100
                ^

scala> 

scala> B = 0000 1101
<console>:1: error: ';' expected but integer literal found.
       B = 0000 1101
                ^

scala> A = 00111100
<console>:10: error: not found: value A
val $ires0 = A
             ^
<console>:7: error: not found: value A
       A = 00111100
       ^

scala> var A = 00111100
A: Int = 37440

scala> var B = 00001101
B: Int = 577

scala> A&B
res0: Int = 576

scala> A|B
res1: Int = 37441

scala> A^B
res2: Int = 36865

scala> ~A
res3: Int = -37441

scala> var A = OX00111100
<console>:7: error: not found: value OX00111100
       var A = OX00111100
               ^

scala> var A = Ox00111100
<console>:7: error: not found: value Ox00111100
       var A = Ox00111100
               ^

scala> 

scala> 

scala> 

scala> var myVar = 10;
myVar: Int = 10

scala> val myVal = "Hello, Scala!";
myVal: java.lang.String = Hello, Scala!

scala> val myVal:String = "Hello, Scala!";
myVal: String = Hello, Scala!

scala> var myVar :Int;
<console>:7: error: only classes can have declared but undefined members
(Note that variables need to be initialized to be defined)
       var myVar :Int;
           ^

scala> val myVal :String;
<console>:7: error: only classes can have declared but undefined members
       val myVal :String;
           ^

scala> var myVar:Int;
<console>:7: error: only classes can have declared but undefined members
(Note that variables need to be initialized to be defined)
       var myVar:Int;
           ^

scala> var myVar = 10;
myVar: Int = 10

scala> val myVal = "Hello, Scala!";
myVal: java.lang.String = Hello, Scala!

scala> val (myVar1: Int, myVar2: String) = Pair(40, "Foo");
myVar1: Int = 40
myVar2: String = Foo

scala> val (myVar1, myVar2) = Pair(40, "Foo");
myVar1: Int = 40
myVar2: java.lang.String = Foo

scala> 0xFFFFFFFF
res4: Int = -1

scala> var A=0xFFFFFFFF
A: Int = -1

scala> var A=0x00111100
A: Int = 1118464

scala> 0
res5: Int = 0

scala> 035
res6: Int = 29

scala> 21
res7: Int = 21

scala> 0
res8: Int = 0

scala> 0xFFFFFFFF
res9: Int = -1

scala> 0777L
res10: Long = 511

scala> 

scala> 

scala> 0.0 
res11: Double = 0.0

scala> 1e30f 
res12: Float = 1.0E30

scala> 3.14159f 
res13: Float = 3.14159

scala> 1.0e100
res14: Double = 1.0E100

scala> .1
<console>:1: error: ';' expected but double literal found.
       res14.1
            ^

scala> .1
<console>:1: error: ';' expected but double literal found.
       res14.1
            ^

scala> 0.1
res15: Double = 0.1

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

scala> 'u0041'
<console>:1: error: unclosed character literal
       'u0041'
             ^

scala> "u0041"
res17: java.lang.String = u0041

scala> "Hello,
<console>:1: error: unclosed string literal
       "Hello,
       ^

scala> World!"
<console>:1: error: unclosed string literal
       World!"
             ^

scala> [root@cdh2 test]# 
[root@cdh2 test]# vim Symb.scala
[root@cdh2 test]# scalac -d classes/ S.scala
Some1.scala       Some.scala        SourceTest.scala  Str.scala         Symb.scala        
[root@cdh2 test]# scalac -d classes/ Symb.scala
[root@cdh2 test]# scala -classpath classes/ Symb
Exception in thread "main" java.lang.RuntimeException: Cannot figure out how to run target: Symb
	at scala.sys.package$.error(package.scala:27)
	at scala.tools.nsc.GenericRunnerCommand.scala$tools$nsc$GenericRunnerCommand$$guessHowToRun(GenericRunnerCommand.scala:38)
	at scala.tools.nsc.GenericRunnerCommand$$anonfun$2.apply(GenericRunnerCommand.scala:48)
	at scala.tools.nsc.GenericRunnerCommand$$anonfun$2.apply(GenericRunnerCommand.scala:48)
	at scala.Option.getOrElse(Option.scala:108)
	at scala.tools.nsc.GenericRunnerCommand.<init>(GenericRunnerCommand.scala:48)
	at scala.tools.nsc.GenericRunnerCommand.<init>(GenericRunnerCommand.scala:17)
	at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:33)
	at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:89)
	at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)
[root@cdh2 test]# cat Symb.scala
package scala
final case class Symbol private (name: String) {
   override def toString: String = "'" + name
}
[root@cdh2 test]# vim Hl.scala
[root@cdh2 test]# scalac -d classes/ Hl.scala
Hl.scala:3: error: unclosed string literal
      println("Hello	World
              ^
Hl.scala:5: error: unclosed string literal
" );
^
Hl.scala:6: error: ')' expected but '}' found.
   }
   ^
three errors found
[root@cdh2 test]# vim Hl.scala
[root@cdh2 test]# scalac -d classes/ Hl.scala
[root@cdh2 test]# scala -classpath classes/ H
HelloWorld2.scala  HelloWorld.scala   Hl.scala           
[root@cdh2 test]# scala -classpath classes/ Hl
Hello	World
[root@cdh2 test]# scala
Welcome to Scala version 2.9.3 (Java HotSpot(TM) Client VM, Java 1.7.0_67).
Type in expressions to have them evaluated.
Type :help for more information.

scala> import scala.collection.immutable.{TreeMap, TreeSet}
import scala.collection.immutable.{TreeMap, TreeSet}

scala> import scala.collection.mutable.HashMap
import scala.collection.mutable.HashMap

scala> import scala.xml._
import scala.xml._

scala> package com.liftcode.stuff
<console>:1: error: illegal start of definition
       package com.liftcode.stuff
       ^

scala> val s = "hello"; println(s)
hello
s: java.lang.String = hello

scala> val s = "hello"
s: java.lang.String = hello

scala> println(s)
hello

scala> println(s);
hello

scala> [root@cdh2 test]# 
[root@cdh2 test]# vim HelloWorld.scala

[13]+  Stopped                 vim HelloWorld.scala
[root@cdh2 test]# vim HelloWorld23.scala
[root@cdh2 test]# scalac -d classes/ HelloWorld2
HelloWorld23.scala  HelloWorld2.scala   
[root@cdh2 test]# scalac -d classes/ HelloWorld23.scala 
[root@cdh2 test]# scala -classpath classes/ HelloWorld23
Hello, world!
[root@cdh2 test]# cat HelloWorld23.scala
object HelloWorld23{
   /* This is my first java program.  
    * This will print 'Hello World' as the output
    * This is an example of multi-line comments.
    */
   def main(args: Array[String]) {
      // Prints Hello World
      // This is also an example of single line comment.
      println("Hello, world!") 
   }
}
[root@cdh2 test]# scala
Welcome to Scala version 2.9.3 (Java HotSpot(TM) Client VM, Java 1.7.0_67).
Type in expressions to have them evaluated.
Type :help for more information.

scala> println("Hello, Scala!");
Hello, Scala!

scala> !scala -version
<console>:8: error: object unary_! is not a member of package scala
              !scala -version
              ^

scala> !scala -version
<console>:8: error: object unary_! is not a member of package scala
              !scala -version
              ^

scala> [root@cdh2 test]# 
[root@cdh2 test]# scala
Welcome to Scala version 2.9.3 (Java HotSpot(TM) Client VM, Java 1.7.0_67).
Type in expressions to have them evaluated.
Type :help for more information.

scala> 
[root@cdh2 test]# scala -version
Scala code runner version 2.9.3 -- Copyright 2002-2011, LAMP/EPFL
[root@cdh2 test]# 







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

5icode.top

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值