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

purrr suggestions #2

Open
gadenbuie opened this issue Sep 13, 2018 · 1 comment
Open

purrr suggestions #2

gadenbuie opened this issue Sep 13, 2018 · 1 comment

Comments

@gadenbuie
Copy link
Member

Hey, so I was taking a stroll through the code in this repo -- looks like a fun project! -- and I saw that you're using purrr! That package definitely revolutionized how I write code!

Anyway, just a quick tip that might be helpful. In lines like these, you might be able to skip the both the call to flatten_int and the anonymous function

kills<-flatten_int(map(recent,function(x) x$kills))
score<-flatten_dbl(map(recent,function(x) x$score))

by rewriting it like

kills <- map_int(recent, "kills")
score <- map_dbl(recent, "score")

If map is given a character string, it pulls out the element of each list item with that name. Adding the _int or _dbl takes care of the flattening down to integer/double.

map_int() does need each element to return a length-1 vector and the type needs to match the suffix or it will complain.

@thomas-keller
Copy link
Contributor

oh hey, a month late but thanks! I have only really scratched the surface of purrr and always feel like I'm not using it efficiently (as seen in this case). Thanks for the tip, that helps a lot actually.

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