What is the current suggestion to replace tupled ?
# arrow
c
What is the current suggestion to replace tupled ?
p
zip(::to)
s
c
I’ve been trying to use zip, but what I’m missing is the reified return behavior
For example I’m trying to accomplish something like this.
Copy code
suspend 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() }
)
where each repository returns an either, if any of them fail, the left path is taken
a
I believe you should be able to combine them using something like:
Copy code
suspend 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 do
☝️ 1
s
Copy code
suspend 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 times
🙃 1
Ah too slow 😂 Exactly as @Alex Johnson mentions. We should add this to the documentation somewhere for sure. If someone wants to contribute I'd be happy to help with whatever you need 😉
c
Thanks so much you guys, I really appreciate the help 🙂
Would you accept a PR that adds a method with no return that can be used like parZip, or is there something that accomplishes this already ? for example
Copy code
either {
  parNoReturn(
    { effectfulFoo().bind() },
    { effectfulBar().bind() }
  )
}
It would be nice to somehow delegate the bind externally, I’m not sure how you might do that though. My imagined syntax would look something like
Copy code
parEffectfulValidatedNel(::effectfulFoo, ::effectfulBar).bind()
where the passed functions can return Either / ValidatedNel / Validated and then the bound result would be something like ValidatedNel<CustomError, Unit>
Sorry, I realize this is somewhat incoherent haha
r
@Cody Mikol I’m not sure if no return would help us but this behavior could be encapsulated into something like
parZipEither
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.