Skip to content

Instantly share code, notes, and snippets.

@halpo
Created February 5, 2012 00:03
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 halpo/1741262 to your computer and use it in GitHub Desktop.
Save halpo/1741262 to your computer and use it in GitHub Desktop.
R Function Composition
#' Copyright Andrew Redd 2012
#' Licensed under GPLv3 or newer
#' Nest functions
#' @return new function cosisting of the functions nested
compose <- function(..., .list){
l <- if(missing(.list)) {
list(...)
} else {
.list
}
body <- as.name('...')
for(i in rev(l)){
body <- as.call(list(i,body))
}
as.function(append(alist(...=), body))
}
#' @rdname compose
`%.%` <- function(x,y){
compose(.list=list(x,y))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment