Find a function

get_fun(fn, default = identity)

Arguments

fn

A function name, either a character string or an unquoted function name, with or without colons.

default

If fn is NULL, the default function is returned. Defaults to identity().

Value

A function

Examples

get_fun(plu_ral)
#> function (x, vector = NULL, n_fn = NULL, ..., n = NULL, pl = NULL, 
#>     irregulars = c("moderate", "conservative", "liberal", "none"), 
#>     replace_n = TRUE, open = "{", close = "}") 
#> {
#>     if (!length(x)) {
#>         return(character(0))
#>     }
#>     assert_type(x, "character")
#>     assert_length_1(replace_n)
#>     assert_t_or_f(replace_n)
#>     pl <- derive_pl(pl, n, vector)
#>     n <- derive_n(pl, n, vector)
#>     validate_delimeters(open, close)
#>     open <- escape(open)
#>     close <- escape(close)
#>     mat <- matrix(x, nrow = 1)
#>     boundaries <- paste0("((?=[^[:alnum:]'\\-", open, "])", "|", 
#>         "(?<=[^[:alnum:]'\\-", open, "]))", "(?![^", open, "]*", 
#>         close, ")")
#>     split_in <- plu_split(mat, boundaries, perl = TRUE)
#>     braced <- str_detect(split_in, paste0(open, ".*", close))
#>     if (replace_n) {
#>         replace_n <- str_detect(split_in, "\\bn\\b")
#>     }
#>     n_fn <- get_fun(n_fn)
#>     split_out <- split_in
#>     if (pl) {
#>         split_out[!braced & !replace_n] <- plu_ralize(split_in[!braced & 
#>             !replace_n], irregulars = irregulars)
#>         removed <- which(split_out == "" & split_in != "")
#>         removed <- removed[removed%%nrow(split_out) != 0]
#>         if (any(removed)) {
#>             caps <- which(is_capitalized(split_in))
#>             caps <- caps[caps %in% removed]
#>             after_caps <- vapply(caps, function(i) {
#>                 idx <- which(str_detect(split_out[seq(i, (i%/%nrow(split_out) + 
#>                   1) * nrow(split_out))], "[[:alpha:]]"))
#>                 idx[1] + i - 1L
#>             }, integer(1))
#>             split_out[after_caps] <- capitalize(split_out[after_caps])
#>             after_removed <- removed + 1
#>             split_out[after_removed][split_out[after_removed] == 
#>                 " "] <- ""
#>         }
#>     }
#>     split_out[braced] <- pluralize_braces(split_out[braced], 
#>         n, open, close)
#>     split_out[replace_n] <- str_replace_all(split_out[replace_n], 
#>         "\\bn\\b", n_fn(n, ...))
#>     mat[] <- apply(split_out, 2, paste, collapse = "")
#>     x[] <- apply(mat, 2, paste, collapse = "")
#>     x
#> }
#> <bytecode: 0x55b583268fc0>
#> <environment: namespace:plu>
get_fun(plu::ral)
#> function (x, vector = NULL, n_fn = NULL, ..., n = NULL, pl = NULL, 
#>     irregulars = c("moderate", "conservative", "liberal", "none"), 
#>     replace_n = TRUE, open = "{", close = "}") 
#> {
#>     if (!length(x)) {
#>         return(character(0))
#>     }
#>     assert_type(x, "character")
#>     assert_length_1(replace_n)
#>     assert_t_or_f(replace_n)
#>     pl <- derive_pl(pl, n, vector)
#>     n <- derive_n(pl, n, vector)
#>     validate_delimeters(open, close)
#>     open <- escape(open)
#>     close <- escape(close)
#>     mat <- matrix(x, nrow = 1)
#>     boundaries <- paste0("((?=[^[:alnum:]'\\-", open, "])", "|", 
#>         "(?<=[^[:alnum:]'\\-", open, "]))", "(?![^", open, "]*", 
#>         close, ")")
#>     split_in <- plu_split(mat, boundaries, perl = TRUE)
#>     braced <- str_detect(split_in, paste0(open, ".*", close))
#>     if (replace_n) {
#>         replace_n <- str_detect(split_in, "\\bn\\b")
#>     }
#>     n_fn <- get_fun(n_fn)
#>     split_out <- split_in
#>     if (pl) {
#>         split_out[!braced & !replace_n] <- plu_ralize(split_in[!braced & 
#>             !replace_n], irregulars = irregulars)
#>         removed <- which(split_out == "" & split_in != "")
#>         removed <- removed[removed%%nrow(split_out) != 0]
#>         if (any(removed)) {
#>             caps <- which(is_capitalized(split_in))
#>             caps <- caps[caps %in% removed]
#>             after_caps <- vapply(caps, function(i) {
#>                 idx <- which(str_detect(split_out[seq(i, (i%/%nrow(split_out) + 
#>                   1) * nrow(split_out))], "[[:alpha:]]"))
#>                 idx[1] + i - 1L
#>             }, integer(1))
#>             split_out[after_caps] <- capitalize(split_out[after_caps])
#>             after_removed <- removed + 1
#>             split_out[after_removed][split_out[after_removed] == 
#>                 " "] <- ""
#>         }
#>     }
#>     split_out[braced] <- pluralize_braces(split_out[braced], 
#>         n, open, close)
#>     split_out[replace_n] <- str_replace_all(split_out[replace_n], 
#>         "\\bn\\b", n_fn(n, ...))
#>     mat[] <- apply(split_out, 2, paste, collapse = "")
#>     x[] <- apply(mat, 2, paste, collapse = "")
#>     x
#> }
#> <bytecode: 0x55b582fc1598>
#> <environment: namespace:plu>
get_fun("plu_ral")
#> function (x, vector = NULL, n_fn = NULL, ..., n = NULL, pl = NULL, 
#>     irregulars = c("moderate", "conservative", "liberal", "none"), 
#>     replace_n = TRUE, open = "{", close = "}") 
#> {
#>     if (!length(x)) {
#>         return(character(0))
#>     }
#>     assert_type(x, "character")
#>     assert_length_1(replace_n)
#>     assert_t_or_f(replace_n)
#>     pl <- derive_pl(pl, n, vector)
#>     n <- derive_n(pl, n, vector)
#>     validate_delimeters(open, close)
#>     open <- escape(open)
#>     close <- escape(close)
#>     mat <- matrix(x, nrow = 1)
#>     boundaries <- paste0("((?=[^[:alnum:]'\\-", open, "])", "|", 
#>         "(?<=[^[:alnum:]'\\-", open, "]))", "(?![^", open, "]*", 
#>         close, ")")
#>     split_in <- plu_split(mat, boundaries, perl = TRUE)
#>     braced <- str_detect(split_in, paste0(open, ".*", close))
#>     if (replace_n) {
#>         replace_n <- str_detect(split_in, "\\bn\\b")
#>     }
#>     n_fn <- get_fun(n_fn)
#>     split_out <- split_in
#>     if (pl) {
#>         split_out[!braced & !replace_n] <- plu_ralize(split_in[!braced & 
#>             !replace_n], irregulars = irregulars)
#>         removed <- which(split_out == "" & split_in != "")
#>         removed <- removed[removed%%nrow(split_out) != 0]
#>         if (any(removed)) {
#>             caps <- which(is_capitalized(split_in))
#>             caps <- caps[caps %in% removed]
#>             after_caps <- vapply(caps, function(i) {
#>                 idx <- which(str_detect(split_out[seq(i, (i%/%nrow(split_out) + 
#>                   1) * nrow(split_out))], "[[:alpha:]]"))
#>                 idx[1] + i - 1L
#>             }, integer(1))
#>             split_out[after_caps] <- capitalize(split_out[after_caps])
#>             after_removed <- removed + 1
#>             split_out[after_removed][split_out[after_removed] == 
#>                 " "] <- ""
#>         }
#>     }
#>     split_out[braced] <- pluralize_braces(split_out[braced], 
#>         n, open, close)
#>     split_out[replace_n] <- str_replace_all(split_out[replace_n], 
#>         "\\bn\\b", n_fn(n, ...))
#>     mat[] <- apply(split_out, 2, paste, collapse = "")
#>     x[] <- apply(mat, 2, paste, collapse = "")
#>     x
#> }
#> <bytecode: 0x55b583268fc0>
#> <environment: namespace:plu>
get_fun("plu::ral")
#> function (x, vector = NULL, n_fn = NULL, ..., n = NULL, pl = NULL, 
#>     irregulars = c("moderate", "conservative", "liberal", "none"), 
#>     replace_n = TRUE, open = "{", close = "}") 
#> {
#>     if (!length(x)) {
#>         return(character(0))
#>     }
#>     assert_type(x, "character")
#>     assert_length_1(replace_n)
#>     assert_t_or_f(replace_n)
#>     pl <- derive_pl(pl, n, vector)
#>     n <- derive_n(pl, n, vector)
#>     validate_delimeters(open, close)
#>     open <- escape(open)
#>     close <- escape(close)
#>     mat <- matrix(x, nrow = 1)
#>     boundaries <- paste0("((?=[^[:alnum:]'\\-", open, "])", "|", 
#>         "(?<=[^[:alnum:]'\\-", open, "]))", "(?![^", open, "]*", 
#>         close, ")")
#>     split_in <- plu_split(mat, boundaries, perl = TRUE)
#>     braced <- str_detect(split_in, paste0(open, ".*", close))
#>     if (replace_n) {
#>         replace_n <- str_detect(split_in, "\\bn\\b")
#>     }
#>     n_fn <- get_fun(n_fn)
#>     split_out <- split_in
#>     if (pl) {
#>         split_out[!braced & !replace_n] <- plu_ralize(split_in[!braced & 
#>             !replace_n], irregulars = irregulars)
#>         removed <- which(split_out == "" & split_in != "")
#>         removed <- removed[removed%%nrow(split_out) != 0]
#>         if (any(removed)) {
#>             caps <- which(is_capitalized(split_in))
#>             caps <- caps[caps %in% removed]
#>             after_caps <- vapply(caps, function(i) {
#>                 idx <- which(str_detect(split_out[seq(i, (i%/%nrow(split_out) + 
#>                   1) * nrow(split_out))], "[[:alpha:]]"))
#>                 idx[1] + i - 1L
#>             }, integer(1))
#>             split_out[after_caps] <- capitalize(split_out[after_caps])
#>             after_removed <- removed + 1
#>             split_out[after_removed][split_out[after_removed] == 
#>                 " "] <- ""
#>         }
#>     }
#>     split_out[braced] <- pluralize_braces(split_out[braced], 
#>         n, open, close)
#>     split_out[replace_n] <- str_replace_all(split_out[replace_n], 
#>         "\\bn\\b", n_fn(n, ...))
#>     mat[] <- apply(split_out, 2, paste, collapse = "")
#>     x[] <- apply(mat, 2, paste, collapse = "")
#>     x
#> }
#> <bytecode: 0x55b582fc1598>
#> <environment: namespace:plu>

get_fun(NULL)
#> function (x) 
#> x
#> <bytecode: 0x55b581470228>
#> <environment: namespace:base>
get_fun(NULL, default = plu_ral)
#> function (x, vector = NULL, n_fn = NULL, ..., n = NULL, pl = NULL, 
#>     irregulars = c("moderate", "conservative", "liberal", "none"), 
#>     replace_n = TRUE, open = "{", close = "}") 
#> {
#>     if (!length(x)) {
#>         return(character(0))
#>     }
#>     assert_type(x, "character")
#>     assert_length_1(replace_n)
#>     assert_t_or_f(replace_n)
#>     pl <- derive_pl(pl, n, vector)
#>     n <- derive_n(pl, n, vector)
#>     validate_delimeters(open, close)
#>     open <- escape(open)
#>     close <- escape(close)
#>     mat <- matrix(x, nrow = 1)
#>     boundaries <- paste0("((?=[^[:alnum:]'\\-", open, "])", "|", 
#>         "(?<=[^[:alnum:]'\\-", open, "]))", "(?![^", open, "]*", 
#>         close, ")")
#>     split_in <- plu_split(mat, boundaries, perl = TRUE)
#>     braced <- str_detect(split_in, paste0(open, ".*", close))
#>     if (replace_n) {
#>         replace_n <- str_detect(split_in, "\\bn\\b")
#>     }
#>     n_fn <- get_fun(n_fn)
#>     split_out <- split_in
#>     if (pl) {
#>         split_out[!braced & !replace_n] <- plu_ralize(split_in[!braced & 
#>             !replace_n], irregulars = irregulars)
#>         removed <- which(split_out == "" & split_in != "")
#>         removed <- removed[removed%%nrow(split_out) != 0]
#>         if (any(removed)) {
#>             caps <- which(is_capitalized(split_in))
#>             caps <- caps[caps %in% removed]
#>             after_caps <- vapply(caps, function(i) {
#>                 idx <- which(str_detect(split_out[seq(i, (i%/%nrow(split_out) + 
#>                   1) * nrow(split_out))], "[[:alpha:]]"))
#>                 idx[1] + i - 1L
#>             }, integer(1))
#>             split_out[after_caps] <- capitalize(split_out[after_caps])
#>             after_removed <- removed + 1
#>             split_out[after_removed][split_out[after_removed] == 
#>                 " "] <- ""
#>         }
#>     }
#>     split_out[braced] <- pluralize_braces(split_out[braced], 
#>         n, open, close)
#>     split_out[replace_n] <- str_replace_all(split_out[replace_n], 
#>         "\\bn\\b", n_fn(n, ...))
#>     mat[] <- apply(split_out, 2, paste, collapse = "")
#>     x[] <- apply(mat, 2, paste, collapse = "")
#>     x
#> }
#> <bytecode: 0x55b583268fc0>
#> <environment: namespace:plu>