Created
May 4, 2020 16:59
-
-
Save aballano/c36d8bca4ba8ad6ae9bd5b432c306c35 to your computer and use it in GitHub Desktop.
Temporary workaround function to flatten an IO of Either
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class IOException(val error: Any?) : Exception() | |
/** | |
* Flattens this IO's Either into IO itself, wrapping the untyped-Left side on a custom exception. | |
* | |
* Note: This is a workaround until IO<E, A> is available | |
*/ | |
fun <A, B> IO<Either<A, B>>.flattenEither(): IO<B> = | |
this.flatMap { | |
it.fold(ifLeft = { | |
IO.raiseError<B>(IOException(it)) | |
}, ifRight = { | |
IO.just(it) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment