The R Project SVN R

Rev

Rev 68948 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 68948 Rev 83041
Line 1... Line 1...
1
% File src/library/base/man/delayedAssign.Rd
1
% File src/library/base/man/delayedAssign.Rd
2
% Part of the R package, https://www.R-project.org
2
% Part of the R package, https://www.R-project.org
3
% Copyright 1995-2015 R Core Team
3
% Copyright 1995-2022 R Core Team
4
% Distributed under GPL 2 or later
4
% Distributed under GPL 2 or later
5
 
5
 
6
\name{delayedAssign}
6
\name{delayedAssign}
-
 
7
\title{Delay Evaluation and Promises}
7
\alias{delayedAssign}
8
\alias{delayedAssign}
8
\alias{promise}
9
\alias{promise}
9
\alias{promises}
10
\alias{promises}
10
\title{Delay Evaluation}
-
 
11
\description{
11
\description{
12
  \code{delayedAssign} creates a \emph{promise} to evaluate the given
12
  \code{delayedAssign} creates a \emph{promise} to evaluate the given
13
  expression if its value is requested.  This provides direct access
13
  expression if its value is requested.  This provides direct access
14
  to the \emph{lazy evaluation} mechanism used by \R for the evaluation
14
  to the \emph{lazy evaluation} mechanism used by \R for the evaluation
15
  of (interpreted) functions.
15
  of (interpreted) functions.
Line 37... Line 37...
37
  the variable is first accessed.
37
  the variable is first accessed.
38
 
38
 
39
  When the promise is eventually forced, it is evaluated within the
39
  When the promise is eventually forced, it is evaluated within the
40
  environment specified by \code{eval.env} (whose contents may have changed in
40
  environment specified by \code{eval.env} (whose contents may have changed in
41
  the meantime).  After that, the value is fixed and the expression will
41
  the meantime).  After that, the value is fixed and the expression will
42
  not be evaluated again.
42
  not be evaluated again, where the promise still keeps its expression.
43
}
43
}
44
\seealso{
44
\seealso{
45
  \code{\link{substitute}}, to see the expression associated with a
45
  \code{\link{substitute}}, to see the expression associated with a
46
  promise, if \code{assign.env} is not the \code{\link{.GlobalEnv}}.
46
  promise, if \code{assign.env} is not the \code{\link{.GlobalEnv}}.
47
}
47
}
Line 69... Line 69...
69
 
69
 
70
### Promises in an environment [for advanced users]:  ---------------------
70
### Promises in an environment [for advanced users]:  ---------------------
71
 
71
 
72
e <- (function(x, y = 1, z) environment())(cos, "y", {cat(" HO!\n"); pi+2})
72
e <- (function(x, y = 1, z) environment())(cos, "y", {cat(" HO!\n"); pi+2})
73
## How can we look at all promises in an env (w/o forcing them)?
73
## How can we look at all promises in an env (w/o forcing them)?
74
gete <- function(e_)
74
gete <- function(e_) {
-
 
75
   ne <- names(e_)
-
 
76
   names(ne) <- ne
75
   lapply(lapply(ls(e_), as.name),
77
   lapply(lapply(ne, as.name),
76
          function(n) eval(substitute(substitute(X, e_), list(X=n))))
78
          function(n) eval(substitute(substitute(X, e_), list(X=n))))
77
 
79
}
78
(exps <- gete(e))
80
(exps <- gete(e))
79
sapply(exps, typeof)
81
sapply(exps, typeof)
80
 
82
 
81
(le <- as.list(e)) # evaluates ("force"s) the promises
83
(le <- as.list(e)) # evaluates ("force"s) the promises
82
stopifnot(identical(unname(le), lapply(exps, eval))) # and another "Ho!"
84
stopifnot(identical(le, lapply(exps, eval))) # and another "Ho!"
83
}
85
}
84
\keyword{programming}
86
\keyword{programming}
85
\keyword{data}
87
\keyword{data}