-
Notifications
You must be signed in to change notification settings - Fork 0
/
fpinCh06.scala
288 lines (257 loc) · 7.04 KB
/
fpinCh06.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
object ch06 {
val rng = new scala.util.Random
def rollDie(rng: scala.util.Random): Int = rng.nextInt(6) + 1
trait RNG {
def nextInt: (Int, RNG)
}
case class SimpleRNG(seed: Long) extends RNG {
def nextInt: (Int, RNG) = {
val newSeed = (seed * 0x5DEECE66DL + 0xBL) & 0xFFFFFFFFFFFFL
val nextRNG = SimpleRNG(newSeed)
val n = (newSeed >>> 16).toInt
(n, nextRNG)
}
}
def randomPair(rng: RNG): ((Int, Int), RNG) = {
val (i1, rng2) = rng.nextInt
val (i2, rng3) = rng2.nextInt
((i1, i2), rng3)
}
// For the most part can just negate negative numbers
// to turn them to positive. But..
// 1. zero only happens once
// 2. Int.MinValue has a bigger abs.value than Int.MaxValue
// So we solve both problems by mapping Int.MinValue to 0.
// Really should be adding this function to the trait.
def nonNegativeInt(rng: RNG): (Int, RNG) = {
val (i, rng2) = rng.nextInt
if (i == Int.MinValue) {
(0, rng2)
} else if (i < 0) {
(-i, rng2)
} else (i, rng2)
}
object ex61 {
def test {
val rng = SimpleRNG(42)
val (i1, rng2) = nonNegativeInt(rng)
val (i2, rng3) = nonNegativeInt(rng2)
val (i3, rng4) = nonNegativeInt(rng3)
assert(List(i1,i2,i3).forall(_ >= 0))
}
}
def double(rng: RNG): (Double, RNG) = {
val (i, rng2) = nonNegativeInt(rng)
val x = i.toDouble / Int.MaxValue.toDouble
(x, rng2)
}
object ex62 {
def test {
val rng = SimpleRNG(42)
val (x1, rng2) = double(rng)
val (x2, rng3) = double(rng2)
val (x3, rng4) = double(rng3)
assert(List(x1,x2,x3).forall(x => (x >= 0.0) && (x <= 1.0)))
}
}
object ex63 {
def get2Rands[A, B](rng: RNG,
fA: RNG => (A, RNG),
fB: RNG => (B, RNG)): ((A, B), RNG) = {
val (a, rng2) = fA(rng)
val (b, rng3) = fB(rng2)
((a, b), rng3)
}
def intAndDouble(rng: RNG): ((Int, Double), RNG) =
get2Rands(rng, _.nextInt, double(_))
def doubleAndInt(rng: RNG): ((Double, Int), RNG) =
get2Rands(rng, double(_), _.nextInt)
def threeDoubles(rng: RNG): ((Double, Double, Double), RNG) = {
val ((d1, d2), rng3) = get2Rands(rng, double(_), double(_))
val (d3, rng4) = double(rng3)
((d1, d2, d3), rng4)
}
def test {
val rng = SimpleRNG(42)
def testIntAndDouble {
val ((i,x), rng2) = intAndDouble(rng)
}
testIntAndDouble
def testDoubleAndInt {
val ((x,i), rng2) = doubleAndInt(rng)
}
testDoubleAndInt
def testThreeDoubles {
val ((x1, x2, x3), rng2) = threeDoubles(rng)
}
testThreeDoubles
}
}
object ex64 {
def ints(count: Int)(rng: RNG): (List[Int], RNG) = {
@annotation.tailrec
def getNextInt(result: List[Int], rng: RNG, remaining: Int): (List[Int], RNG) = {
if (remaining <= 0) (result, rng) else {
val (i, nextRNG) = rng.nextInt
getNextInt(i :: result, nextRNG, remaining - 1)
}
}
getNextInt(Nil, rng, count)
}
def test {
val rng = SimpleRNG(42)
val (xs, rng2) = ints(4)(rng)
assert(xs.length == 4)
}
}
type Rand[+T] = RNG => (T, RNG)
val int: Rand[Int] = _.nextInt
// a pass-through
def unit[T](a: T): Rand[T] = rng => (a, rng)
def map[T, R](rand: Rand[T])(f: T => R): Rand[R] =
rng => {
val (x, rng2) = rand(rng)
(f(x), rng2)
}
def nonNegativeEven: Rand[Int] = map(nonNegativeInt)(i => i - i % 2)
object ex65 {
def double: Rand[Double] = map(int)(i => {
if (i == Int.MinValue) 0.0 else (i.abs.toDouble / Int.MaxValue.toDouble)
})
def test {
val rng = SimpleRNG(42)
val (x, rng2) = double(rng)
assert(x <= 1.0)
assert(x >= 0.0)
}
}
def map2[A, B, C](ra: Rand[A], rb: Rand[B])(op: (A, B) => C): Rand[C] =
rng => {
val (a, rng2) = ra(rng)
val (b, rng3) = rb(rng2)
(op(a, b), rng3)
}
object ex66 {
def test {
val rng = SimpleRNG(42)
val twoInts = map2(int, int)((_, _))
val ((i, j), rng2) = twoInts(rng)
val (i1, rng_1) = int(rng)
val (i2, rng_2) = int(rng_1)
assert(i == i1)
assert(j == i2)
}
}
def both[A, B](ra: Rand[A], rb:Rand[B]): Rand[(A, B)] = map2(ra, rb)((_, _))
val randIntDouble: Rand[(Int, Double)] = both(int, double)
val randDoubleInt: Rand[(Double, Int)] = both(double, int)
@annotation.tailrec
def randSeqStepper[T](results: List[T], rands: List[Rand[T]], rng: RNG): (List[T], RNG) =
rands match {
case r :: restOfRands => {
val (x, newRNG) = r(rng)
randSeqStepper(x :: results, restOfRands, newRNG)
}
case Nil => (results.reverse, rng)
}
def sequence[T](fs: List[Rand[T]]): Rand[List[T]] =
rng => randSeqStepper(Nil, fs, rng)
object ex67 {
def ints(n: Int): Rand[List[Int]] = sequence(List.fill(n)(int))
def testMixed {
val threeMixedInts = sequence(List(int, nonNegativeInt, nonNegativeEven): List[Rand[Int]])
val rng = SimpleRNG(42)
val (xs, rngEnd) = threeMixedInts(rng)
val (i1, rng2) = int(rng)
val (i2, rng3) = nonNegativeInt(rng2)
val (i3, rng4) = nonNegativeEven(rng3)
assert(xs == List(i1, i2, i3))
}
def testInts {
val threeInts = ints(3)
val rng = SimpleRNG(42)
val (xs, rngEnd) = threeInts(rng)
val (i1, rng2) = int(rng)
val (i2, rng3) = int(rng2)
val (i3, rng4) = int(rng3)
assert(xs == List(i1, i2, i3))
}
def test {
testInts
testMixed
}
}
def nonNegativeLessThan(n: Int): Rand[Int] = {
@annotation.tailrec
def tryNonNegativeLessThan(rng: RNG): (Int, RNG) = {
val (i, nextRNG) = nonNegativeInt(rng)
if (i / n >= Int.MaxValue / n) {
tryNonNegativeLessThan(nextRNG)
} else (i % n, nextRNG)
}
rng => tryNonNegativeLessThan(rng)
}
def flatMap[T, R](rand: Rand[T])(f: T => Rand[R]): Rand[R] =
rng => {
val (x, nextRNG) = rand(rng)
val newRand = f(x)
newRand(nextRNG)
}
object ex68 {
def nonNegativeLessThan(n:Int): Rand[Int] =
flatMap(nonNegativeInt)(i =>
if ((i / n) >= (Int.MaxValue / n)){
nonNegativeLessThan(n)
} else unit(i))
def testInt {
val intAgain = flatMap(int)(unit)
val rng = SimpleRNG(42)
val (i, rng2) = intAgain(rng)
val (ip, rng2p) = int(rng)
assert(i == ip)
assert(rng2 == rng2p)
}
def test {
testInt
val cap = Int.MaxValue / 2 + Int.MaxValue / 10
val lessThanCap = nonNegativeLessThan(cap)
val rng = SimpleRNG(42)
val tenLessThans = sequence(List.fill(10)(lessThanCap))
val (xs, rng2) = tenLessThans(rng)
assert(xs.forall(_ < cap))
}
}
object ex69 {
def map[T,R](rand: Rand[T])(f: T => R): Rand[R] =
flatMap(rand)(x => unit(f(x)))
def map2[A, B, C](ra: Rand[A], rb: Rand[B])(op: (A, B) => C): Rand[C] =
flatMap(ra)(a =>
flatMap(rb)(b =>
unit(op(a,b))
)
)
// Can't do this because flatMap is not defined as a method on Rand:
// (but otherwise would work)
// def map2[A, B, C](ra: Rand[A], rb: Rand[B])(op: (A, B) => C): Rand[C] =
// for (a <- ra; b <- rb) yield op(a, b)
def test {
val rng = SimpleRNG(42)
val twoInts = map2(int, nonNegativeInt)(_ + _)
val (theSum, rng2) = twoInts(rng)
val (i1, rng_1) = int(rng)
val (i2, rng_2) = nonNegativeInt(rng_1)
assert(theSum == (i1 + i2))
}
}
def main(args: Array[String]) {
ex61.test
ex62.test
ex63.test
ex64.test
ex65.test
ex66.test
ex67.test
ex68.test
ex69.test
}
}