Is there an equivalent to `also` in arrow for thi...
# arrow
c
Is there an equivalent to
also
in arrow for things like logging? Currently doing something like
Copy code
foo()
.mapLeft { it.also { logger.error("Something terrible has happened!") } }
.map { it.also { logger.success("Something good has happened!") } }
c
That sounds cool, I see it was removed though
m
No, only effectM? You need the latest version though I think.
c
Those docs you linked are v0.10, I couldn’t find anything like that in the v0.13.2 docs
But
.tap
.tapLeft
.flatTap
sound like they are exactly what I’m looking for
🙌 2
m
Yeah, we had them as local utility functions here. Could remove them when we upgrade.
c
Came up with this
Copy code
package com.openanywhere.oautils.arrow

import arrow.core.Either

inline fun <L, R> Either<L, R>.tap(fn: () -> Unit) = this.map { it.also { fn() } }

inline fun <L, R> Either<L, R>.tapLeft(fn: () -> Unit) = this.mapLeft { it.also { fn() } }
not sure if flatTap makes sense as flatMap expects an either return type given the right path
Could do something cool like
Copy code
inline fun <L, R> Either<L, R>.tapFold(lFn: () -> Unit, rFn: () -> Unit) = this
    .tap { rFn() }
    .tapLeft { lFn() }
m
If you find something new you could try submitting a PR.
c
foo().tapFold(:logErr,:logSuccess)
If this is something that the arrow team is interested in, I’d make a PR
m
I don't know, but it's possible, if there is something new that is missing.
141 Views