Created
January 26, 2016 19:09
-
-
Save yloiseau/a15d16e632af775c99cb 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 checks | |
function checkWithFallback = |log, message, predicate, fallback| -> |value| { | |
case { | |
when predicate(value) { | |
return value | |
} | |
otherwise { | |
log: add(message) | |
log: add("use %s instead": format(fallback)) | |
return fallback | |
} | |
} | |
} | |
function composeMany = |f, funs...| -> |args...| { | |
require(funs: length() == args: length(), "can't apply composition") | |
let ap = list[] | |
for (var i = 0, i < funs: length(), i = i + 1) { | |
ap: add(funs: get(i)(args: get(i))) | |
} | |
return f: invoke(ap: toArray()) | |
} | |
function ensureNumber = |err| -> |x| -> checkWithFallback(err, "null or NaN", | |
|x| -> x isnt null and x oftype java.lang.Number.class, 0)(x) | |
function ensureGreater = |err| -> |ref, val| -> [ | |
ref, | |
checkWithFallback(err, "not greater than", |x| -> x >= ref, ref)(val) | |
] | |
function validate = |err, minVal, maxVal| -> | |
composeMany(ensureGreater(err), | |
ensureNumber(err), ensureNumber(err))(minVal, maxVal) | |
function temperature = -> DynamicObject() | |
function temperature = |minVal, maxVal| { | |
let err = list[] | |
let min, max = validate(err, minVal, maxVal) | |
if (not err: isEmpty()) { | |
println("temperature: some failures:") | |
println(err) | |
} | |
return temperature(): minTemperature(min): maxTemperature(max) | |
} | |
function main = |args| { | |
temperature(50,null) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment