|
45 | 45 | coalesce <- function(..., .ptype = NULL, .size = NULL) { |
46 | 46 | args <- list2(...) |
47 | 47 |
|
48 | | - if (vec_any_missing(args)) { |
49 | | - # Drop `NULL`s |
50 | | - not_missing <- !vec_detect_missing(args) |
51 | | - args <- vec_slice(args, not_missing) |
52 | | - } |
53 | | - |
54 | 48 | if (length(args) == 0L) { |
55 | 49 | abort("`...` can't be empty.") |
56 | 50 | } |
| 51 | + if (vec_all_missing(args)) { |
| 52 | + abort("`...` must contain at least 1 non-`NULL` value.") |
| 53 | + } |
| 54 | + |
| 55 | + # We do vector, type, and size checks up front before dropping any `NULL` |
| 56 | + # values or extracting out a `default` to ensure that any errors report |
| 57 | + # the correct index |
| 58 | + list_check_all_vectors(args, allow_null = TRUE, arg = "") |
| 59 | + |
| 60 | + .ptype <- vec_ptype_common(!!!args, .ptype = .ptype) |
| 61 | + args <- vec_cast_common(!!!args, .to = .ptype) |
| 62 | + |
| 63 | + if (is_null(.size)) { |
| 64 | + .size <- vec_size_common(!!!args) |
| 65 | + } else { |
| 66 | + # Check recyclability, but delay actual recycling |
| 67 | + list_check_all_recyclable(args, .size, allow_null = TRUE, arg = "") |
| 68 | + } |
57 | 69 |
|
58 | | - # Recycle early so logical conditions computed below will be the same length, |
59 | | - # as required by `vec_case_when()` |
60 | | - args <- vec_recycle_common(!!!args, .size = .size) |
| 70 | + # From this point on we don't expect any errors |
61 | 71 |
|
62 | | - # Name early to get correct indexing in `vec_case_when()` error messages |
63 | | - names <- names2(args) |
64 | | - names <- names_as_error_names(names) |
65 | | - args <- set_names(args, names) |
| 72 | + args <- convert_from_coalesce_to_case_when(args, .size) |
| 73 | + values <- args$values |
| 74 | + default <- args$default |
66 | 75 |
|
67 | | - conditions <- map(args, function(arg) { |
68 | | - !vec_detect_missing(arg) |
| 76 | + cases <- map(values, function(value) { |
| 77 | + !vec_detect_missing(value) |
69 | 78 | }) |
70 | 79 |
|
71 | | - vec_case_when( |
72 | | - conditions = conditions, |
73 | | - values = args, |
74 | | - conditions_arg = "", |
75 | | - values_arg = "", |
| 80 | + vctrs::vec_case_when( |
| 81 | + cases = cases, |
| 82 | + values = values, |
| 83 | + default = default, |
76 | 84 | ptype = .ptype, |
77 | | - size = .size, |
78 | | - call = current_env() |
| 85 | + size = .size |
79 | 86 | ) |
80 | 87 | } |
| 88 | + |
| 89 | +# Goal is to convert from `...` of `coalesce()` to `values` and `default` |
| 90 | +# of `vec_case_when()` |
| 91 | +# |
| 92 | +# Recognize that these are equivalent: |
| 93 | +# |
| 94 | +# ``` |
| 95 | +# coalesce(x, y) |
| 96 | +# case_when(!vec_detect_missing(x) ~ x, !vec_detect_missing(y) ~ y) |
| 97 | +# |
| 98 | +# coalesce(x, y_with_no_missings) |
| 99 | +# case_when(!vec_detect_missing(x) ~ x, .default = y_with_no_missings) |
| 100 | +# |
| 101 | +# coalesce(x, NULL, y, 0) |
| 102 | +# case_when(!vec_detect_missing(x) ~ x, !vec_detect_missing(y) ~ y, .default = 0) |
| 103 | +# ``` |
| 104 | +# |
| 105 | +# Note how the last element can be used as `default` if it doesn't contain any |
| 106 | +# missing values. This is a very nice optimization since `vec_case_when()` |
| 107 | +# doesn't need to recycle that value, and efficiently computes its output |
| 108 | +# locations! |
| 109 | +# |
| 110 | +# Note how `NULL`s are dropped during the conversion. |
| 111 | +convert_from_coalesce_to_case_when <- function(args, size) { |
| 112 | + if (vec_any_missing(args)) { |
| 113 | + # Drop `NULL` |
| 114 | + args <- vec_slice(args, vec_detect_complete(args)) |
| 115 | + } |
| 116 | + |
| 117 | + args_size <- length(args) |
| 118 | + |
| 119 | + if (args_size == 0L) { |
| 120 | + abort("Checked for at least 1 non-`NULL` value earlier", .internal = TRUE) |
| 121 | + } |
| 122 | + |
| 123 | + # Try to promote the `last` element of `args` to `default` |
| 124 | + # |
| 125 | + # For the 99% case of `coalesce(x, 0)`, this: |
| 126 | + # - Avoids recycling `0` to size `size`. |
| 127 | + # - Avoids computing `!vec_detect_missing()` on that recycled `0`. |
| 128 | + # |
| 129 | + # Can only do this if the `last` element doesn't contain missing values |
| 130 | + # due to how names are handled. We don't want to take the name from any `NA` |
| 131 | + # element, which is what would happen if we promoted the whole `y` vector here |
| 132 | + # to `default`. |
| 133 | + # |
| 134 | + # ``` |
| 135 | + # x <- c(a = NA, b = 2) |
| 136 | + # y <- c(c = NA, d = 4) |
| 137 | + # |
| 138 | + # coalesce(x, y) |
| 139 | + # # Want c(NA, b = 2) |
| 140 | + # # Not c(c = NA, b = 2) |
| 141 | + # |
| 142 | + # # Compare to |
| 143 | + # case_when(!vec_detect_missing(x) ~ x, !vec_detect_missing(y) ~ y) |
| 144 | + # case_when(!vec_detect_missing(x) ~ x, .default = y) |
| 145 | + # ``` |
| 146 | + last <- args[[args_size]] |
| 147 | + |
| 148 | + if (vec_any_missing(last)) { |
| 149 | + default <- NULL |
| 150 | + } else { |
| 151 | + default <- last |
| 152 | + args <- args[-args_size] |
| 153 | + } |
| 154 | + |
| 155 | + # Most of the time this recycle is a no-op. Two cases where it isn't: |
| 156 | + # - `coalesce(x, 0, 1)`, where `1` becomes `default` but we still have a |
| 157 | + # scalar `0`. |
| 158 | + # - `coalesce(x, NA)`, where `NA` can't be promoted, so we have a scalar `NA`. |
| 159 | + args <- vec_recycle_common(!!!args, .size = size) |
| 160 | + |
| 161 | + list(values = args, default = default) |
| 162 | +} |
| 163 | + |
| 164 | +vec_all_missing <- function(x) { |
| 165 | + if (!vec_any_missing(x)) { |
| 166 | + return(FALSE) |
| 167 | + } |
| 168 | + sum(vec_detect_missing(x)) == vec_size(x) |
| 169 | +} |
0 commit comments