David
01/15/2025, 1:48 PMEither<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?phldavies
01/15/2025, 3:42 PMval a = either { forEachAccumulating(config.listOfItems) { it.execute().bind() } }
@OptIn(ExperimentalRaiseAccumulateApi::class)
val b = accumulate(::either) { config.listOfItems.forEach { it.execute().bind() } }
either of these should workphldavies
01/15/2025, 3:48 PMEither<NonEmptyList<Error>, Unit>
David
01/15/2025, 3:49 PMphldavies
01/15/2025, 3:49 PM