Hi, and sorry for the probably dumb question, but ...
# arrow
v
Hi, and sorry for the probably dumb question, but where is
Applicative
in the latest release? Or what should I use instead?
s
Hey @Ville Peurala,
Applicative
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 values
v
I have a couple of `Either`s, with different generic data types. I would like to put their right sides together, if they are all `Either.Right`s. Something like this:
Copy code
val 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")
            }
        }
Actually there are three
Either
values in my case, so the last part puts them together triple by triple.
v
I think you need to nest `zip`s. e.g.
Copy code
fun <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?
👍 1
r
@Ville Peurala you may get away without nesting as @simon.vergauwen mentioned above, you can use
n
arity-9
zip
with most arrow data types including Either. It may look something like
Copy code
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)
}
👍 1
1
☝️ 1
s
Copy code
val 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.
1
👍 1
v
Thanks a lot @simon.vergauwen, @raulraja, @Vincent Péricart!
🙂 1
👍 2
Now I understand the functionality of
Either.zip
a lot better.