TampaUseRs / EducationOutreach

This is the repository for the Education Outreach material
MIT License
0 stars 0 forks source link

purrr suggestions #2

Open gadenbuie opened 6 years ago

gadenbuie commented 6 years ago

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

https://github.com/TampaUseRs/EducationOutreach/blob/836ab9ff6334744363a4c2f629841da2c0541a7b/ParseUserData.R#L17-L18

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 commented 6 years ago

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.