Cody Mikol
04/16/2021, 1:10 PMpakoito
04/16/2021, 1:14 PMstojan
04/16/2021, 1:55 PMCody Mikol
04/16/2021, 2:26 PMCody Mikol
04/16/2021, 2:31 PMsuspend fun magic() {
val (foo, bar, baz) = getDependencies().bind()
return foo.count + bar.count + baz.count
}
suspend fun getDependencies() = parZip(
{ fooRepository.get() },
{ barRepository.get() },
{ bazRepository.get() }
)
Cody Mikol
04/16/2021, 2:32 PMAlex Johnson
04/16/2021, 3:25 PMsuspend fun dependencies(): Either<Throwable, Pair<String, String>> = either {
parZip(
{ a().bind() },
{ b().bind() }
) { a, b -> Pair(a, b) }
}
suspend fun a(): Either<Throwable, String> {
delay(500)
println("a")
return Either.Right("a")
}
suspend fun b(): Either<Throwable, String> {
println("b")
return Either.Right("b")
}
there might be a different syntax available but that's generally what I dosimon.vergauwen
04/16/2021, 3:25 PMsuspend fun magic() = either {
val (foo, bar, baz) = getDependencies().bind()
return foo.count + bar.count + baz.count
}
suspend fun getDependencies() = either {
parZip(
{ fooRepository.get().bind() },
{ barRepository.get().bind() },
{ bazRepository.get().bind() }
) { foo, bar, baz -> Triple(foo, bar baz) }
}
I think this is what you're looking for. You can simply bind()
inside parZip
while it preserves the semantics of either { }
and safely deals with the cancellation and concurrency within parZip
. So if fooRepository.get()
returns immediately with Left
than barRepository.get()
and bazRepository.get()
will get cannceled.
We deprecated/removed the aliases for ::Triple
etc since it's such a silly overload to repeat so many timessimon.vergauwen
04/16/2021, 3:27 PMCody Mikol
04/16/2021, 6:48 PMCody Mikol
04/16/2021, 7:18 PMeither {
parNoReturn(
{ effectfulFoo().bind() },
{ effectfulBar().bind() }
)
}
Cody Mikol
04/16/2021, 7:29 PMparEffectfulValidatedNel(::effectfulFoo, ::effectfulBar).bind()
where the passed functions can return Either / ValidatedNel / Validated and then the bound result would be something like ValidatedNel<CustomError, Unit>Cody Mikol
04/16/2021, 7:40 PMraulraja
04/18/2021, 9:09 AMparZipEither
where the binds happen in the impl. The issue with zip
and some of these methods is that it requires N methods for all the arities you want to support. I think people use suspend () -> Either<E, A>
frequent enough to justify it.