forked from fpinscala/fpinscala
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jeremy.Stone
authored and
Jeremy.Stone
committed
Nov 5, 2014
1 parent
ecda0f8
commit 650cfc3
Showing
5 changed files
with
19 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,12 @@ | ||
def hasSubsequence[A](s: Stream[A]): Boolean = | ||
tails exists (_ startsWith s) | ||
/* | ||
The function can't be implemented using `unfold`, since `unfold` generates elements of the `Stream` from left to right. It can be implemented using `foldRight` though. | ||
The implementation is just a `foldRight` that keeps the accumulated value and the stream of intermediate results, which we `cons` onto during each iteration. When writing folds, it's common to have more state in the fold than is needed to compute the result. Here, we simply extract the accumulated list once finished. | ||
*/ | ||
def scanRight[B](z: B)(f: (A, => B) => B): Stream[B] = | ||
foldRight((z, Stream(z)))((a, p0) => { | ||
// p0 is passed by-name and used in by-name args in f and cons. So use lazy val to ensure only one evaluation... | ||
lazy val p1 = p0 | ||
val b2 = f(a, p1._1) | ||
(b2, cons(b2, p1._2)) | ||
})._2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
- | ||
The function can't be implemented using `unfold`, since `unfold` generates elements of the `Stream` from left to right. It can be implemented using `foldRight` though. |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters