Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug with Streaming#thunk. #683

Merged
merged 3 commits into from
Nov 20, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions core/src/main/scala/cats/data/Streaming.scala
Original file line number Diff line number Diff line change
Expand Up @@ -808,11 +808,11 @@ object Streaming extends StreamingInstances {
* Continually return the result of a thunk.
*
* This method only differs from `continually` in that the thunk may
* not be pure. The stream is memoized to ensure that repeated
* traversals produce the same results.
* not be pure. Thus, repeated traversals may produce different
* results.
*/
def thunk[A](f: () => A): Streaming[A] =
knot(s => Cons(f(), s), memo = true)
knot(s => Cons(f(), s), memo = false)

/**
* Produce an infinite stream of values given an initial value and a
Expand Down
10 changes: 10 additions & 0 deletions tests/src/test/scala/cats/tests/StreamingTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,16 @@ class AdHocStreamingTests extends CatsSuite {
}
}

test("thunk is evaluated for each item") {
// don't want the stream to be too big
implicit val arbInt = Arbitrary(Gen.choose(-10, 20))
forAll { (start: Int, end: Int) =>
var i = start - 1
val stream = Streaming.thunk{ () => i += 1; i}.takeWhile(_ <= end)
stream.toList should === ((start to end).toList)
}
}

test("interval") {
// we don't want this test to take a really long time
implicit val arbInt: Arbitrary[Int] = Arbitrary(Gen.choose(-10, 20))
Expand Down