Marius Kotsbak
03/05/2021, 9:53 AMsimon.vergauwen
03/05/2021, 10:32 AMparTraverseEither
in 0.12.0
suspend fun either(): Either<Int, Long> = TODO()
suspend fun program(): Either<Int, Long> = either {
parMapN(
{ either().bind() },
{ either().bind() },
{ either().bind() })
{ a, b, c -> a + b + c }
}
suspend fun either2(i: Int): Either<Int, Long> = TODO()
suspend fun program2(): Either<Int, List<Long>> =
listOf(1, 2, 3).parTraverseEither { i -> either2(i) }
Marius Kotsbak
03/05/2021, 10:34 AMsimon.vergauwen
03/05/2021, 10:35 AMbind()
from concurrent contextssimon.vergauwen
03/05/2021, 10:36 AMparMapN
takes care of calling the functions in parallel, and you can use bind()
safely from those parallel running functions.simon.vergauwen
03/05/2021, 10:37 AMLeft
and the bind
short-circuits it first cancels the other tasks before returning Left
from the either
blocksimon.vergauwen
03/05/2021, 10:37 AMsuspend
simon.vergauwen
03/05/2021, 10:38 AMIO
effect with Either
effect which effectively makes it a EitherT
and IO
stack without any of the monad transformers costsMarius Kotsbak
03/05/2021, 10:41 AMsimon.vergauwen
03/05/2021, 10:48 AMsimon.vergauwen
03/05/2021, 10:50 AMeither { }
and bind()
in the first example you could do this but it is semantically not the same.
suspend fun either(): Either<Int, Long> = TODO()
suspend fun program(): Either<Int, Long> =
parMapN(
{ either() },
{ either() },
{ either() })
{ a: Either<Int, Long>, b, c ->
a.flatMap { aa -> b.flatMap { bb -> c.map { cc -> aa + bb + cc } } }
}
simon.vergauwen
03/05/2021, 10:51 AMLeft
was encountered.
Whilst if you call bind()
inside the parMapN
functions it will short-circuit the parallel operations as well.Marius Kotsbak
03/05/2021, 1:22 PMsimon.vergauwen
03/05/2021, 1:26 PMeither { }
is totally different from for comprehension.simon.vergauwen
03/05/2021, 1:26 PMflatMap
afaikMarius Kotsbak
03/05/2021, 1:35 PMeither().bind()
(not tested yet)? I would think the value would be Long and not Kind<F, A>
simon.vergauwen
03/05/2021, 1:41 PMLong
from either().bind()
but there is multiple different examples now here in the thread so not sure which one you talking about 😅Marius Kotsbak
03/05/2021, 3:10 PMsimon.vergauwen
03/05/2021, 4:04 PMsimon.vergauwen
03/05/2021, 4:04 PMsuspend fun either(): Either<Int, Long> = TODO()
suspend fun program(): Either<Int, Long> = either {
parMapN(
{ either().bind() },
{ either().bind() },
{ either().bind() })
{ a, b, c -> a + b + c }
}
There was a bind()
at the end of parMapN
but that's not necessary since parMapN
just returns Long
here. Which is lifted by the either { }
block back into Either<Int, Long>
simon.vergauwen
03/05/2021, 4:05 PMMarius Kotsbak
03/05/2021, 5:38 PM