Skip to content

Commit 323a57d

Browse files
author
Full Name
committed
Combine x and ... in min, max, range
Only call `vec_c` if dots are nonempty to stay fast in the empty case.
1 parent 920903d commit 323a57d

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

R/type-vctr.R

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,10 @@ vec_cast_or_na <- function(x, to, ...) {
545545

546546
#' @export
547547
min.vctrs_vctr <- function(x, ..., na.rm = FALSE) {
548+
if (dots_n(...) != 0L) {
549+
x <- vec_c(x, ...)
550+
}
551+
548552
if (vec_is_empty(x)) {
549553
return(vec_cast_or_na(Inf, x))
550554
}
@@ -566,6 +570,10 @@ min.vctrs_vctr <- function(x, ..., na.rm = FALSE) {
566570

567571
#' @export
568572
max.vctrs_vctr <- function(x, ..., na.rm = FALSE) {
573+
if (dots_n(...) != 0L) {
574+
x <- vec_c(x, ...)
575+
}
576+
569577
if (vec_is_empty(x)) {
570578
return(vec_cast_or_na(-Inf, x))
571579
}
@@ -587,6 +595,10 @@ max.vctrs_vctr <- function(x, ..., na.rm = FALSE) {
587595

588596
#' @export
589597
range.vctrs_vctr <- function(x, ..., na.rm = FALSE) {
598+
if (dots_n(...) != 0L) {
599+
x <- vec_c(x, ...)
600+
}
601+
590602
if (vec_is_empty(x)) {
591603
return(vec_cast_or_na(c(Inf, -Inf), x))
592604
}

tests/testthat/test-type-vctr.R

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,18 @@ test_that("Summary generics behave identically to base for empty vctrs (#88)", {
632632
)
633633
})
634634

635+
test_that("methods combine `x` and `...` when generic expects them to (#1372)", {
636+
x <- new_vctr(1)
637+
y <- new_vctr(3)
638+
639+
expect_identical(min(x, y), x)
640+
expect_identical(min(y, x), x)
641+
expect_identical(max(x, y), y)
642+
expect_identical(max(y, x), y)
643+
expect_identical(range(x, y), c(x, y))
644+
expect_identical(range(y, x), c(x, y))
645+
})
646+
635647
test_that("generic predicates return logical vectors (#251)", {
636648
x <- new_vctr(c(1, 2))
637649
expect_identical(is.finite(x), c(TRUE, TRUE))

0 commit comments

Comments
 (0)