Scala

オブジェクトの定義

オブジェクトの定義 scala> object Simple defined module Simple scala> new Simple <console>:6: error: not found: type Simple new Simple ^ scala> object OneMethod { | def myMethod() = "only one" | } defined module OneMethod scala> OneMethod.myMethod r</console>…

トレイトの定義

トレイトの定義 scala> trait Cat { | def meow(): String | } defined trait Cat scala> new Cat <console>:7: error: trait Cat is abstract; cannot be instantiated new Cat ^ scala> trait FuzzyCat extends Cat { | override def meow(): String = "Meeeeeeeeee</console>…

クラスの定義

クラスの定義 scala> class Foo defined class Foo scala> new Foo res0: Foo = Foo@1f40e1f scala> new Foo() res1: Foo = Foo@18efd7c scala> class Bar(name: String) defined class Bar scala> new Bar() <console>:7: error: not enough arguments for construct</console>…

数字の加算

数字の加算 lesson004.scala import scala.io._ def toInt(in: String): Option[Int] = try { Some(Integer.parseInt(in.trim)) } catch { case e: NumberFormatException => None } def sum(in: Seq[String]) = { val ints = in.flatMap(s => toInt(s)) int…

数字の出力

数字の出力 lesson002.scala for {i <- 1 to 10} println(i)実行結果 T:\>scala lesson002.scala 1 2 3 4 5 6 7 8 9 10 lesson003.scala for {i <- 1 to 3 j <- 2 to 4} println(i * j)実行結果 T:\>scala lesson003.scala 2 3 4 4 6 8 6 9 12

hello world

さまざまな言語で "hello world" Scala lesson001.scala println("Hello World")実行結果 T:\>scala lesson001.scala Hello World F# lesson001.fsx printfn "Hello World"実行結果 T:\>fsi lesson001.fsx Hello World