Last active
August 29, 2015 13:57
-
-
Save richardhundt/9920066 to your computer and use it in GitHub Desktop.
Guard composition
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
function conjoin(a, b) | |
meta = { } | |
repr = nil | |
function meta::__tostring() | |
if not repr then | |
repr = "%{a} * %{b}" | |
end | |
return repr | |
end | |
function meta.__is(v) | |
return v is a and v is b | |
end | |
return meta as meta | |
end | |
function disjoin(a, b) | |
meta = { } | |
repr = nil | |
function meta::__tostring() | |
if not repr then | |
repr = "%{a} + %{b}" | |
end | |
return repr | |
end | |
function meta.__is(v) | |
return v is a or v is b | |
end | |
end | |
function ge(n) | |
meta = { } | |
repr = nil | |
function meta::__tostring() | |
if not repr then | |
repr = "ge %{n}" | |
end | |
return repr | |
end | |
function meta::__mul(a, b) | |
return conjoin(a, b) | |
end | |
function meta::__add(a, b) | |
return disjoin(a, b) | |
end | |
function meta.__is(b) | |
return b >= n | |
end | |
return meta as meta | |
end | |
function lt(n) | |
meta = { } | |
repr = nil | |
function meta::__tostring() | |
if not repr then | |
repr = "lt %{n}" | |
end | |
return repr | |
end | |
function meta::__mul(a, b) | |
return conjoin(a, b) | |
end | |
function meta::__add(a, b) | |
return disjoin(a, b) | |
end | |
function meta.__is(b) | |
return b < n | |
end | |
return meta as meta | |
end | |
x is Number * ge(3) * lt(100) = 5 | |
x = 2 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment