2015年9月18日金曜日

開発環境

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

その他参考書籍

Exercises 7.(No. 1889)

コード(Emacs)

def conditional[T](x:T, p:T => Boolean, y:String):String = {
  if (p(x))
    y
  else
    ""
}

for (n <- 1 to 100) {
  var s = conditional[Int](n,  _ % 3 == 0, "type") +
          conditional[Int](n, _ % 5 == 0, "safe")
  if (s == "") s = s"$n"
  println(s)
}

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

$ scala-2.11 sample7.scala
1
2
type
4
safe
type
7
8
type
safe
11
type
13
14
typesafe
16
17
type
19
safe
type
22
23
type
safe
26
type
28
29
typesafe
31
32
type
34
safe
type
37
38
type
safe
41
type
43
44
typesafe
46
47
type
49
safe
type
52
53
type
safe
56
type
58
59
typesafe
61
62
type
64
safe
type
67
68
type
safe
71
type
73
74
typesafe
76
77
type
79
safe
type
82
83
type
safe
86
type
88
89
typesafe
91
92
type
94
safe
type
97
98
type
safe
$

0 コメント:

コメントを投稿