# Creates awaitable functions that transform their inputs into a stream
generate_stream <- async_generator(function(x) for (elt in x) yield(elt))
# Maps a function to a stream
async_map <- async_generator(function(.i, .fn, ...) {
for (elt in await_each(.i)) {
yield(.fn(elt, ...))
}
})
# Example usage:
if (interactive()) {
library(magrittr)
generate_stream(1:3) %>% async_map(`*`, 2) %>% async_collect()
}
Run the code above in your browser using DataLab