Skip to content

Surprising behaviour on Stream using "modify", "each" and "using" #291

@danile42

Description

@danile42

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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions