Skip to content

Commit 746eb87

Browse files
committed
Isolate case_match() and vec_case_match()
1 parent ea319ec commit 746eb87

File tree

6 files changed

+533
-527
lines changed

6 files changed

+533
-527
lines changed

R/case-match.R

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,46 @@ case_match <- function(.x, ..., .default = NULL, .ptype = NULL) {
148148
call = current_env()
149149
)
150150
}
151+
152+
vec_case_match <- function(
153+
needles,
154+
haystacks,
155+
values,
156+
...,
157+
needles_arg = "needles",
158+
haystacks_arg = "haystacks",
159+
values_arg = "values",
160+
default = NULL,
161+
default_arg = "default",
162+
ptype = NULL,
163+
call = current_env()
164+
) {
165+
check_dots_empty0(...)
166+
167+
obj_check_vector(needles, arg = needles_arg, call = call)
168+
obj_check_list(haystacks, arg = haystacks_arg, call = call)
169+
list_check_all_vectors(haystacks, arg = haystacks_arg, call = call)
170+
171+
haystacks <- vec_cast_common(
172+
!!!haystacks,
173+
.to = needles,
174+
.arg = haystacks_arg,
175+
.call = call
176+
)
177+
178+
conditions <- map(haystacks, vec_in, needles = needles)
179+
180+
size <- vec_size(needles)
181+
182+
vec_case_when(
183+
conditions = conditions,
184+
values = values,
185+
conditions_arg = "",
186+
values_arg = values_arg,
187+
default = default,
188+
default_arg = default_arg,
189+
ptype = ptype,
190+
size = size,
191+
call = call
192+
)
193+
}

R/vec-case-match.R

Lines changed: 0 additions & 44 deletions
This file was deleted.

tests/testthat/_snaps/case-match.md

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@
1414
Error in `case_match()`:
1515
! At least one condition must be supplied.
1616

17+
---
18+
19+
Code
20+
vec_case_match(1, haystacks = list(), values = list())
21+
Condition
22+
Error in `vec_case_match()`:
23+
! At least one condition must be supplied.
24+
1725
# `.default` is part of common type computation
1826

1927
Code
@@ -58,3 +66,177 @@
5866
Caused by error:
5967
! oh no!
6068

69+
# `haystacks` must be castable to `needles`
70+
71+
Code
72+
vec_case_match(1L, haystacks = list(1.5), values = list(2))
73+
Condition
74+
Error in `vec_case_match()`:
75+
! Can't convert from `haystacks[[1]]` <double> to <integer> due to loss of precision.
76+
* Locations: 1
77+
78+
# `ptype` overrides `values` common type
79+
80+
Code
81+
vec_case_match(1:2, haystacks = list(1), values = list(1.5), ptype = integer())
82+
Condition
83+
Error in `vec_case_match()`:
84+
! Can't convert from `values[[1]]` <double> to <integer> due to loss of precision.
85+
* Locations: 1
86+
87+
# `default` respects `ptype`
88+
89+
Code
90+
vec_case_match(needles = 1, haystacks = list(1), values = list(2L), default = 1.5,
91+
ptype = integer())
92+
Condition
93+
Error in `vec_case_match()`:
94+
! Can't convert from `default` <double> to <integer> due to loss of precision.
95+
* Locations: 1
96+
97+
# `NULL` values in `haystacks` and `values` are not dropped
98+
99+
Code
100+
vec_case_match(1:2, list(1, NULL, 2), list("a", NULL, "b"))
101+
Condition
102+
Error in `vec_case_match()`:
103+
! `haystacks[[2]]` must be a vector, not `NULL`.
104+
105+
---
106+
107+
Code
108+
vec_case_match(1:2, list(1, NULL, 2), list("a", "a", "b"))
109+
Condition
110+
Error in `vec_case_match()`:
111+
! `haystacks[[2]]` must be a vector, not `NULL`.
112+
113+
---
114+
115+
Code
116+
vec_case_match(1:2, list(1, 1, 2), list("a", NULL, "b"))
117+
Condition
118+
Error in `vec_case_match()`:
119+
! `values[[2]]` must be a vector, not `NULL`.
120+
121+
# size of `needles` is maintained
122+
123+
Code
124+
vec_case_match(1, haystacks = list(1), values = list(1:2))
125+
Condition
126+
Error in `vec_case_match()`:
127+
! `values[[1]]` must have size 1, not size 2.
128+
129+
# input must be a vector
130+
131+
Code
132+
vec_case_match(environment(), haystacks = list(environment()), values = list(1))
133+
Condition
134+
Error in `vec_case_match()`:
135+
! `needles` must be a vector, not an environment.
136+
137+
# `haystacks` must be a list
138+
139+
Code
140+
vec_case_match(1, haystacks = 1, values = list(2))
141+
Condition
142+
Error in `vec_case_match()`:
143+
! `haystacks` must be a list, not the number 1.
144+
145+
# `values` must be a list
146+
147+
Code
148+
vec_case_match(1, haystacks = list(1), values = 2)
149+
Condition
150+
Error in `vec_case_match()`:
151+
! `values` must be a list, not the number 2.
152+
153+
# `needles_arg` is respected
154+
155+
Code
156+
vec_case_match(needles = environment(), haystacks = list(environment()),
157+
values = list(1), needles_arg = "foo")
158+
Condition
159+
Error in `vec_case_match()`:
160+
! `foo` must be a vector, not an environment.
161+
162+
---
163+
164+
Code
165+
vec_case_match(needles = environment(), haystacks = list(environment()),
166+
values = list(1), needles_arg = "")
167+
Condition
168+
Error in `vec_case_match()`:
169+
! Input must be a vector, not an environment.
170+
171+
# `haystacks_arg` is respected
172+
173+
Code
174+
vec_case_match(needles = 1, haystacks = 1, values = list(1), haystacks_arg = "foo")
175+
Condition
176+
Error in `vec_case_match()`:
177+
! `foo` must be a list, not the number 1.
178+
179+
---
180+
181+
Code
182+
vec_case_match(needles = 1, haystacks = 1, values = list(1), haystacks_arg = "")
183+
Condition
184+
Error in `vec_case_match()`:
185+
! Input must be a list, not the number 1.
186+
187+
---
188+
189+
Code
190+
vec_case_match(needles = 1, haystacks = list(a = "x"), values = list(1),
191+
haystacks_arg = "foo")
192+
Condition
193+
Error in `vec_case_match()`:
194+
! Can't convert `foo$a` <character> to <double>.
195+
196+
---
197+
198+
Code
199+
vec_case_match(needles = 1, haystacks = list("x"), values = list(1),
200+
haystacks_arg = "")
201+
Condition
202+
Error in `vec_case_match()`:
203+
! Can't convert `..1` <character> to <double>.
204+
205+
# `values_arg` is respected
206+
207+
Code
208+
vec_case_match(needles = 1, haystacks = list(1, 2), values = list("x", b = 1),
209+
values_arg = "foo")
210+
Condition
211+
Error in `vec_case_match()`:
212+
! Can't combine `foo[[1]]` <character> and `foo$b` <double>.
213+
214+
---
215+
216+
Code
217+
vec_case_match(needles = 1, haystacks = list(1, 2), values = list("x", b = 1),
218+
values_arg = "")
219+
Condition
220+
Error in `vec_case_match()`:
221+
! Can't combine `..1` <character> and `b` <double>.
222+
223+
# `default_arg` is respected
224+
225+
Code
226+
vec_case_match(needles = 1, haystacks = list(1), values = list(2L), default = 1.5,
227+
default_arg = "foo", ptype = integer())
228+
Condition
229+
Error in `vec_case_match()`:
230+
! Can't convert from `foo` <double> to <integer> due to loss of precision.
231+
* Locations: 1
232+
233+
---
234+
235+
Code
236+
vec_case_match(needles = 1, haystacks = list(1), values = list(2L), default = 1.5,
237+
default_arg = "", ptype = integer())
238+
Condition
239+
Error in `vec_case_match()`:
240+
! Can't convert from <double> to <integer> due to loss of precision.
241+
* Locations: 1
242+

0 commit comments

Comments
 (0)