2016年1月15日金曜日

開発環境

  • OS X El Capitan - Apple (OS)
  • Emacs (Text Editor)
  • Scala (プログラミング言語)

Learning Scala: Practical Functional Programming for the JVM (Jason Swartz (著)、O'Reilly Media)のPart 2. (Object-Oriented Scala)、Chapter 8.(Classes)、Exercises 1-c.(No. 3847)を解いてみる。

その他参考書籍

Exercises 1-c.(No. 3847)

コード(Emacs)

import java.util.Date

class Console(val make:String, val model:String, val debut:Date,
  val wifi_type:Option[String], val formats:List[String], val resolution:Int) {

  override def toString = s"make: $make, model: $model, debut: $debut, " +
  s"wifi type: $wifi_type, formats: $formats, resolution: $resolution"
}

class Game(val name:String, val maker:String, val consoles:List[Console]) {
  def isSupported(console:Console) = consoles contains console
  override def toString = name.toString
}

val c1 = new Console("make1", "model1", new Date(), Option("b/g"),
  List("format0", "format1"), 10)
val c2 = new Console("make2", "model2", new Date(), Option("b/g"),
  List("format0", "format1"), 10)
val c3 = new Console("make3", "model3", new Date(), Option("b/g"),
  List("format0", "format1"), 10)
val c4 = new Console("make4", "model4", new Date(), Option("b/g"),
  List("format0", "format1"), 10)

val g = new Game("game", "maker", List(c1, c2))

println(g)
println(g.isSupported(c1))
println(g.isSupported(c2))
println(g.isSupported(c3))
println(g.isSupported(c4))

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

$ scala-2.11 sample1_c.scala
game
true
true
false
false
$

0 コメント:

コメントを投稿