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

C++ implementations of map, map2, map3 etc? #118

Closed
hadley opened this issue Nov 5, 2015 · 4 comments
Closed

C++ implementations of map, map2, map3 etc? #118

hadley opened this issue Nov 5, 2015 · 4 comments

Comments

@hadley
Copy link
Member

@hadley hadley commented Nov 5, 2015

Advantages:

  • Control exactly what happens
  • Speed? (need to benchmark)
  • Build in output type (logical, integer, double, character, list) at low level
@hadley
Copy link
Member Author

@hadley hadley commented Nov 9, 2015

The implementation of lapply() is https://github.com/wch/r-source/blob/b156e3a711967f58131e23c1b1dc1ea90e2f0c43/src/main/apply.c#L34 - rather than doing the subsetting in C, it creates a new variable called i in the enclosing scope, then constructs and evaluates a call to f(x[[i]], ...).

Very rough start on C++ implementation:

// [[Rcpp::export]]
List map(List x, Function f, Environment e) {
  int n = x.length();
  List out(n);

  SEXP tmp = PROTECT(LCONS(R_Bracket2Symbol,
    LCONS(X, LCONS(isym, R_NilValue))));
  SEXP R_fcall = PROTECT(LCONS(FUN,
    LCONS(tmp, LCONS(R_DotsSymbol, R_NilValue))));

  for (int i = 0; i < n; ++i) {
    e.assign("i", i);


  }

  return out;
}
@lionel-
Copy link
Member

@lionel- lionel- commented Nov 9, 2015

That's also the strategy I used in rows-based functions. See by_slice_impl() and by_row_impl() in https://github.com/hadley/purrr/blob/6d57a81302eaa18579f82b414b6bbbc9e6ca282e/src/rows.cpp#L186. Maybe we can use the same call constructor in all mapping functions.

@hadley
Copy link
Member Author

@hadley hadley commented Nov 10, 2015

@lionel- oh nice - that's going to save me a lot of struggling!

@lionel-
Copy link
Member

@lionel- lionel- commented Nov 10, 2015

that's not guaranteed ;)

hadley added a commit that referenced this issue Nov 11, 2015
@lionel- I think this approach is slightly cleaner/safer than what you did in by_slice_impl() etc. Feedback appreciated!

#118
hadley added a commit that referenced this issue Nov 11, 2015
@hadley hadley closed this in e3fcb93 Nov 12, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants