-
-
Save rightfold/ae787ad2006478b5a934 to your computer and use it in GitHub Desktop.
This file contains 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
contractStack <- array! | |
withContract <- { |impl| | |
{ |this., args...| | |
contractStack.push (object "require" array! "ensure" array! "body" null) | |
try { | |
impl.apply this args... | |
(last contractStack).require.forEach { |f| f! } | |
out <- (last contractStack).body! | |
(last contractStack).ensure.forEach { |f| f out } | |
out | |
} finally { | |
contractStack.pop! | |
} | |
} | |
} | |
require <- { |impl| (last contractStack).require.push impl } | |
ensure <- { |impl| (last contractStack).ensure.push impl } | |
body <- { |impl| (last contractStack).body := impl } | |
abs <- withContract { |n| | |
require { instanceof n number } | |
ensure { |out| >= out 0 } | |
body { | |
if (< n 0) { - n } { n } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment