Skip to content

Instantly share code, notes, and snippets.

@rightfold
Last active August 29, 2015 14:19
Show Gist options
  • Save rightfold/ae787ad2006478b5a934 to your computer and use it in GitHub Desktop.
Save rightfold/ae787ad2006478b5a934 to your computer and use it in GitHub Desktop.
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