Scala 学习笔记
1 object func_exp { 2 println("Welcome to the Scala worksheet") //> Welcome to the Scala worksheet 3 def hello(name: String): String = { 4 s"Hello, ${name}" 5 } //> hello: (name: String)String 6 7 hello("hadoop") //> res0: String = Hello, hadoop 8 9 def hello2(name: String): String = { 10 s"Hello, ${name}" 11 } //> hello2: (name: String)String 12 13 14 def add(x: Int, y: Double) = x * y //> add: (x: Int, y: Double)Double 15 add(1,6) //> res1: Double = 6.0 16 17 18 19 val l = List("hadoop","java","scala") //> l : List[String] = List(hadoop, java, scala) 20 for( 21 s<-l 22 )println(s) //> hadoop 23 //| java 24 //| scala 25 26 val xz = List("hadoop","java","scala") //> xz : List[String] = List(hadoop, java, scala) 27 for( 28 s<-l 29 if(s.length>4) 30 )println(s) //> hadoop 31 //| scala 32 33 34 35 val result_for = for { 36 s<-l 37 s1 = s.toUpperCase() 38 if(s1 != "") 39 }yield(s1) //> result_for : List[String] = List(HADOOP, JAVA, SCALA) 40 41 42 43 44 val code = 4 //> code : Int = 4 45 val rest_match = code match{ 46 case 1 => "one" 47 case 2 => "two" 48 case _ => "other" 49 50 } //> rest_match : String = other 51 52 53 val rest_try = try { 54 Integer.parseInt("dog") 55 } catch { 56 case _ => 0 57 }finally{ 58 println("always be printed") 59 } //> always be printed 60 //| rest_try : Int = 0 61 }