-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfpinCh05.scala
450 lines (402 loc) · 12.3 KB
/
fpinCh05.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
//
// scalac fpinCh05.scala ; scala ch05
sealed trait Stream[+T] {
def toList: List[T]
def take(n: Int): Stream[T]
def takeWhile(satisfies: T => Boolean): Stream[T]
def headOption: Option[T]
final def exists(satisfies: T => Boolean): Boolean =
this match {
case Cons(hF, tF) => satisfies(hF()) || tF().exists(satisfies)
case _ => false
}
final def foldRight[R](zero: => R)(op: (T, => R) => R): R =
this match {
case Empty => zero
case Cons(hF, tF) => op(hF(), tF().foldRight(zero)(op))
}
@annotation.tailrec
final def forall(satisfies: T => Boolean): Boolean =
this match {
case Empty => true
case Cons(hF, tF) => {
if (satisfies(hF())) tF().forall(satisfies)
else false
}
}
final def map[R](f: T => R): Stream[R] =
this.foldRight(Empty: Stream[R])(
(a, b) => Stream.cons(f(a), b))
final def filter(satisfies: T => Boolean): Stream[T] =
this.foldRight(Empty: Stream[T])(
(a, b) => {
if (satisfies(a)) Stream.cons(a, b)
else b
})
final def append[T2 >: T](next: => Stream[T2]): Stream[T2] =
this.foldRight(next)(Stream.cons(_, _))
final def flatMap[R](f: T => Stream[R]): Stream[R] =
this.foldRight(Empty: Stream[R])((a, b) => f(a).append(b))
final def find(satisfies: T => Boolean): Option[T] =
this.filter(satisfies).headOption
}
case object Empty extends Stream[Nothing] {
def toList: List[Nothing] = Nil
def take(n:Int): Stream[Nothing] = this
def takeWhile(satisfies: Nothing => Boolean) = this
def headOption: Option[Nothing] = None
}
// This declaration below does *not* work:
// case class Cons[+T](headExpr: => T, tailExpr: => Stream[T]) extends Stream[T]
// val parameters to a case class cannot be call-by-name. Therefore we send in
// function values (a loophole!)
case class Cons[+T](headF: () => T, tailF: () => Stream[T]) extends Stream[T] {
def toList: List[T] = headF() :: (tailF().toList)
def take(n:Int): Stream[T] = {
if (n <= 0) Empty
else Cons(headF, () => tailF().take(n-1))
}
def takeWhile(satisfies: T => Boolean): Stream[T] = {
if (satisfies(headF())) Cons(headF, () => tailF().takeWhile(satisfies))
else Empty
}
def headOption: Option[T] = Some(headF())
}
object Stream {
def cons[T](headExpr: => T, tailExpr: => Stream[T]): Stream[T] = {
// converts the expressions, which could
// would be called every time
lazy val headValue = headExpr
lazy val tailValue = tailExpr
Cons(() => headValue, () => tailValue)
}
def empty[T]: Stream[T] = Empty
def emptyLike[T](template: T): Stream[T] = Empty
def apply[T](xs: T*): Stream[T] =
if (xs.isEmpty) empty else cons(xs.head, apply(xs.tail: _*))
def headOption[T](xs: Stream[T]): Option[T] =
xs match {
case Cons(hF, tF) => Some(hF())
case Empty => None
}
def tailOption[T](xs: Stream[T]): Option[Stream[T]] =
xs match {
case Cons(hF, tF) => Some(tF())
case Empty => None
}
def constant[T](x: T): Stream[T] = Stream.cons(x, constant(x))
val ones = constant(0)
def from(n: Int = 0): Stream[Int] = Stream.cons(n, from(n + 1))
def test {
val s = Stream.cons(1, Stream.empty)
assert(headOption(s) == Some(1))
val xs = Stream.apply(3,2,1)
assert(headOption(xs) == Some(3))
}
}
object ex51 {
def test {
val s = Stream.cons("cat", Stream.empty)
assert(s.toList == List("cat"))
val xs = Stream.apply(3,2,1)
assert(xs.toList == List(3,2,1))
assert(Stream.empty.toList == Nil)
}
}
object ex52 {
def test {
val s = Stream.cons("cat", Stream.empty)
assert(s.take(1).toList == List("cat"))
val xs = Stream.apply(3,2,1)
assert(xs.take(2).toList == List(3,2))
assert(xs.take(0).toList == Nil)
assert(xs.take(10).toList == List(3,2,1))
assert(Stream.empty.take(0).toList == Nil)
}
}
object ex53 {
def test {
val s = Stream.cons("cat", Stream.empty)
assert(s.takeWhile(_ == "cat").toList == List("cat"))
assert(s.takeWhile(_ == "dog").toList == Nil)
val xs = Stream.apply(3,2,1)
assert(xs.takeWhile(_ > 1).toList == List(3,2))
assert(xs.takeWhile(_ > 0).toList == List(3,2,1))
assert(xs.takeWhile(_ > 5).toList == Nil)
}
}
object ch05 {
object lazyPlay {
def if2[T](cond: Boolean,
onTrue: => T, onFalse: => T): T = {
if (cond) onTrue else onFalse
}
var counter = 0
def called(x: Int): Int = {
counter = counter + 1
x
}
def test {
assert(counter == 0)
called(1)
assert(counter == 1)
if2(true, 1, called(2))
assert(counter == 1)
if2(true, called(1), called(2))
assert(counter == 2)
}
}
object testFoldRight {
def contains[T](xs: Stream[T])(satisfies: T => Boolean): Boolean =
xs.foldRight(false)((x,y) => satisfies(x) || y)
def test {
val xs = Stream(1,2,3)
assert(xs.foldRight(0)(_ + _) == 6)
assert(contains(xs)(_ > 2))
assert(!contains(xs)(_ > 4))
}
}
object ex54 {
def test {
val xs = Stream(2,4,6)
assert(xs.forall(_ % 2 == 0))
assert(! Stream(2,5,6).forall(_ % 2 == 0))
}
}
object ex55 {
def takeWhile[T](xs: Stream[T])(satisfies: T=> Boolean): Stream[T] =
xs.foldRight(Stream.empty: Stream[T])(
(a, b) => {
if (!satisfies(a)) Stream.empty
else Stream.cons(a, takeWhile(b)(satisfies))
})
def test {
val xs = Stream(1,2,3)
assert(takeWhile(xs)(_ < 3).toList == List(1,2))
assert(takeWhile(xs)(_ < 2).toList == List(1))
assert(takeWhile(xs)(_ > 5).toList == Nil)
}
}
object ex56 {
def headOption[T](xs: Stream[T]): Option[T] =
xs.foldRight(None: Option[T])((a, b) => Some(a))
def test {
val xs = Stream(1,2,3)
assert(headOption(xs) == Some(1))
assert(headOption(Stream.empty) == None)
}
}
object ex57 {
def toDbledStream[T](x: T): Stream[T] = Stream(x, x)
def test {
assert(Stream(1,2,3).map(2 * _).toList == List(2,4,6))
assert(Stream.emptyLike(1).map(2 * _).toList == Nil)
assert(Stream("a","b").map(_.toUpperCase).toList == List("A","B"))
assert(Stream(1,2,3).filter(_ > 2).toList == List(3))
assert(Stream(1,2,3).filter(_ % 2 == 1).toList == List(1,3))
assert(Stream(1,2,3).filter(_ > 5).toList == Nil)
assert(Stream(1,2).append(Stream(3)).toList == List(1,2,3))
assert(Stream().append(Stream(3)).toList == List(3))
assert(Stream(1,2).append(Stream()).toList == List(1,2))
assert(Stream(1,2).flatMap(toDbledStream(_)).toList == List(1,1,2,2))
assert(Stream(1,2,3).find(_ > 1).getOrElse(-1) == 2)
assert(Stream(1,2,3).find(_ > 4).getOrElse(-1) == -1)
}
}
object ex58 {
def test {
assert(Stream.constant(7).take(3).toList == List(7,7,7))
assert(Stream.constant(5).map(2*_).take(3).toList == List(10,10,10))
}
}
object ex59 {
def test {
assert(Stream.from(0).take(3).toList == List(0,1,2))
assert(Stream.from(0).find(_ > 20).getOrElse(-1) == 21)
}
}
object ex510 {
def fibs: Stream[Int] = {
def fibStep(lastTwo: (Int, Int)): Stream[(Int, Int)] = {
val nextTwo = (lastTwo._2, lastTwo._1 + lastTwo._2)
Stream.cons(nextTwo, fibStep(nextTwo))
}
val fibSteps: Stream[(Int, Int)] = Stream.cons((0,1), fibStep((0, 1)))
// In this arrangement we compute the n+1th fib number before returning the nth.
fibSteps.map(_._1)
}
def test {
assert(fibs.take(7).toList == List(0, 1, 1, 2, 3, 5, 8))
}
}
object ex511 {
def unfold[R, S](startingState: S)(step: S => Option[(R, S)]): Stream[R] = {
step(startingState) match {
case Some((newResult, nextState)) => Stream.cons(newResult, unfold(nextState)(step))
case None => Stream.empty
}
}
case class FibState(lagging: Int, leading: Int)
def fibStep(state: FibState): (Int, FibState) =
(state.lagging, FibState(state.leading, state.leading + state.lagging))
def fibs: Stream[Int] = unfold(FibState(0,1))(state => Some(fibStep(state)))
def from(n: Int): Stream[Int] =
unfold(n)(x => Some(Tuple2(x, x + 1)))
def constant[T](theValue: T): Stream[T] =
unfold(None)(x => Some(Tuple2(theValue, None)))
def test {
assert(fibs.take(7).toList == List(0, 1, 1, 2, 3, 5, 8))
}
}
object ex512 {
import ex511._
def test {
assert(fibs.take(7).toList == List(0, 1, 1, 2, 3, 5, 8))
assert(from(0).take(3).toList == List(0,1,2))
assert(from(0).find(_ > 20).getOrElse(-1) == 21)
assert(constant(7).take(3).toList == List(7,7,7))
assert(constant(5).map(2*_).take(3).toList == List(10,10,10))
// ommitted ones test. it is just constant(1)
}
}
object ex513 {
import ex511.unfold
def map[T,R](xs: Stream[T])(f: T => R): Stream[R] =
unfold(xs)(x => x match {
case Cons(hF, tF) => Some(Tuple2(f(hF()), tF()))
case Empty => None
})
def zip[A, B](as: Stream[A], bs: Stream[B]): Stream[(A, B)] =
unfold(Tuple2(as, bs))({
case Tuple2(Cons(ahF, atF), Cons(bhF, btF)) => {
val heads = (ahF(), bhF())
val tails = (atF(), btF())
Some(heads, tails)
}
case _ => None
})
def zipAll[A, B](as: Stream[A], bs: Stream[B]): Stream[(Option[A], Option[B])] =
unfold(Tuple2(as,bs))({
case Tuple2(Cons(ahF, atF), Cons(bhF, btF)) => {
val heads = (Some(ahF()), Some(bhF()))
val tails = (atF(), btF())
Some(heads, tails)
}
case Tuple2(Cons(ahF, atF), Empty) => {
val heads = (Some(ahF()), None)
val tails = (atF(), Empty)
Some(heads, tails)
}
case Tuple2(Empty, Cons(bhF, btF)) => {
val heads = (None, Some(bhF()))
val tails = (Empty, btF())
Some(heads, tails)
}
case _ => None
})
def zipWith[A, B, R](as: Stream[A], bs: Stream[B])
(op: (A, B) => R): Stream[R] = zip(as, bs).map({case (a,b) => op(a,b)})
def take[T](xs: Stream[T], n:Int): Stream[T] =
unfold(zip(Stream.from(0), xs))({
case Cons(headsF, tailsF) => {
val (i, x) = headsF()
if (i < n) Some((x, tailsF())) else None
}
case Empty => None
} )
def takeWhile[T](xs: Stream[T])(satisfies: T => Boolean): Stream[T] =
unfold(xs)({
case Cons(headsF, tailsF) => {
val theHead = headsF()
if (satisfies(theHead)) Some((theHead, tailsF())) else None
}
case Empty => None
})
def test {
assert(map(Stream(1,2,3))(x => x).toList == List(1,2,3))
assert(map(Stream(1,2,3))(x => 2*x).toList == List(2,4,6))
assert(map(Stream.from(0))(x => 2*x).take(3).toList == List(0,2,4))
val zipped = zip(Stream.from(0), Stream("a","b","c"))
assert(zipped.toList == List((0, "a"), (1, "b"), (2, "c")))
val zippedWith = zipWith(Stream.from(0), Stream.constant(2))(_ + _)
assert(zippedWith.take(4).toList == List(2,3,4,5))
assert(take(Stream.from(2), 3).toList == List(2,3,4))
assert(takeWhile(Stream.from(0))(_ < 3).toList == List(0,1,2))
val allZipped = zipAll(Stream(1,2,3,4), Stream(1,2))
assert(allZipped.toList == List((Some(1), Some(1)),
(Some(2),Some(2)), (Some(3), None), (Some(4), None) ))
}
}
object ex513alt {
def zip[A, B](as: Stream[A], bs: Stream[B]): Stream[(A, B)] = ???
def zipAll[A, B](as: Stream[A], bs: Stream[B]): Stream[(Option[A], Option[B])] = ???
def take[T](xs: Stream[T], n:Int): Stream[T] = ??? // could do same as with a list.
def takeWhile[T](xs: Stream[T])(satisfies: T => Boolean): Stream[T] = xs match {
case Cons(hF, tF) => {
lazy val theHead = hF()
if (satisfies(theHead)) {
Stream.cons(theHead, takeWhile(tF())(satisfies) )
} else {
takeWhile(tF())(satisfies)
}
}
case Empty => Empty
}
}
object ex514 {
import ex513._
def check[T](ox: Option[T], oq: Option[T]): Boolean = {
ox match {
case None => false
case Some(x) => oq match {
case Some(q) => x == q
case None => true
}
}
}
def startsWith[T](xs: Stream[T], query: Stream[T]): Boolean = {
val zipped = zipAll(xs, query)
zipped.takeWhile({
case (_, Some(q)) => true
case _ => false
}).forall({
case (ox, oq) => check(ox, oq)
})
}
def test {
assert(startsWith(Stream(1,2,3), Stream(1,2)))
}
}
object ex515 {
import ex511.unfold
def tails[T](xs: Stream[T]): Stream[Stream[T]] =
unfold(xs)(rest => rest match {
case Empty => None
case Cons(headF, tailF) => Some((rest, tailF()))
}).append(Stream(Empty: Stream[T]))
def test {
val xstails = tails(Stream(1,2,3))
assert(xstails.map(_.toList).toList == List(List(1,2,3), List(2,3), List(3), List()))
}
}
def main(args: Array[String]) {
Stream.test
lazyPlay.test
ex51.test
ex52.test
ex53.test
testFoldRight.test
ex54.test
ex55.test
ex56.test
ex57.test
ex58.test
ex59.test
ex510.test
ex511.test
ex512.test
ex513.test
ex514.test
ex515.test
}
}