https://kotlinlang.org logo
#arrow
Title
# arrow
d

dimsuz

08/01/2022, 3:27 PM
If I have
Copy code
val lens1: Lens<S, A>
val lens2: Lens<S, B>
how do I apply them one after another to change field
A
then change field
B
and return
S
?
Copy code
fun modify(s: S): S {
  return lens1.modify { a.doStuff() } ?? lens2.modify { b.doStuff() }
}
i.e. I want not to compose them but to sequence them.
s

simon.vergauwen

08/01/2022, 6:34 PM
d

dimsuz

08/01/2022, 9:07 PM
Nice! I also wonder how this would be done in Haskell. I'm in process of reading the book on optics, didn't see something similar there yet. My hunch is that this looks like "sequencing in context" and this speaks "Monad", but lenses are not monads, so this hunch must be wrong 🙂 Still
copy
in this PR looks like haskell's
do
🙂
s

simon.vergauwen

08/01/2022, 9:09 PM
This uses a mutable
var
in the underlying impl and applies the transformations in sequence
6 Views