Ville Peurala
09/15/2021, 11:17 AMApplicative
in the latest release? Or what should I use instead?simon.vergauwen
09/15/2021, 11:45 AMApplicative
is not longer present in the latest version. This is because Kotlin doesn’t support higher-kinded types, typeclasses (coherent injection), etc are not present in Kotlin which made these functional constructs very user unfriendly to use.
Depending on your use-case there are serveral different options. If you’re looking for applicative builders, all data types expose an arity-9 zip
function to combine independent valuesVille Peurala
09/15/2021, 11:54 AMval eitherA: Either<String, NonEmptyList<A>> = TODO("Obtained from some API.")
val eitherB: Either<String, NonEmptyList<B>> = TODO("Same as above.")
return Either.applicative<String>().tupledN(
eitherA,
eitherB
).fix().map {
it.a.zip(it.b) { a, b ->
TODO("Put the contents together pair by pair")
}
}
Ville Peurala
09/15/2021, 11:57 AMEither
values in my case, so the last part puts them together triple by triple.Vincent Péricart
09/15/2021, 1:14 PMfun <A, B, C> zipEithers(eitherA: Either<String, NonEmptyList<A>>,
eitherB: Either<String, NonEmptyList<B>>,
zipper: (A, B) -> C): Either<String, NonEmptyList<C>> {
return eitherA.zip(eitherB) { aa, bb -> aa.zip(bb, zipper) }
}
The first zips eithers, the second zips the lists. Hope this helps?raulraja
09/15/2021, 1:33 PMn
arity-9 zip
with most arrow data types including Either. It may look something like
import arrow.core.zip
fun main() {
val ea: Either<String, Int> = Right(1)
val eb: Either<String, String> = Right("ok")
val ec: Either<String, Double> = Right(0.0)
val combined: Either<String, Triple<Int, String, Double>> =
ea.zip(eb, ec, ::Triple)
}
simon.vergauwen
09/15/2021, 1:41 PMval eitherA: Either<String, NonEmptyList<A>> = TODO("Obtained from some API.")
val eitherB: Either<String, NonEmptyList<B>> = TODO("Same as above.")
eitherA.zip(eitherB) { a, b -> a to b }
So rewriting your snippet it becomes the above.
And zip
is overloaded to take 8
elements, so you can combine 9
Either
without having to resort to nesting.Ville Peurala
09/15/2021, 6:03 PMVille Peurala
09/15/2021, 6:04 PMEither.zip
a lot better.