---- (off topic from the previous question) ---
Let's say I have a function like this:
fun doSomething(name:String): Either<FailReason,Int>
and I have a
names:List<String>
.
I could do
val result:List<Either<FailReason,Int>> = names.map { doSomething(it) }
which is going to give me a list of
Either
instances. Fine.
Now let's assume I want the iteration to stop upon the first case that
doSomething()
returns a left. This could be done using a sequence like this:
val result2:Either<FailReason,Int> = names.asSequence().map { doSomething(it) }.first { it is Either.Left }
but it's going to return the first left that fails, or the right that results from the last call to
doSomething()
. All intermediate "right" values will be discarded.
What if I wanted it to return
Either<FailReason,List<Int>>
? Essentially, return the first left (terminating any subsequent calls to
doSomething()
), or a list of all the rights? Does Arrow provide any help here? (Also I'm not looking for a solution involving while loops or mutable variables.)