knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "man/figures/README-", out.width = "100%" )
The package implements list comprehensions as purely syntactic sugar with a minor runtime overhead. It constructs nested for-loops and executes the byte-compiled loops to collect the results.
remotes::install_github("dirkschumacher/listcomp")
install.packages("listcomp")
This is a basic example which shows you how to solve a common problem:
library(listcomp) head(gen_list(c(x, y), x = 1:100, y = 1:100, z = 1:100, x < 5, y < 5, z == x + y))
gen_list(c(x, y), x = 1:10, y = x:5, x < 2)
This is how the code looks like:
lst_verbose <- function(expr, ...) { deparse(listcomp:::translate(rlang::enquo(expr), rlang::enquos(...))) } lst_verbose(c(x, y), x = 1:10, y = x:5, x < 2)
You can also burn in external variables
z <- 10 gen_list(c(x, y), x = 1:!!z, y = x:5, x < 2)
It also supports parallel iteration by passing a list of named sequences
gen_list(c(i, j, k), list(i = 1:10, j = 1:10), k = 1:5, i < 3, k < 3)
The code then looks like this:
lst_verbose(c(i, j, k), list(i = 1:10, j = 1:10), k = 1:5, i < 3, k < 3)
It is quite fast, but the order of filter conditions also greatly determines the execution time. Sometimes, ahead of time compiling is slower than running it right away.
bench::mark( a = gen_list(c(x, y), x = 1:100, y = 1:100, z = 1:100, x < 5, y < 5, z == x + y), b = gen_list(c(x, y), x = 1:100, x < 5, y = 1:100, y < 5, z = 1:100, z == x + y), c = gen_list(c(x, y), x = 1:100, y = 1:100, z = 1:100, x < 5, y < 5, z == x + y, .compile = FALSE), d = gen_list(c(x, y), x = 1:100, x < 5, y = 1:100, y < 5, z = 1:100, z == x + y, .compile = FALSE) )
How slow is it compared to a for loop and lapply for a very simple example?
bench::mark( a = gen_list(x * 2, x = 1:1000, x**2 < 100), b = gen_list(x * 2, x = 1:1000, x**2 < 100, .compile = FALSE), c = lapply(Filter(function(x) x**2 < 100, 1:1000), function(x) x * 2), d = { res <- list() for (x in 1:1000) { if (x**2 >= 100) next res[[length(res) + 1]] <- x * 2 } res }, time_unit = "ms" )
listcomp
listcomp
but with a different syntax.listcomp
and offers special generator functions for lists, vectors, data.frames
and matrices.Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.