r - Custom Iterator with more than one return value -
my custom iterator bit slow. hope speed when use unlist(as.list(ic, n=2000)) construct. however, not know how implement functionality. found nextelem , hasnext methods. iterator looks this:
library(itertools) fibonacci <- function(count = na) { ab = c(0, 1) n <- function() { if (!is.na(count)) { if (count > 0) count <<- count -1 else stop('stopiteration') } # ab <<- c(ab[2], sum(ab)) ab[1] } obj <- list(nextelem = n) class(obj) <- c('fibonacci', 'abstractiter', 'iter') obj } i can use this:
ic <- fibonacci () print (nextelem (ic)) now next 10 fibonacci numbers @ once, via
print(unlist(as.list(ic, n=10))) but of course needs implemented. how this?
the fibonacci iterator serves example. actually, work on iterator gives k-combinations of n-set, i.e. memory-friendly version of combn.
Comments
Post a Comment