On Thu, May 16, 2013 at 6:06 AM, McGehee, Robert <
Robert.McGehee@geodecapital.com> wrote:

> Duncan, Thank you for the clarification on how delayedAssign works. Should
> R-level interfaces to promise objects ever become available, I expect they
> would at time come in handy.
>
> On the subject of substitute and delayedAssign, I do have a follow-up
> question for the list. I'm trying to convert a named list of expression
> objects into an environment of promise objects. After conversion, each
> expression in the list will be automatically evaluated when the variable
> with the same name is accessed in the environment. Effectively, I'm trying
> to create a hash table of promise objects.
>

Populating a new environment with promises happens to be what "calling a
function" in R does anyway, so an elegant way to accomplish this goal is:

makePromiseEnv <- function(expressions, parent=parent.frame()) {
    f <- function() environment()
    formals(f) <- as.pairlist(expressions)
    environment(f) <- parent
    f()
}

> e <- makePromiseEnv(alist(a = {print("hello"); 4}, b = {print("again");
6}))
> e$a
[1] "hello"
[1] 4
> e$a
[1] 4
> e$b
[1] "again"
[1] 6
> e$b
[1] 6

	[[alternative HTML version deleted]]

