Question about `sequence`. Is there a way to avoid...
# arrow
g
Question about
sequence
. Is there a way to avoid 2 boilerplate `fix()`s in the below example to get that data type? Is there any on-going development on this, now that
Kind
is being deprecated?
Copy code
val listOfOptionalNumbers: List<Either<Int, Int>> =
     listOf(1.right(), 2.right(),3.right())
val result: Either<Int, List<Int>> = 
     listOfOptionalNumbers.sequence(Either.applicative()).map { it.fix() }.fix()
s
One option is to utilise `either.eager`:
Copy code
fun <E, A> Iterable<Either<E, A>>.sequence(): Either<E, Iterable<A>> =
    either.eager { this@sequence.map { it.bind() } }

listOf(1.right(), 2.right(), 3.right()).sequence()
Or more generally:
Copy code
fun <E, A, B> Iterable<A>.traverse(f: (A) -> Either<E, B>): Either<E, Iterable<B>> =
    either.eager { this@traverse.map { f(it).bind() } }

fun <E, A> Iterable<Either<E, A>>.sequence(): Either<E, Iterable<A>> =
    this.traverse(::identity)
s
These methods will be added in the next version of Arrow, since we want to get rid of
fix
and by extension
Kind
. This is the PR for
Either
. https://github.com/arrow-kt/arrow-core/pull/272
😍 1
👍 2
Using
either
or
either.eager
works too as @Scott Christopher mentioned!