Skip to content

Instantly share code, notes, and snippets.

@Torvaney
Created January 21, 2021 11:51
Show Gist options
  • Save Torvaney/e500fefb2c676a7f8a03911456a5070a to your computer and use it in GitHub Desktop.
Save Torvaney/e500fefb2c676a7f8a03911456a5070a to your computer and use it in GitHub Desktop.
# From: https://twitter.com/dirk_sch/status/1352194126654148618
library(purrr)
library(testthat)
fn <- function(xs) {
if (!is_list(xs) | length(xs) < 2)
return(xs)
f_xs <- map(xs, fn)
exec(f_xs[[1]], !!!f_xs[2:length(f_xs)])
}
expect_equal(fn(list(`+`, 1, list(`*`, 2, 2))), 5)
expect_equal(fn(list()), list())
expect_equal(fn(42), 42)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment