Skip to content

Instantly share code, notes, and snippets.

@aballano
Created May 4, 2020 16:59
Show Gist options
  • Save aballano/c36d8bca4ba8ad6ae9bd5b432c306c35 to your computer and use it in GitHub Desktop.
Save aballano/c36d8bca4ba8ad6ae9bd5b432c306c35 to your computer and use it in GitHub Desktop.
Temporary workaround function to flatten an IO of Either
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