Hey Guys, need some help. I have multiple Either’s and I want to collect the right from all of them ...
h
Hey Guys, need some help. I have multiple Either’s and I want to collect the right from all of them to build an object. Something like this, but I’m not able to make this work:
Copy code
@Test
    fun combineEithers() {
        val name = randomEither(null, "Alfredo Lambda")
        val phone = randomEither(null, 55555555)
        val address = randomEither(null, listOf("1 Main Street", "11130", "NYC"))

        val result = Either.applicative<Profile>()
            .tupled(name, phone, address)
            .fix()
            .map { Profile(it.a, it.b, it.c) }

        println(result)
    }

    fun <K> randomEither(left: ResponseError?, right: K): Either<ResponseError, K> {
        return if (left != null) {
            Either.left(left)
        } else {
            Either.right(right)
        }
    }

    data class Profile(val name: String, val phone: Int, val address: List<String>)
r
What happens if one of the generated Either is left?
Would it fail to generate the object ? and what value do you expect in return?
Applicative.tupled or Applicative.mapN already does what you want but short-circuits in left values preserving the first one found as the choosen one
h
… short-circuits in left values preserving the first one found as the choosen one
@raulraja This is exactly what I would like to accomplish but I can’t make it work.
If you see my previous example it doesn’t compile. Can you help me understand what I’m doing wrong ?