Comments (2)
This would be particularly nice with some helpers that provide similar semantics to purrr::imap()
or to enumerate()
in python.
library(zeallot)
library(purrr)
library(glue)
pfor <- function(loop_construct, expr) {
loop_construct <- substitute(loop_construct)
expr <- substitute(expr)
loop_list <- eval(loop_construct[[3]], parent.frame())
for(i in seq_along(loop_list)) {
loop_construct[[3]] <- loop_list[[i]]
eval(loop_construct, envir = parent.frame())
eval(expr, envir = parent.frame())
}
}
enumerate <- function(l) {
if(is.null(names(l)))
idx <- seq_along(l)
else
idx <- names(l)
transpose(list(idx, l))
}
l <- list(x = 1:3, y = 4:6, z = 7:9)
pfor(c(name, val) %<-% enumerate(l), {
cat(glue("name: {name} val: {collapse(val, sep = ', ')}"), "\n")
})
#> name: x val: 1, 2, 3
#> name: y val: 4, 5, 6
#> name: z val: 7, 8, 9
names(l) <- NULL
pfor(c(name, val) %<-% enumerate(l), {
cat(glue("name: {name} val: {collapse(val, sep = ', ')}"), "\n")
})
#> name: 1 val: 1, 2, 3
#> name: 2 val: 4, 5, 6
#> name: 3 val: 7, 8, 9
Created on 2018-02-22 by the reprex package (v0.2.0).
from zeallot.
This would be awesome. For the time being I would urge users to work with purrr::map2
or purrr::pmap
. Because in an ideal world zeallot would not exist. Unpacking and destructuring assignment would be native to R. purrr
's toolkit is well suited for the cases described above and the resulting code, in my opinion, would be cleaner. For now I will shelve this idea, but I am right there with you wishing this were a reality.
from zeallot.
Related Issues (20)
- Multiple assignment of ggplot2 objects HOT 3
- Formally support and test R 3.1 and greater? HOT 1
- Assign multiple types? HOT 1
- Can we get %<->%? HOT 1
- %<-% and %>% HOT 3
- !!! tidy eval in LHS of %<-% ? HOT 1
- improving traceback of rhs errors
- Destructuring Assignment by Name HOT 10
- A named unpacking function sans %->%? HOT 5
- version support R 3.1 never sent to CRAN
- Set := to %<-% HOT 3
- Support for partial assignment HOT 3
- Better tracebacks with rlang
- Could an second variation on the operator be created so it doesn't with %<-% from future HOT 6
- Improve step-in functionality during debugging
- ...x can be empty
- no visible binding for global variable for CRAN check HOT 2
- Move `master` branch to `main` HOT 1
- Conflict with future package HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from zeallot.