Skip to content

Commit

Permalink
Recycle names in map2()
Browse files Browse the repository at this point in the history
Closes #779
  • Loading branch information
lionel- committed Aug 5, 2020
1 parent 74f6dd8 commit 7abe7af
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/map.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,20 @@
#include "utils.h"

void copy_names(SEXP from, SEXP to) {
if (Rf_length(from) != Rf_length(to))
return;

SEXP names = Rf_getAttrib(from, R_NamesSymbol);
if (Rf_isNull(names))
if (names == R_NilValue) {
return;
}

R_len_t n = Rf_length(to);

if (Rf_length(names) != n) {
names = short_vec_recycle(names, n);
}
PROTECT(names);

Rf_setAttrib(to, R_NamesSymbol, names);
UNPROTECT(1);
}

void check_vector(SEXP x, const char *name) {
Expand Down
7 changes: 7 additions & 0 deletions tests/testthat/test-map2.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,10 @@ test_that("map2() with empty input copies names", {
expect_identical(map2_chr(named_list, list(), identity), named(chr()))
expect_identical(map2_raw(named_list, list(), identity), named(raw()))
})

test_that("map2() recycle names (#779)", {
expect_identical(
map2(c(a = 1), 1:2, ~ .x),
list(a = 1, a = 1)
)
})

0 comments on commit 7abe7af

Please sign in to comment.