Skip to content

Instantly share code, notes, and snippets.

@richardhundt
Last active August 29, 2015 13:57
Show Gist options
  • Save richardhundt/9920066 to your computer and use it in GitHub Desktop.
Save richardhundt/9920066 to your computer and use it in GitHub Desktop.
Guard composition
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