2015年8月19日水曜日

開発環境

Learning Scala: Practical Functional Programming for the JVM (Jason Swartz (著)、O'Reilly Media)のPart Ⅰ. (Core Scala)、Chapter 4.(Functions)、Exercises 7.(No. 1471)を解いてみる。

その他参考書籍

Exercises 7.(No. 1471)

コード(Emacs)

def firstPosition[T, S](x:(T, S)):(Any, Any) = {
  val x1 = x._1
  val x2 = x._2
  val a: Boolean = x1.isInstanceOf[Int]
  val b: Boolean = x2.isInstanceOf[Int]
  (a, b) match {
    case (true, _) => x
    case (_, true)  => (x2, x1)
    case _ => x
  }
}

println(firstPosition((1, 2)))
println(firstPosition((1, 1.2)))
println(firstPosition((1.2,1)))
println(firstPosition((1.2,1.2)))

入出力結果(Terminal, REPL(Read, Eval, Print, Loop))

$ scala-2.11 sample7.scala
(1,2)
(1,1.2)
(1,1.2)
(1.2,1.2)
$

0 コメント:

コメントを投稿