Closed renkun-ken closed 10 years ago
An alternative design is to override $.Pipe
:
Pipe <- function(value) {
envir <- environment()
class(envir) <- c("Pipe","environment")
envir
}
`$.Pipe` <- function(x,y) {
value <- get("value",envir = x,inherits = FALSE)
if(exists(y,envir = x,inherits = FALSE)) {
get(y,envir = x,inherits = FALSE)
} else {
fun <- match.fun(y)
function(...) {
Pipe(fun(value,...))
}
}
}
print.Pipe <- function(x,...) {
cat("Pipe\n")
print(x$value,...)
}
which allows
z <- Pipe(1:3)$
lapply(function(i) i+1)$
as.numeric()$
mean()
> z
Pipe
[1] 3
This allows the following code:
And benchmark test shows that