-
Notifications
You must be signed in to change notification settings - Fork 54
Open
Description
When I use "modify", "each" and "using" with a side-effectful function on a Stream, the result is surprising.
Consider this minimal example:
import com.softwaremill.quicklens.{modify, QuicklensEach}
import scala.collection.immutable.Stream.Empty
import scala.collection.mutable
case class A(x: Int)
val regularSeq = Seq(A(1), A(3))
val lazySeq1 = Seq(A(1), A(3)).groupBy(_.x).mapValues(_.head).values.toSeq
val lazySeq2 = Stream.cons(A(1), Stream.cons(A(3), Empty))
def test(s: Seq[A]): Unit = {
val buffer = mutable.ArrayBuffer[Int]()
val modifier1 = modify(_: Seq[A])(_.each.x).using { e =>
buffer += e
e
}
val modifier2 = modify(_: Seq[A])(_.each.x).using(_ + 1)
modifier1(s)
println(buffer)
println(modifier2(s))
}
test(regularSeq) // output: ArrayBuffer(1, 3)
// List(A(2), A(4))
test(lazySeq1). // output: ArrayBuffer(1)
// Stream(A(2), ?)
test(lazySeq2). // output: ArrayBuffer(1)
// Stream(A(2), ?)The surprising part is that the buffer which is filled as a side-effect only contains the first element, and - in contrast to the resulting Stream - there is no indication that this may not be the complete result.
I understand that this side-effectful usage is not optimal, but since it is possible, I would appreciate more feedback from the library that I may be missing some elements here.
P.S.: Quicklens is a great tool, thank you very much for your efforts!
Metadata
Metadata
Assignees
Labels
No labels