scalaの勉強会に行ったのでとりあえず、scalaを触ってみた
東京でscalaの勉強会に参加してきました。
http://www.kaigai-engineer.com/blog/?p=59
完全にノリで参加したのですが、
LL Tigerで関数型言語がえらく取り上げられていたこともあるし、
勉強会に参加することでscalaをさわってみよう!という取っ掛かりができた。
ということで、チュートリアルでも読んでみる。
英語はわからないので和訳で。
Tutorial和訳 - プログラミング言語 Scala Wiki - アットウィキ
# cat << _EOD_ > HelloWorld.scala object HelloWorld { def main(args: Array[String]) { println("Hello, world!") } } _EOD_ # scalac HelloWorld.scala # ls HelloWorld$.class HelloWorld.class HelloWorld.scala # scala -classpath . HelloWorld Hello, world!
- Javaと連携
# cat << _EOD_ > JavaAndScala.scala import java.util.{Date, Locale} import java.text.DateFormat import java.text.DateFormat._ object FrenchDate { def main(args: Array[String]) { val now = new Date val df = getDateInstance(LONG, Locale.FRANCE) println(df format now) } } _EOD_ # scalac JavaAndScala.scala # scala -classpath . FrenchDate 2 septembre 2010
# scala Welcome to Scala version 2.7.7.final (OpenJDK Client VM, Java 1.6.0_18). Type in expressions to have them evaluated. Type :help for more information. scala>
- 数
scala> val x = 2 x: Int = 2 scala> 1+2*3/x res1: Int = 4 scala> (1).+(((2).*(3))./(x)) res4: Int = 4 scala> 1.+(2) res5: Double = 3.0 scala> (1).+(2) res6: Int = 3
- 関数
# cat << _EOD_ > FuncIsObject.scala object Timer { def oncePerSecond(callback: () => Unit) { while (true) { callback(); Thread sleep 1000 } } def timeFlies() { println("time flies like an arrow...") } def main(args: Array[String]) { oncePerSecond(timeFlies) } } _EOD_ # scalac FuncIsObject.scala # scala -classpath . Timer.class error: IO error while decoding /root/work_scala/Timer.class with UTF-8 Please try specifying another one using the -encoding option TODO:エンコーディング周りでエラー。あとで調べる。 →解決 .classが付いていたw # scala -classpath . Timer time flies like an arrow... time flies like an arrow... time flies like an arrow... time flies like an arrow... time flies like an arrow... time flies like an arrow... time flies like an arrow... time flies like an arrow... ^C
- 無名関数
# cat << _EOD_ > NoNameFunc.scala object TimerAnonymous { def oncePerSecond(callback: () => Unit) { while (true) { callback(); Thread sleep 1000 } } def main(args: Array[String]) { oncePerSecond(() => println("time flies like an arrow...")) } } _EOD_ # scalac NoNameFunc.scala # scala -classpath . TimerAnonymous time flies like an arrow... time flies like an arrow... time flies like an arrow... time flies like an arrow... time flies like an arrow... ^C
今日はここまで!
追記!
- クラス
# cat Klass.scala class Complex(real: Double, imaginary: Double) { def re() = real def im() = imaginary } object ComplexNumbers { def main(args: Array[String]) { val c = new Complex(1.2, 3.4) println("real part: " + c.re()) println("imaginary part: " + c.im()) } } # scalac Klass.scala # scala ComplexNumbers real part: 1.2 imaginary part: 3.4
- 引数なしメソッド
# cat NoArgMethod.scala class Complex(real: Double, imaginary: Double) { def re = real def im = imaginary } object ComplexNumbers { def main(args: Array[String]) { val c = new Complex(1.2, 3.4) println("real part: " + c.re) println("imaginary part: " + c.im) } } # scalac NoArgMethod.scala # scala ComplexNumbers real part: 1.2 imaginary part: 3.4
- 継承とオーバーライド
# cat Inheritance.scala class Complex(real: Double, imaginary: Double) { def re = real def im = imaginary override def toString() = "" + re + (if (im < 0) "-" else "+") + im + "i" } object ComplexNumbers { def main(args: Array[String]) { val c = new Complex(1.2, 3.4) println("toString: " + c.toString()) } } # scalac Inheritance.scala # scala ComplexNumbers toString: 1.2+3.4i
今日はここまで!