ramhiser/itertools2

Helper functions for consuming iterators

Opened this issue · 1 comments

Writing unlist(as.list(it)) gets kind of old. Plus, it can get weird when it returns something more than a single numeric or character. With this in mind, helper functions are a must. Something along the lines of:

  • to_list
  • to_vector
  • to_dataframe

In my own code, I've been using as_vector and as_numeric as:

as_vector <- function(x) {
  unlist(as.list(x))
}

set.seed(42)
it <- irep(function(x) rnorm(1), times=10)
as_vector(it)
# [1]  1.37095845 -0.56469817  0.36312841  0.63286260  0.40426832 -0.10612452  1.51152200 -0.09465904
# [9]  2.01842371 -0.06271410
as_numeric <- function(x) {
  as.numeric(as.list(x))
}
set.seed(42)
it <- irep(function(x) rnorm(1), times=10)
as_numeric(it)
# [1]  1.37095845 -0.56469817  0.36312841  0.63286260  0.40426832 -0.10612452  1.51152200 -0.09465904
# [9]  2.01842371 -0.06271410