Skip to content

Basic map implementation #495

@hadley

Description

@hadley

Need to benchmark the two strategies: in the first we just put in a list and then vec_c() at the end; in the second we assume that the type is mostly stable so we can create the space for output in the beginning, and only occasionally change.

vec_map <- function(.x, .f, ..., .ptype = NULL) {
  .x <- lapply(.x, .f, ...)
  vec_c(!!!.x, .ptype = .ptype)
}


vec_map <- function(.x, .f, ..., .ptype = NULL) {
  first <- .x[[1]]
  first_out <- .f(.x, ...) 
  
  out <- vec_na(first_out, vec_size(.x)) 
  vec_slice(out, 1) <- first_out

  for (i in seq2(2, vec_size(.x))) {
    outi <- .f(vec_slice(x, i), ...)
    if (vec_is(outi, out)) {
      # change type
    }
    vec_slice(out, i) <- outi
  }
  
  out
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    featurea feature request or enhancement

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions