Is there any idiomatic way of dropping
Either<A, B>
and replacing it with
Either<A, Unit>
? I'm thinking along the way how
merge()
looks instead of
fold(::identity, ::identity)
. My case is as follows:
val maybeListOfError = config.listOfItems.map {
it.execute() // Returns Either<Error, A>
}
.flattenOrAccumulate() // Either<List<Error>, List<A>>
.onLeft { // Do some recovery }
.map { }
// I have to to return the list of errors later, if present, but first do some other stuff.
...
maybeListOfError.mapLeft { MyCustomError(it) }.bind()
...
I want to return
Either<List<Error>, Unit>
, but I don't feel
map { }
conveys that clearly. I guess I could use
leftOrNull()
but then it doesn't turn out so nice in a either effect context:
ensure(maybeListOfError == null) { MyCustomError(result!!) }
forces me to use
!!
and the other alternative:
if(maybeListOfError != null)
raise(MyCustomError(maybeListOfError))
}
gives IDE hint of replacing it with ensure again.
Maybe I'm missing something?