64-Bit Server VM, Java 1.8.0_25). Type in expressions to have them evaluated. Type :help for more information. scala> println("Hello Scala!") Hello Scala! scala> :quit
64-Bit Server VM, Java 1.8.0_25). Type in expressions to have them evaluated. Type :help for more information. scala> println("Hello Scala!") Hello Scala! scala> :quit λʔϛφϧ͔Β”scala”ίϚϯυΛ࣮ߦ
64-Bit Server VM, Java 1.8.0_25). Type in expressions to have them evaluated. Type :help for more information. scala> println("Hello Scala!") Hello Scala! scala> :quit λʔϛφϧ͔Β”scala”ίϚϯυΛ࣮ߦ ऴྃ͢Δͱ͖”:quit”͘͠”:q”
scala> lang = "java" <console>:8: error: reassignment to val lang = "java" ^ scala> var lang = "php" lang: String = php scala> lang = "ruby" lang: String = ruby ࠶ೖෆՄ
scala> lang = "java" <console>:8: error: reassignment to val lang = "java" ^ scala> var lang = "php" lang: String = php scala> lang = "ruby" lang: String = ruby ࠶ೖෆՄ ࠶ೖՄ
1 scala> val num0: Int = "1" <console>:7: error: type mismatch; found : String("1") required: Int val num0: Int = "1" ^ scala> val num0: String = "1" num0: String = 1 ܕ͕ҟͳΔ
y plus: (Int, Int) => Int = <function2> scala> val sum = (x: Int, y: Int) => { | println("x is " + x) | println("y is " + y) | x + y | } sum: (Int, Int) => Int = <function2> ؔϦςϥϧΛ มʹೖ
y plus: (Int, Int) => Int = <function2> scala> val sum = (x: Int, y: Int) => { | println("x is " + x) | println("y is " + y) | x + y | } sum: (Int, Int) => Int = <function2> scala> sum(2, 3) x is 2 y is 3 res4: Int = 5 ؔϦςϥϧΛ มʹೖ ෳߦͷ߹ ࠷ޙͷߦ͕ධՁ͞ΕΔ
= "Scala" lang match { case "Java" => println("Eclipse") case "Scala" => println("IntelliJ") case "PHP" | "Ruby"=> println("CodeRunner") case _ => println("Vim") } } }
= "Scala" lang match { case "Java" => println("Eclipse") case "Scala" => println("IntelliJ") case "PHP" | "Ruby"=> println("CodeRunner") case _ => println("Vim") } } } “IntelliJ”͕ग़ྗ͞ΕΔ
= "Scala" val ide:String = lang match { case "Java" => "Eclipse" case "Scala" => "IntelliJ" case "PHP" | "Ruby"=> "CodeRunner" case _ => "Vim" } println("Use "+ ide +".") } }
= "Scala" val ide:String = lang match { case "Java" => "Eclipse" case "Scala" => "IntelliJ" case "PHP" | "Ruby"=> "CodeRunner" case _ => "Vim" } println("Use "+ ide +".") } } Λฦ͢͜ͱͰ͖·͢
= "Scala" val ide:String = lang match { case "Java" => "Eclipse" case "Scala" => "IntelliJ" case "PHP" | "Ruby"=> "CodeRunner" case _ => "Vim" } println("Use "+ ide +".") } } Λฦ͢͜ͱͰ͖·͢ “Use IntelliJ.”͕ग़ྗ͞ΕΔ
val sum = (x0: Int, x1: Int) => x0 + x1 val isEven = (x: Int) => x % 2 == 0 // (1 to 100).filter(x => x % 2 == 0).reduce(_ + _) val result = (1 to 100).filter(isEven).reduce(sum) println(result) } } ίϨΫγϣϯϝιουΛνΣʔϯ͢Δ͜ͱͰ͖·͢
val fizzBuzz = (x: Int) => ( x%3, x%5 ) match { case (0, 0) => "FizzBuzz" case (0, _) => "Fizz" case (_, 0) => "Buzz" case _ => x.toString } val result = (1 to 100).map(fizzBuzz).mkString(",") println(result) } }
val fizzBuzz = (x: Int) => ( x%3, x%5 ) match { case (0, 0) => "FizzBuzz" case (0, _) => "Fizz" case (_, 0) => "Buzz" case _ => x.toString } val result = (1 to 100).map(fizzBuzz).mkString(",") println(result) } } xΛ3Ͱׂͬͨ͋·Γͱɺ 5Ͱׂͬͨ͋·ΓͷTuple
val fizzBuzz = (x: Int) => ( x%3, x%5 ) match { case (0, 0) => "FizzBuzz" case (0, _) => "Fizz" case (_, 0) => "Buzz" case _ => x.toString } val result = (1 to 100).map(fizzBuzz).mkString(",") println(result) } } xΛ3Ͱׂͬͨ͋·Γͱɺ 5Ͱׂͬͨ͋·ΓͷTuple TupleͷͰύλʔϯϚον ͢Δ͜ͱͰ͖Δ
val fizzBuzz = (x: Int) => ( x%3, x%5 ) match { case (0, 0) => "FizzBuzz" case (0, _) => "Fizz" case (_, 0) => "Buzz" case _ => x.toString } val result = (1 to 100).map(fizzBuzz).mkString(",") println(result) } } fizzBuzzมʹ͞Ε͍ͯΔ ͷؔϦςϥϧ