Skip to content

Commit

Permalink
Merge pull request #683 from non/bug/fix-streaming-thunk
Browse files Browse the repository at this point in the history
Fix bug with Streaming#thunk.
  • Loading branch information
adelbertc committed Nov 20, 2015
2 parents d2a62be + e22c15d commit b07df91
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
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 @@ -815,11 +815,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 @@ -250,6 +250,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

0 comments on commit b07df91

Please sign in to comment.