Skip to content

Instantly share code, notes, and snippets.

@njtierney
Created October 6, 2020 01:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save njtierney/92fbf044fe5b8a7bd10432fe473e7083 to your computer and use it in GitHub Desktop.
Save njtierney/92fbf044fe5b8a7bd10432fe473e7083 to your computer and use it in GitHub Desktop.
library(purrr)
cases <- 250
halving_thing <- function(cases, n_times){
  purrr::accumulate(.x = rep(cases, n_times), .f = ~.x * 0.5)
}

halving_thing(532, 3)
#> [1] 532 266 133
halving_thing(227, 3)
#> [1] 227.00 113.50  56.75

date_today <- lubridate::today() 
date_today + lubridate::period(num = 1, units = "week")
#> [1] "2020-10-13"

purrr::accumulate(.x = rep(date_today, 3), 
                  .f = 
                    ~.x + lubridate::period(num = 1, 
                                          units = "week")) %>% 
  purrr::map(lubridate::as_date) %>% 
  purrr::map(c)
#> [[1]]
#> [1] "2020-10-06"
#> 
#> [[2]]
#> [1] "2020-10-13"
#> 
#> [[3]]
#> [1] "2020-10-20"

Created on 2020-10-06 by the reprex package (v0.3.0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment