Skip to content

Commit

Permalink
chore: Prefer map_*() over vapply()
Browse files Browse the repository at this point in the history
  • Loading branch information
krlmlr committed Jan 11, 2024
1 parent 392ea47 commit 5d492e5
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 22 deletions.
8 changes: 4 additions & 4 deletions R/expectations.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ expect_all_args_have_default_values <- function(object) {
act <- quasi_label(enquo(object), arg = "object")
act$args <- formals(act$val)
act$args <- act$args[names(act$args) != "..."]
act$char_args <- vapply(act$args, as.character, character(1L))
act$char_args <- map_chr(act$args, as.character)
expect(
all(nzchar(act$char_args, keepNA = FALSE)),
sprintf("%s has arguments without default values", act$lab)
Expand Down Expand Up @@ -47,13 +47,13 @@ expect_invisible_true <- function(code) {
}

expect_equal_df <- function(actual, expected) {
factor_cols <- vapply(expected, is.factor, logical(1L))
factor_cols <- map_lgl(expected, is.factor)
expected[factor_cols] <- lapply(expected[factor_cols], as.character)

asis_cols <- vapply(expected, inherits, "AsIs", FUN.VALUE = logical(1L))
asis_cols <- map_lgl(expected, inherits, "AsIs")
expected[asis_cols] <- lapply(expected[asis_cols], unclass)

list_cols <- vapply(expected, is.list, logical(1L))
list_cols <- map_lgl(expected, is.list)

if (!any(list_cols)) {
order_actual <- do.call(order, actual)
Expand Down
4 changes: 2 additions & 2 deletions R/spec-arrow-append-table-arrow.R
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ spec_arrow_append_table_arrow <- list(
use_append = TRUE,
con, tbl_in,
transform = function(out) {
dates <- vapply(out, inherits, "POSIXt", FUN.VALUE = logical(1L))
dates <- map_lgl(out, inherits, "POSIXt")
tz <- toupper(names(out))
tz[tz == "LOCAL"] <- ""
out[dates] <- Map(lubridate::with_tz, out[dates], tz[dates])
Expand Down Expand Up @@ -468,7 +468,7 @@ spec_arrow_append_table_arrow <- list(
use_append = TRUE,
con, tbl_in,
transform = function(out) {
dates <- vapply(out, inherits, "POSIXt", FUN.VALUE = logical(1L))
dates <- map_lgl(out, inherits, "POSIXt")
tz <- toupper(names(out))
tz[tz == "LOCAL"] <- ""
out[dates] <- Map(lubridate::with_tz, out[dates], tz[dates])
Expand Down
4 changes: 2 additions & 2 deletions R/spec-arrow-write-table-arrow.R
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ spec_arrow_write_table_arrow <- list(
test_arrow_roundtrip(
con, tbl_in,
transform = function(out) {
dates <- vapply(out, inherits, "POSIXt", FUN.VALUE = logical(1L))
dates <- map_lgl(out, inherits, "POSIXt")
tz <- toupper(names(out))
tz[tz == "LOCAL"] <- ""
out[dates] <- Map(lubridate::with_tz, out[dates], tz[dates])
Expand Down Expand Up @@ -701,7 +701,7 @@ spec_arrow_write_table_arrow <- list(
test_arrow_roundtrip(
con, tbl_in,
transform = function(out) {
dates <- vapply(out, inherits, "POSIXt", FUN.VALUE = logical(1L))
dates <- map_lgl(out, inherits, "POSIXt")
tz <- toupper(names(out))
tz[tz == "LOCAL"] <- ""
out[dates] <- Map(lubridate::with_tz, out[dates], tz[dates])
Expand Down
4 changes: 2 additions & 2 deletions R/spec-result-roundtrip.R
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ test_select <- function(con, ..., .dots = NULL, .add_null = "none",
.ctx, .envir = parent.frame()) {
values <- c(list(...), .dots)

value_is_formula <- vapply(values, is.call, logical(1L))
value_is_formula <- map_lgl(values, is.call)
names(values)[value_is_formula] <- lapply(values[value_is_formula], "[[", 2L)
values[value_is_formula] <- lapply(
values[value_is_formula],
Expand Down Expand Up @@ -350,7 +350,7 @@ equals_minus_100 <- function(x) {
}

all_have_utf8_or_ascii_encoding <- function(x) {
all(vapply(x, has_utf8_or_ascii_encoding, logical(1L)))
all(map_lgl(x, has_utf8_or_ascii_encoding)
}

has_utf8_or_ascii_encoding <- function(x) {
Expand Down
4 changes: 2 additions & 2 deletions R/spec-sql-append-table.R
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ spec_sql_append_table <- list(
use_append = TRUE,
con, tbl_in,
transform = function(out) {
dates <- vapply(out, inherits, "POSIXt", FUN.VALUE = logical(1L))
dates <- map_lgl(out, inherits, "POSIXt")
tz <- toupper(names(out))
tz[tz == "LOCAL"] <- ""
out[dates] <- Map(lubridate::with_tz, out[dates], tz[dates])
Expand Down Expand Up @@ -457,7 +457,7 @@ spec_sql_append_table <- list(
use_append = TRUE,
con, tbl_in,
transform = function(out) {
dates <- vapply(out, inherits, "POSIXt", FUN.VALUE = logical(1L))
dates <- map_lgl(out, inherits, "POSIXt")
tz <- toupper(names(out))
tz[tz == "LOCAL"] <- ""
out[dates] <- Map(lubridate::with_tz, out[dates], tz[dates])
Expand Down
8 changes: 4 additions & 4 deletions R/spec-sql-list-objects.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ spec_sql_list_objects <- list(

#' part of the data frame.
objects <- dbListObjects(con)
quoted_tables <- vapply(objects$table, dbQuoteIdentifier, conn = con, character(1))
quoted_tables <- map_chr(objects$table, dbQuoteIdentifier, conn = con)
expect_true(dbQuoteIdentifier(con, table_name) %in% quoted_tables)
},
# second stage
Expand All @@ -55,7 +55,7 @@ spec_sql_list_objects <- list(
#' As soon a table is removed from the database,
#' it is also removed from the data frame of database objects.
objects <- dbListObjects(con)
quoted_tables <- vapply(objects$table, dbQuoteIdentifier, conn = con, character(1))
quoted_tables <- map_chr(objects$table, dbQuoteIdentifier, conn = con)
expect_false(dbQuoteIdentifier(con, table_name) %in% quoted_tables)
},

Expand All @@ -66,7 +66,7 @@ spec_sql_list_objects <- list(
dbWriteTable(con, table_name, data.frame(a = 1L), temporary = TRUE)

objects <- dbListObjects(con)
quoted_tables <- vapply(objects$table, dbQuoteIdentifier, conn = con, character(1))
quoted_tables <- map_chr(objects$table, dbQuoteIdentifier, conn = con)
expect_true(dbQuoteIdentifier(con, table_name) %in% quoted_tables)
}
},
Expand All @@ -84,7 +84,7 @@ spec_sql_list_objects <- list(
local_remove_test_table(con, table_name)
dbWriteTable(con, dbQuoteIdentifier(con, table_name), data.frame(a = 2L))
objects <- dbListObjects(con)
quoted_tables <- vapply(objects$table, dbQuoteIdentifier, conn = con, character(1))
quoted_tables <- map_chr(objects$table, dbQuoteIdentifier, conn = con)
expect_true(dbQuoteIdentifier(con, table_name) %in% quoted_tables)
}
},
Expand Down
6 changes: 3 additions & 3 deletions R/spec-sql-quote-literal.R
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,17 @@ spec_sql_quote_literal <- list(
#' @section Specification:
do_test_literal <- function(x) {
#' The returned expression can be used in a `SELECT ...` query,
literals <- vapply(x, dbQuoteLiteral, conn = con, character(1))
literals <- map_chr(x, dbQuoteLiteral, conn = con)
query <- paste0("SELECT ", paste(literals, collapse = ", "))
#' and the value of
#' \code{dbGetQuery(paste0("SELECT ", dbQuoteLiteral(x)))[[1]]}
#' must be equal to `x`
x_out <- check_df(dbGetQuery(con, query))
expect_equal(nrow(x_out), 1L)

is_logical <- vapply(x, is.logical, FUN.VALUE = logical(1))
is_logical <- map_lgl(x, is.logical)
x_out[is_logical] <- lapply(x_out[is_logical], as.logical)
is_numeric <- vapply(x, is.numeric, FUN.VALUE = logical(1))
is_numeric <- map_lgl(x, is.numeric)
x_out[is_numeric] <- lapply(x_out[is_numeric], as.numeric)
expect_equal(as.list(unname(x_out)), x)
}
Expand Down
4 changes: 2 additions & 2 deletions R/spec-sql-write-table.R
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ spec_sql_write_table <- list(
test_table_roundtrip(
con, tbl_in,
transform = function(out) {
dates <- vapply(out, inherits, "POSIXt", FUN.VALUE = logical(1L))
dates <- map_lgl(out, inherits, "POSIXt")
tz <- toupper(names(out))
tz[tz == "LOCAL"] <- ""
out[dates] <- Map(lubridate::with_tz, out[dates], tz[dates])
Expand Down Expand Up @@ -663,7 +663,7 @@ spec_sql_write_table <- list(
test_table_roundtrip(
con, tbl_in,
transform = function(out) {
dates <- vapply(out, inherits, "POSIXt", FUN.VALUE = logical(1L))
dates <- map_lgl(out, inherits, "POSIXt")
tz <- toupper(names(out))
tz[tz == "LOCAL"] <- ""
out[dates] <- Map(lubridate::with_tz, out[dates], tz[dates])
Expand Down
2 changes: 1 addition & 1 deletion R/spec-stress-connection.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ spec_stress_connection <- list(
}

inherit_from_connection <-
vapply(cons, is, class2 = "DBIConnection", logical(1))
map_lgl(cons, is, class2 = "DBIConnection")
expect_true(all(inherit_from_connection))
},

Expand Down

0 comments on commit 5d492e5

Please sign in to comment.