https://github.com/dirkschumacher/listcomp
List comprehensions in R
https://github.com/dirkschumacher/listcomp
comprehensions list-comprehensions listcomprehensions r
Last synced: about 1 month ago
JSON representation
List comprehensions in R
- Host: GitHub
- URL: https://github.com/dirkschumacher/listcomp
- Owner: dirkschumacher
- License: other
- Created: 2020-04-25T01:10:27.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2022-02-01T08:35:11.000Z (over 3 years ago)
- Last Synced: 2025-05-12T02:07:33.842Z (about 1 month ago)
- Topics: comprehensions, list-comprehensions, listcomprehensions, r
- Language: R
- Homepage:
- Size: 97.7 KB
- Stars: 19
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.Rmd
- License: LICENSE
Awesome Lists containing this project
README
---
output: github_document
---```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# List comprehensions[](https://github.com/dirkschumacher/listcomp/actions)
[](https://CRAN.R-project.org/package=listcomp)
[](https://github.com/dirkschumacher/listcomp/actions)The package implements [list comprehensions](https://en.wikipedia.org/wiki/List_comprehension) as purely syntactic sugar with a minor runtime overhead. It constructs nested for-loops and executes the byte-compiled loops to collect the results.
## Installation
``` r
remotes::install_github("dirkschumacher/listcomp")
`````` r
install.packages("listcomp")
```## Example
This is a basic example which shows you how to solve a common problem:
```{r example}
library(listcomp)
head(gen_list(c(x, y), x = 1:100, y = 1:100, z = 1:100, x < 5, y < 5, z == x + y))
``````{r}
gen_list(c(x, y), x = 1:10, y = x:5, x < 2)
```This is how the code looks like:
```{r}
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
```{r}
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
```{r}
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:
```{r}
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.```{r}
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?
```{r}
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"
)
```# Related packages
* [lc](https://github.com/mailund/lc) Uses a similar syntax as `listcomp`
* [comprehenr](https://github.com/gdemin/comprehenr) Uses a similar code generation approach as `listcomp` but with a different syntax.
* [listcompr](https://github.com/patrickroocks/listcompr) Uses a similar syntax as `listcomp` and offers special generator functions for lists, vectors, data.frames
and matrices.