Skip to content

Instantly share code, notes, and snippets.

@yloiseau
Last active January 15, 2016 07:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yloiseau/e183fbe70e97bf624309 to your computer and use it in GitHub Desktop.
Save yloiseau/e183fbe70e97bf624309 to your computer and use it in GitHub Desktop.
module Test
# in reply to https://github.com/TypeUnsafe/golo-x/blob/master/main.functional.golo#L8
import gololang.Errors
@result
function divide = |a,b| -> a / b
@result
function toInt = |sParam| -> intValue(sParam: trim())
# monadic chaining :)
function eval = |a, b| ->
toInt(a): andThen(|x| ->
toInt(b): andThen(|y| ->
divide(x, y)))
function main = |args| {
println(eval("84", "a"))
# Result.error[java.lang.NumberFormatException: For input string: "a"]
println(eval("84", "0"))
# Result.error[java.lang.ArithmeticException: / by zero]
println(eval("84", "2"))
# Result.value[42]
println(eval(" foo ", " 2 "): orElse(1))
# 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment