-
Notifications
You must be signed in to change notification settings - Fork 12
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
Method dispatch for rx_string
#11
Comments
In ‘Code smells’ Jenny Bryan argued that whenever you have Perhaps we need |
This is exactly what the package needs! What is the use case for |
Exactly the point. It does nothing. It is needed to prevent sanitization. For |
Here's S3 method for sanitize <- function(.data = NULL){
UseMethod("sanitize", .data)
}
sanitize.rx_string <- function(.data = NULL) {
.data
}
# your function, unchanged, although we will need to rethink the error message now
sanitize.default <- function(.data = NULL) {
if(missing(.data))
stop("The 'value' argument is missing. Did you forget to start the rx chain with rx()", .call = FALSE)
escape_chrs <- c(".", "|", "*", "?", "+", "(", ")", "{", "}", "^", "$", "\\", ":", "=", "[", "]")
string_chrs <- strsplit(.data, "")[[1]]
idx <- which(string_chrs %in% escape_chrs)
idx_new <- paste0("\\", string_chrs[idx])
paste0(replace(string_chrs, idx, idx_new), collapse = "")
}
rx_literal("?@") %>% sanitize()
#> [1] "\\?@"
#> attr(,"class")
#> [1] "rx_string" "character"
"?@" %>% sanitize()
#> [1] "\\?@" I simplified the funcitons above assuming method dispatch for |
Nice, I recently changed sanitize to: sanitize <- function(.data = NULL) {
if(missing(.data))
stop("The 'value' argument is missing. Did you forget to start the rx chain with rx()?")
esc <- c(".", "|", "*", "?", "+", "(", ")", "{", "}", "^", "$", "\\", ":", "=", "[", "]")
gsub(paste0("([\\", paste0(collapse = "\\", esc), "])"), "\\\\\\1", .data, perl = TRUE)
} The Regarding the error message, maybe something like "Nothing to sanitize, please provide a character vector or rx call." |
I am fine with |
Haha, yes it does look scary. I mainly just assumed its bullet proof given who wrote the package. Let's stick with |
To do here:
|
Still relevant, because we really need to go away from the need to use |
I've been trying to figure out how to avoid rx() %>%
rx_either_of(
rx_find(., "foo"),
rx_find(., "bar")
) |
One lazy solution I've came up with is: `~` <- function(...) {
args <- as.list(sys.call())[-1]
args <- paste0("rx() %>% ", args)
args <- sapply(args, function(x) eval(parse(text = x)), USE.NAMES = FALSE)
paste0(args, collapse = "")
}
vals <- `~`
# but keep in mind
fortunes::fortune("answer is parse") This allows something like: # with tilde
rx() %>%
rx_group(
~ rx_either_of("cat", "dog")
) %>%
rx_literal(" food") %>%
stringr::str_extract(c("cat food", "dog food", "fish food"), .)
#> [1] "cat food" "dog food" NA
# with vals
rx() %>%
rx_group(
vals(rx_either_of("cat", "dog"))
) %>%
rx_literal(" food") %>%
stringr::str_extract(c("cat food", "dog food", "fish food"), .)
#> [1] "cat food" "dog food" NA |
Basically boils down to detecting that
.data
is not ofrx_string
class and acting as though first argument is thevalue
argument.Reference: http://adv-r.had.co.nz/S3.html
Now you dont need a constructor. Function works both in chain and stand alone
Hadley says we should also implement a few essential methods. We should rethink all of our functions with vectorization in mind.
The text was updated successfully, but these errors were encountered: