In optics, is there a way to focus on a `Either` o...
# arrow
l
In optics, is there a way to focus on a
Either
only if it is a
Left
? I have a state that I want to modify its
Either
variable, something like:
Copy code
data class State(val either: Either<Error, Something>)
And I want something like this:
Copy code
State.either./*focusIfLeft()*/.modify(state) { left: Left<Error> -> Something().right() }
Is there a way to do that already or a need to define my own extension?
I wrote this for now which seems to work, but I was wondering if something already existed.
Copy code
fun <T, R, L> Lens<T, Either<L, R>>.onLeft(): POptional<T, T, L, Either<L, R>> = plus(
    POptional(
        getOrModify = { (it as? Either.Left<L>)?.value?.right() ?: it.left() },
        set = { _, newValue -> newValue }
    )
)
t
I think you need a prism for that