my playroom!
https://github.com/keisks/scala-tutorial
references
http://xerial.org/scala-cookbook/
https://www.coursera.org/course/progfun (関数型言語として?のScalaに興味があれば)
http://docs.scala-lang.org/cheatsheets/ (cheat sheet)
http://utcompling.github.io/nlpclass-fall2013/scala/#notes
Scalaをコンパイルせずに実行する
scala Hoge.scala Scala
のコマンドは、object NAME {
で始まるものは実行できない?
→そんなことなかった… objectを作るときははmain関数を書かないとダメということ?
(main関数なしでもsbt compileが通ってしまうのが良くない気がするが…)
val words_org = line.stripLineEnd split ' '
で得られるのはArray。
line.stripLineEnd.split(' ').toList がオススメ?
ファイルの読み込み
import scala.io._
val source = Source.fromFile(filePath, "UTF-8")
source.close()
ファイルの書き込み
import java.io.PrintWriter
val outFile = new PrintWriter("test.txt")
outFile.println("test test")
outFile.close()
Character encoding
(例によって?)NLPでは文字コードとのお付き合いは避けて通ることができません。
export JAVA_OPTS="-Dfile.encoding=UTF-8"
~/.sbtconfig and write SBT_OPTS=-Dfile.encoding=UTF-8
sbtでOutofmemoryが出る。
export SBT_OPTS="-XX:+CMSClassUnloadingEnabled -XX:PermSize=256M -XX:MaxPermSize=512M"
useful functions
.zipWithIndex // pythonのemumerateに相当
http://stackoverflow.com/questions/6486466/is-there-a-scala-equivalent-for-the-python-enumerate
import scala.util.Random
Random.nextInt(N) // N未満の乱数
get cumulative
scala> List(2,4,6,8,10).scanLeft(0){_ + _}
res: List[Int] = List(0, 2, 6, 12, 20, 30)
Double型の時はscanLeft(0.0)にします。(0)だとエラーが出る。
scala> List(1.0, 2.0, 3.0, 4.0, 5.0).scanLeft(0.0){_ + _}
res: List[Double] = List(0.0, 1.0, 3.0, 6.0, 10.0, 15.0)
最終更新:2014年01月12日 06:55