2016年1月16日土曜日

開発環境

  • 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-d.(No. 3859)を解いてみる。

その他参考書籍

Exercises 1-d.(No. 3859)

コード(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 = s"${maker} ${name}"
}

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 g1 = new Game("game1", "maker2", List(c1, c2))
val g2 = new Game("game1", "maker1", List(c1, c3))
val g3 = new Game("game2", "maker2", List(c2, c4))
val g4 = new Game("game2", "maker1", List(c3, c4))
val games = List(g4, g1, g3, g2)

for (game <- games sortBy (game => s"${game.maker}${game.name}")) println(game)

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

$ scala-2.11 sample1_d.scala
maker1 game1
maker1 game2
maker2 game1
maker2 game2
$

0 コメント:

コメントを投稿