Skip to content

Instantly share code, notes, and snippets.

@clarenced
Created October 31, 2020 14:21
Show Gist options
  • Save clarenced/f9f50cb08e665477eef1f1cc70c96a2f to your computer and use it in GitHub Desktop.
Save clarenced/f9f50cb08e665477eef1f1cc70c96a2f to your computer and use it in GitHub Desktop.
Either example with Arrow
class DivisionByZero {
override fun toString(): String {
return "Division by zero"
}
}
fun divide(a: Int, b: Int): Either<DivisionByZero, Int> {
return try {
Right(a / b)
} catch (exception: Exception) {
Left(DivisionByZero())
}
}
fun main() {
divide(10, 10).fold(
{ println(it)},
{println("Value ${it}") } // happy path
)
divide(10, 0).fold(
{ println(it)}, // fail case
{println("Value ${it}") }
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment