Created
February 5, 2012 00:03
-
-
Save halpo/1741262 to your computer and use it in GitHub Desktop.
R Function Composition
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' 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