Last active
January 15, 2016 07:22
-
-
Save yloiseau/e183fbe70e97bf624309 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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