I think that I have learnt and forgotten, and then learnt about this feature of R a few times in the past 4 years. The idea (I think), is this:

  1. R allows you to pass functions as arguments
  2. Functions can be modified inside a function

So what the hell does that mean?

Well, I think I can summarise it down to this crazy piece of magic:

my_fun <- function(x, fun){ fun(x)

Now we can pass in some input, and any function.

Let’s take the …