Skip to content

Streaming.thunk doesn't seem to reevaluate thunk #677

Closed
@ceedubs

Description

@ceedubs

The ScalaDoc for Streaming.thunk says that the thunk may not be pure. However, it doesn't seem to support this use-case. In the following example, I would expect the output to be List(1, 2, 3, 4), but it is List(1, 1, 1, 1).

scala> var i = 0
i: Int = 0

scala> Streaming.thunk{() => i += 1; i}.take(4).toList
res6: List[Int] = List(1, 1, 1, 1)

Here is a test that helped me catch this:

  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)
    }
  }

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions