Kentaro Kuribayashi's blog

Software Engineering, Management, Books, and Daily Journal.

[diary] 2012-02-08 (Wed) - Scala

I've been interested in Scala, while not started studying yet. Today, I found Effective Scala by Twitter and I thought it was time for me to start it. I almost didn't know the language, so I read a tutorial as the beginning:

I remember I couldn't understand the concept "pattern matching" when I learned it a bit before. I got it this time by the example below:

abstract class Tree
case class Sum(l: Tree, r: Tree) extends Tree
case class Var(n: String) extends Tree
case class Con(v: Int) extends Tree

object Foo {
  type Environment = String => Int

  def eval(t: Tree, env: Environment): Int = t match {
    case Sum(l, r) => eval(l, env) + eval(r, env)
    case Var(n)    => env(n)
    case Con(v)    => v
  }

  def main(args: Array[String]) {
    val exp: Tree = Sum(Sum(Var("x"), Var("x")), Sum(Con(7), Var("y")))
    val env: Environment = { case "x" => 5 case "y" => 7 }

    println("Expression: " + exp)
    println("Evaluation with x=5, y=7: " + eval(exp, env))
  }
}

Hmm, it looks smart. So, I wonder what to do next. To read the official documents seems better. id:motemen, scala master, taught me to read Scala by Example next. Yeah, I'll try it!!1

By the way, I want/need to learn many technical things. There can't be too much tome to spend. I have to spend time much more effectively.