Skip to content

Commit

Permalink
Optimize and join swap() and swap_if()
Browse files Browse the repository at this point in the history
  • Loading branch information
danielvartan committed May 25, 2021
1 parent ae4f3a9 commit ac55804
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 48 deletions.
2 changes: 1 addition & 1 deletion R/shorter_interval.R
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ shorter_interval <- function(x, y, class = "hms", inverse = FALSE,
x <- flat_posixt(convert(x, "posixct", quiet = TRUE))
y <- flat_posixt(convert(y, "posixct", quiet = TRUE))

list2env(swap_if(x, y, "x > y"), envir = environment())
list2env(swap(x, y, x > y), envir = environment())

x1_y1_interval <- lubridate::interval(x, y)
y1_x2_interval <- lubridate::interval(y, x + lubridate::days())
Expand Down
4 changes: 2 additions & 2 deletions R/utils-checks.R
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ assert_identical <- function(..., type = "value", any.missing = TRUE,
}
}

# Used in swap_decimal() and parse_to_date_time()
# Used in parse_to_date_time()
check_custom_1 <- function(x, any.missing = TRUE, null.ok = FALSE,
name = deparse(substitute(x))) {
checkmate::assert_flag(any.missing)
Expand All @@ -218,5 +218,5 @@ check_custom_1 <- function(x, any.missing = TRUE, null.ok = FALSE,
}
}

# Used in swap_decimal() and parse_to_date_time()
# Used in parse_to_date_time()
assert_custom_1 <- checkmate::makeAssertionFunction(check_custom_1)
28 changes: 8 additions & 20 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -174,28 +174,16 @@ close_round <- function(x, digits = 3) {
TRUE ~ x)
}

swap <- function(x, y) {
a <- x
b <- y
swap <- function(x, y, condition = TRUE) {
assert_identical(x, y, type = "class")
assert_identical(x, y, condition, type = "length")
checkmate::assert_logical(condition)

x <- b
y <- a
first_arg <- x
second_arg <- y

list(x = x, y = y)
}

swap_if <- function(x, y, condition = "x > y") {
choices <- c("x == y", "x < y", "x <= y", "x > y", "x >= y")
checkmate::assert_choice(condition, choices)

condition <- sub("x", "a", condition)
condition <- sub("y", "b", condition)

a <- x
b <- y

x <- dplyr::if_else(eval(parse(text = condition)), b, a)
y <- dplyr::if_else(eval(parse(text = condition)), a, b)
x <- dplyr::if_else(condition, second_arg, first_arg)
y <- dplyr::if_else(condition, first_arg, second_arg)

list(x = x, y = y)
}
Expand Down
30 changes: 5 additions & 25 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -250,32 +250,12 @@ test_that("close_round() | general test", {
})

test_that("swap() | general test", {
x <- 1
y <- 2
expect_equal(swap(x, y), list(x = y, y = x))
expect_equal(swap(5, 1), list(x = 1, y = 5))
expect_equal(swap(1, 5, 1 > 5), list(x = 1, y = 5))
expect_equal(swap(5, 1, 2 > 1), list(x = 1, y = 5))

x <- 1
y <- TRUE
expect_equal(swap(x, y), list(x = TRUE, y = 1))

x <- c(1, 1)
y <- ""
expect_equal(swap(x, y), list(x = "", y = c(1, 1)))
})

test_that("swap_if() | general test", {
x <- 2
y <- 1
condition <- "x > y"
expect_equal(swap_if(x, y, condition = condition), list(x = y, y = x))

x <- 1
y <- 1
condition <- "x > y"
expect_equal(swap_if(x, y, condition = condition), list(x = x, y = y))

# Error test
expect_error(swap_if(1, 1, ""))
# Assert condition error
expect_error(swap(1, 1, 1), "not 'double'")
})

test_that("count_na() | general test", {
Expand Down

0 comments on commit ac55804

Please sign in to comment.