Skip to content

Commit 85967f8

Browse files
committed
Rework vec_case_match() on vctrs::vec_case_when()
1 parent 746eb87 commit 85967f8

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

R/case-match.R

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -168,26 +168,32 @@ vec_case_match <- function(
168168
obj_check_list(haystacks, arg = haystacks_arg, call = call)
169169
list_check_all_vectors(haystacks, arg = haystacks_arg, call = call)
170170

171+
if (length(haystacks) == 0L) {
172+
# `case_match()` is like `case_when()` and doesn't allow empty `...`,
173+
# even though `vec_case_when()` is well defined for this case.
174+
abort("At least one condition must be supplied.", call = call)
175+
}
176+
171177
haystacks <- vec_cast_common(
172178
!!!haystacks,
173179
.to = needles,
174180
.arg = haystacks_arg,
175181
.call = call
176182
)
177183

178-
conditions <- map(haystacks, vec_in, needles = needles)
184+
cases <- map(haystacks, vec_in, needles = needles)
179185

180186
size <- vec_size(needles)
181187

182-
vec_case_when(
183-
conditions = conditions,
188+
vctrs::vec_case_when(
189+
cases = cases,
184190
values = values,
185-
conditions_arg = "",
186-
values_arg = values_arg,
187191
default = default,
188-
default_arg = default_arg,
189192
ptype = ptype,
190193
size = size,
191-
call = call
194+
cases_arg = "",
195+
values_arg = values_arg,
196+
default_arg = default_arg,
197+
error_call = call
192198
)
193199
}

tests/testthat/_snaps/case-match.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
case_match(1, 1 ~ 1L, .default = "x")
2929
Condition
3030
Error in `case_match()`:
31-
! Can't combine `..1 (right)` <integer> and `.default` <character>.
31+
! Can't combine <integer> and `.default` <character>.
3232

3333
# `NULL` formula element throws meaningful error
3434

@@ -124,7 +124,7 @@
124124
vec_case_match(1, haystacks = list(1), values = list(1:2))
125125
Condition
126126
Error in `vec_case_match()`:
127-
! `values[[1]]` must have size 1, not size 2.
127+
! Can't recycle `values[[1]]` (size 2) to size 1.
128128

129129
# input must be a vector
130130

0 commit comments

Comments
 (0)