Hi everyone! :wave: :smile: I’ve just started to u...
# arrow
r
Hi everyone! 👋 😄 I’ve just started to use Arrow and I’ve come across this situation where I’m not totally sure what I’m supposed to do or what is best practice. I have this function in a file service:
fun getSignedUrl(fileName: String): Either<FileError, URL>
in which I
Either.catch
a generation of a signed URL and
mapLeft
for a potential
FileError.SignedUrlError
. In another service I have a function that looks like this:
fun getReports(): Either<GeneralError, List<Report>>
. In this I’m mapping through each report and replacing filenames with a call to
getSignedUrl
. I want to do it eagerly and go left on first fail. In the end I’m folding into a HttpResponse like this:
fun <BODY> Either<GeneralError, BODY>.toResponse(): HttpResponse<BODY>
As you can see I have two different Error objects and nested Either’s. The only solution I’ve found without changing
FileError.SignedUrlError
into
GeneralError
is something like this:
Copy code
fun <OUTER_TYPE, INNER_TYPE, ERROR_TYPE> Collection<OUTER_TYPE>.mapEitherReturningFirstLeft(block: (item: OUTER_TYPE) -> Either<ERROR_TYPE, INNER_TYPE>): Either<ERROR_TYPE, List<INNER_TYPE>> =
    this.map { item ->
        block(item).fold(
            ifRight = { it },
            ifLeft = { return it.left() }
        )
    }.right()
This doesn’t seem optimal. Any suggestions or thoughts will be much appreciated! 🤩
s
That opereation is `traverseEither`/`traverse`.
r
Thank you! Exactly what I was looking for 😄