-
Notifications
You must be signed in to change notification settings - Fork 255
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
strictly() #275
Comments
Keeping with the principle of separating concerns, it would be handy if |
It seems https://cran.r-project.org/web/packages/ensurer/vignettes/ensurer.html |
@smbache I was unaware of What I understand of In another sense, however, I think the value of Have I understood the role and function of |
In addition to a
Both |
This is a big problem so I think it's out of scope for purrr. Also see https://github.com/jimhester/types |
@hadley I guess I need to do my homework! Didn't know about these packages, either. Thanks. Both Types for R, and the related package Declarative function argument check, tackle the larger problem. The second one especially is quite close in implementation to what I've suggested, but uses roxygen comments, rather than formulae, to specify checks. Relying on a comments-based mechanism is not as flexible as using formulae, à la lazyeval.
But you are right. The basic problem is big and nontrivial, and I do see the rationale behind leaving |
@hadley You're right, |
A common form of boilerplate code at the top of functions is argument checking: You make some checks on the arguments, signal a condition if any show-stopping checks fail, then move on to the meat of the function if everything is good. The problem with this approach is that it can clutter up the main work of a function with admin; it spoils the "fun" of a function with the inconvenience of a security check.
A function
strictly()
, as a cohort ofsafely()
andpossibly()
, would separate these concerns, functionally. It could have the signaturewhere
.f
is an interpreted function andcond
is a condition object (if notNULL
). The main part is...
, which consists of two-sided formulasp ~ m
that specify the argument checks for.f
:p
is an expression that is a call to a predicate function (the "check")m
is an expression whose evaluation can be coerced to a string (the "failure message" if the check fails)The value of
strictly(.f, ...)
would be a function that applies.f
"strictly."Example:
(While the error messages could be more informative, the basic utility is clear.)
Here's a basic implementation, with a modicum of metaprogramming:
Alternatively, one could implement
strictly()
as a function-composition. However, the above implementation has the advantage of preserving the argument signature of.f
, as well as its source code and environment.Would
strictly()
be a meaningful addition to purrr?The text was updated successfully, but these errors were encountered: