Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement {map,map2,pmap}_{lgl,int,dbl,chr,raw}_matrix() #801

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions R/map.R
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,41 @@ map_raw <- function(.x, .f, ...) {
.Call(map_impl, environment(), ".x", ".f", "raw")
}

#' @rdname map
#' @export
map_lgl_matrix <- function(.x, .f, .n, ..., .by_row = FALSE) {
.f <- as_mapper(.f, ...)
.Call(map_matrix_impl, environment(), ".x", ".f", "logical", .n, .by_row)
}

#' @rdname map
#' @export
map_chr_matrix <- function(.x, .f, .n, ..., .by_row = FALSE) {
.f <- as_mapper(.f, ...)
.Call(map_matrix_impl, environment(), ".x", ".f", "character", .n, .by_row)
}

#' @rdname map
#' @export
map_int_matrix <- function(.x, .f, .n, ..., .by_row = FALSE) {
.f <- as_mapper(.f, ...)
.Call(map_matrix_impl, environment(), ".x", ".f", "integer", .n, .by_row)
}

#' @rdname map
#' @export
map_dbl_matrix <- function(.x, .f, .n, ..., .by_row = FALSE) {
.f <- as_mapper(.f, ...)
.Call(map_matrix_impl, environment(), ".x", ".f", "double", .n, .by_row)
}

#' @rdname map
#' @export
map_raw_matrix <- function(.x, .f, .n, ..., .by_row = FALSE) {
.f <- as_mapper(.f, ...)
.Call(map_matrix_impl, environment(), ".x", ".f", "raw", .n, .by_row)
}

#' @rdname map
#' @param .id Either a string or `NULL`. If a string, the output will contain
#' a variable with that name, storing either the name (if `.x` is named) or
Expand Down
87 changes: 87 additions & 0 deletions R/map2-pmap.R
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,38 @@ map2_raw <- function(.x, .y, .f, ...) {
.f <- as_mapper(.f, ...)
.Call(map2_impl, environment(), ".x", ".y", ".f", "raw")
}

#' @export
#' @rdname map2
map2_lgl_matrix <- function(.x, .y, .f, .n, ..., .by_row = FALSE) {
.f <- as_mapper(.f, ...)
.Call(map2_matrix_impl, environment(), ".x", ".y", ".f", "logical", .n, .by_row)
}
#' @export
#' @rdname map2
map2_int_matrix <- function(.x, .y, .f, .n, ..., .by_row = FALSE) {
.f <- as_mapper(.f, ...)
.Call(map2_matrix_impl, environment(), ".x", ".y", ".f", "integer", .n, .by_row)
}
#' @export
#' @rdname map2
map2_dbl_matrix <- function(.x, .y, .f, .n, ..., .by_row = FALSE) {
.f <- as_mapper(.f, ...)
.Call(map2_matrix_impl, environment(), ".x", ".y", ".f", "double", .n, .by_row)
}
#' @export
#' @rdname map2
map2_chr_matrix <- function(.x, .y, .f, .n, ..., .by_row = FALSE) {
.f <- as_mapper(.f, ...)
.Call(map2_matrix_impl, environment(), ".x", ".y", ".f", "character", .n, .by_row)
}
#' @export
#' @rdname map2
map2_raw_matrix <- function(.x, .y, .f, .n, ..., .by_row = FALSE) {
.f <- as_mapper(.f, ...)
.Call(map2_matrix_impl, environment(), ".x", ".y", ".f", "raw", .n, .by_row)
}

#' @rdname map2
#' @export
map2_dfr <- function(.x, .y, .f, ..., .id = NULL) {
Expand Down Expand Up @@ -228,6 +260,61 @@ pmap_raw <- function(.l, .f, ...) {
.Call(pmap_impl, environment(), ".l", ".f", "raw")
}

#' @export
#' @rdname map2
pmap_lgl_matrix <- function(.l, .f, .n, ..., .by_row = FALSE) {
.f <- as_mapper(.f, ...)
if (is.data.frame(.l)) {
.l <- as.list(.l)
}

.Call(pmap_matrix_impl, environment(), ".l", ".f", "logical", .n, .by_row)
}

#' @export
#' @rdname map2
pmap_int_matrix <- function(.l, .f, .n, ..., .by_row = FALSE) {
.f <- as_mapper(.f, ...)
if (is.data.frame(.l)) {
.l <- as.list(.l)
}

.Call(pmap_matrix_impl, environment(), ".l", ".f", "integer", .n, .by_row)
}

#' @export
#' @rdname map2
pmap_dbl_matrix <- function(.l, .f, .n, ..., .by_row = FALSE) {
.f <- as_mapper(.f, ...)
if (is.data.frame(.l)) {
.l <- as.list(.l)
}

.Call(pmap_matrix_impl, environment(), ".l", ".f", "double", .n, .by_row)
}

#' @export
#' @rdname map2
pmap_chr_matrix <- function(.l, .f, .n, ..., .by_row = FALSE) {
.f <- as_mapper(.f, ...)
if (is.data.frame(.l)) {
.l <- as.list(.l)
}

.Call(pmap_matrix_impl, environment(), ".l", ".f", "character", .n, .by_row)
}

#' @export
#' @rdname map2
pmap_raw_matrix <- function(.l, .f, .n, ..., .by_row = FALSE) {
.f <- as_mapper(.f, ...)
if (is.data.frame(.l)) {
.l <- as.list(.l)
}

.Call(pmap_matrix_impl, environment(), ".l", ".f", "raw", .n, .by_row)
}

#' @rdname map2
#' @export
pmap_dfr <- function(.l, .f, ..., .id = NULL) {
Expand Down
1 change: 0 additions & 1 deletion src/coerce.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ void set_vector_value(SEXP to, int i, SEXP from, int j) {
}
}


SEXP coerce_impl(SEXP x, SEXP type_) {
int n = Rf_length(x);

Expand Down
6 changes: 6 additions & 0 deletions src/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ extern SEXP coerce_impl(SEXP, SEXP);
extern SEXP pluck_impl(SEXP, SEXP, SEXP, SEXP);
extern SEXP flatten_impl(SEXP);
extern SEXP map_impl(SEXP, SEXP, SEXP, SEXP);
extern SEXP map_matrix_impl(SEXP, SEXP, SEXP, SEXP, SEXP, SEXP);
extern SEXP map2_impl(SEXP, SEXP, SEXP, SEXP, SEXP);
extern SEXP map2_matrix_impl(SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP);
extern SEXP pmap_impl(SEXP, SEXP, SEXP, SEXP);
extern SEXP pmap_matrix_impl(SEXP, SEXP, SEXP, SEXP, SEXP, SEXP);
extern SEXP transpose_impl(SEXP, SEXP);
extern SEXP vflatten_impl(SEXP, SEXP);

Expand All @@ -26,8 +29,11 @@ static const R_CallMethodDef CallEntries[] = {
{"pluck_impl", (DL_FUNC) &pluck_impl, 4},
{"flatten_impl", (DL_FUNC) &flatten_impl, 1},
{"map_impl", (DL_FUNC) &map_impl, 4},
{"map_matrix_impl", (DL_FUNC) &map_matrix_impl, 6},
{"map2_impl", (DL_FUNC) &map2_impl, 5},
{"map2_matrix_impl", (DL_FUNC) &map2_matrix_impl, 7},
{"pmap_impl", (DL_FUNC) &pmap_impl, 4},
{"pmap_matrix_impl", (DL_FUNC) &pmap_matrix_impl, 6},
{"transpose_impl", (DL_FUNC) &transpose_impl, 2},
{"vflatten_impl", (DL_FUNC) &vflatten_impl, 2},
{"purrr_eval", (DL_FUNC) &Rf_eval, 2},
Expand Down
Loading