TIL Closures
Though, to me this was YIL since I was reading Advanced R Programming at 2 am in between games of Call of Duty. Specifically, I was reading Hadley's articulation of functional programming in R: http://adv-r.had.co.nz/Functional-programming.html. To say the least, I am greatly impressed by this. I've only gotten my feet wet with regards functional programming, but there are things that R does behind the scenes, you don't really think about. Closures is one of them. The whole "names(x) <- c(...)" is another. You typically don't think about what is going on, because it all makes sense. However, programmatically, this is craziness!!
So what are closures? It's basically enclosing functions (puts OOP on its head--objects enclose data with methods; closures enclose methods with data). Look at Hadley's example
Very simple. It's a function that raises x to the specified exponent, right? Well, not exactly. It took me all morning while I was shopping--I spend a lot of time walking around on the weekends, it gives me time to think--but I finally framed this in functional terms instead of object terms. I couldn't quite understand how you could do something like
To frame it correctly, I thought about "foobar <- mean". This now renames (makes foobar refer to) the mean function. With this I can foobar(...) some vector to get its mean. This is essentially what the power function is doing. It is encapsulating an anonymous function with a specific attribute--viz., the specified exponent value. When you do an assignment like "square <- power(2)" you are assigning square that specific anonymous function (with e=2). Thus, when you do square(5) you are passing 5 to "function (x) x^2". The difficulty I was having was in thinking that somehow the "5 was being passed into power and into the anonymous function, which is just stupid. Instead, the return type of power is that of an anonymous function. So we might properly document such a think thusly,
This is powerful stuff because we can encode similar functional calls into a closure that can generate the variation in functions. Think how you would have to do this manually
As soon as you had to create other varieties, you'd have to start manually coding a lot of details. This would be hard to maintain in the future and would be very inconvenient. The other nice thing is that since these functions can be generated by a function, you can program the automatic generation of these. That's powerful stuff!
Thanks to reading this, I'll be having this functional programming thought at the back of my head whenever I'm writing new functions. It'll be whispering "can you make a closure for this? Go ahead. Do it. You know you want to ..."
Though, to me this was YIL since I was reading Advanced R Programming at 2 am in between games of Call of Duty. Specifically, I was reading Hadley's articulation of functional programming in R: http://adv-r.had.co.nz/Functional-programming.html. To say the least, I am greatly impressed by this. I've only gotten my feet wet with regards functional programming, but there are things that R does behind the scenes, you don't really think about. Closures is one of them. The whole "names(x) <- c(...)" is another. You typically don't think about what is going on, because it all makes sense. However, programmatically, this is craziness!!
So what are closures? It's basically enclosing functions (puts OOP on its head--objects enclose data with methods; closures enclose methods with data). Look at Hadley's example
Code:
power <- function (e) {
function (x) x^e
}
Code:
square <- power(2)
cube <- power(3)
square(5) # 25
cube(2) # 8
Code:
#' Power Function Generator
#'
#' Creates an anonymous function of the specified exponent
#'
#' @param e The power to which x will be raised.
#' @return An anonymous function that raises inputs to the power of e.
power <- function (e) {
function (x) x^e
}
Code:
square <- function(x) x^2
cube <- function(x) x^3
Thanks to reading this, I'll be having this functional programming thought at the back of my head whenever I'm writing new functions. It'll be whispering "can you make a closure for this? Go ahead. Do it. You know you want to ..."