Is there any idiomatic way of dropping `Either<A, ...
# arrow
d
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:
Copy code
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:
Copy code
ensure(maybeListOfError == null) { MyCustomError(result!!) }
forces me to use
!!
and the other alternative:
Copy code
if(maybeListOfError != null)  
    raise(MyCustomError(maybeListOfError))
}
gives IDE hint of replacing it with ensure again. Maybe I'm missing something?
p
Copy code
val 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 work
🙌 1
both will return
Either<NonEmptyList<Error>, Unit>
1
d
Ah, the top level accumulate solves it. Thanks!
p
I'm not sure how stable the new accumulate DSL will be - but I'm assuming it won't change much - option a is probably safer for now
👍 1