Last active
April 27, 2018 06:20
-
-
Save egnha/52add617de38bb5b5bcd8751ff179140 to your computer and use it in GitHub Desktop.
Declarative post conditions
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
`%?%` <- function(value, predicate) { | |
list(value = value, predicate = predicate(value)) | |
} | |
# Implicitly, `assert` is either a boolean or a value-predicate pair | |
`%because%` <- function(assert, reason) { | |
if (is.logical(assert)) { | |
if (!assert) stop(reason, call. = FALSE) | |
return(invisible(TRUE)) | |
} | |
if (!assert$predicate) stop(reason, call. = FALSE) | |
invisible(assert$value) | |
} | |
# Simple assertion (returns TRUE, invisibly) | |
is.function(identity) %because% "`identity` should be a function" | |
# Verify and assign | |
f <- identity %?% is.function %because% "`f` must be a function" | |
stopifnot(identical(f, identity)) | |
# Error: `f` must be a function | |
f <- NULL %?% is.function %because% "`f` must be a function" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment