Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A map/zip function with implicit binding #1126

Closed
twhitehead opened this issue May 6, 2024 · 1 comment
Closed

A map/zip function with implicit binding #1126

twhitehead opened this issue May 6, 2024 · 1 comment

Comments

@twhitehead
Copy link

Was writing this sort of code

map(x,\(x) f(...,x,...))

and got thinking it would be really nice to just be able to write it as

map(f(...,x,...))

where the fact that I used x in f(x) implies that f(x) is to be applied element wise to x.

In essence, I was imagining a more implicit function, call it zip, in keeping with standard functional naming, that would automatically vectorize the passed expression. That is, it would take all the free variables in the expression passed to it, look them up in it's environment, expand them all to equal length vectors, and map across them all.

In terms of the wider tidyverse, you could replace this sort how-many-times-do-I-have-to-type-xyz code

data |> mutate(r = pmap_vec(list(x,y,z), \(x,y,z) f(...,x,y,z,...)))

or this less verbose but more sinfully-stateful code

data |> rowwise() |> mutate(r = f(...,x,y,z,...)) |> ungroup()

with just

data |> mutate(r = zip(f(..,x,y,z,...)))

Hopefully that makes sense. Have no idea if this is even practical, but figured I would suggest it. Maybe it it would make sense for the mutate .by option to also accept 'row' too to avoid stateful grouping/ungrouping.

Feel free to just close this if you want.

@hadley
Copy link
Member

hadley commented Jul 15, 2024

I think this is a cool idea, but it just feels a bit too magical for purrr, which has generally been moving away from "magic" and towards explicitness.

@hadley hadley closed this as completed Jul 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants