開発環境
- OS X Yosemite - Apple (OS)
- Emacs (Text Editor)
- Scala (プログラミング言語)
Learning Scala: Practical Functional Programming for the JVM (Jason Swartz (著)、O'Reilly Media)のPart Ⅰ. (Core Scala)、Chapter 5.(First-Class Functions)、Exercises 2.(No. 1874)を解いてみる。
その他参考書籍
Exercises 2.(No. 1874)
コード(Emacs)
import util.Random.nextInt
val max = (a:Int, b:Int) => {
if (a > b) a
else b
}
val min = (a:Int, b:Int) => {
if (a > b) b else a
}
val apply = (f:(Int, Int) => Int, a:Int, b:Int) => f(a, b)
val (x, y) = (nextInt, nextInt)
println(s"$x, $y")
println(s"max: ${max(x, y)}")
println(apply(max, x, y))
println(s"min: ${min(x, y)}")
println(apply(min, x, y))
println(s"second: ${apply((a:Int, b:Int) => b, x, y)}")
入出力結果(Terminal, REPL(Read, Eval, Print, Loop))
$ scala-2.11 sample2.scala -945818191, -2060746920 max: -945818191 -945818191 min: -2060746920 -2060746920 second: -2060746920 $
0 コメント:
コメントを投稿