egnha / valaddin

Functional input validation to make R functions more readable and robust
Other
33 stars 1 forks source link

Simplify the semantics of addings checks via magrittr pipe #20

Closed egnha closed 7 years ago

egnha commented 7 years ago

Cf. #19

To keep checks and function header in close proximity, you can use magrittr's %>% operator:

f <- list(
  ~is.numeric,
  vld_true(~length(x) == length(y)
) %>%
  firmly(
    .f = function(x, y) x + y,
    .checklist = .
  )

By permuting the argument signature of firmly from

function(.f, ..., .checklist = list(), .warn_missing = character())

to

function(.checklist = list(), .f, ..., .warn_missing = character())

it'd be possible to slightly simplify the above to

f <- list(
  ~is.numeric,
  vld_true(~length(x) == length(y)
) %>%
  firmly(function(x, y) x + y)

eliminating the need to explicitly specify .f = and .checklist = ..

This is a straightforward fix (albeit somewhat tedious to implement, since many tests will need to be edited to accommodate the new call signature).

egnha commented 7 years ago

Changing the argument signature as proposed would be a mistake, as it'd make firmly more awkward to use; for example, firmly(f, ~is.numeric) would not longer work, as you'd need to write instead firmly(.f = f, .checklist = list(), ~is.numeric) to avoid matching .checklist to either .f or ....