-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMailund_article_promises_env.R
51 lines (43 loc) · 1.12 KB
/
Mailund_article_promises_env.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
```{r Mailund}# {{{
Idea: Treat separately an R expression and the env to evaluate it.
R can NOT manipulate a promise. See Ch 20, quosures to turn promise into R object. Before get to Hadley, MAILUND seems clearer
https://mailund.dk/posts/promises-and-lazy-evaluation/
pryr:: note spelling
```
```{r mailund2, echo=TRUE}
# why does this work?
# b/c evaluation is function frame, not calling frame
f <- function(x, y, z = 2 * x) x + z
a <- b <- 1
f(a + b, stop("ARGH!!!"))
```
```{r}
f,g created in Global Env.
create `promises` only; no evaluation
Case I, f is also called from Global env
Case II, f is called when current_env is NOT Global
```
```{r}
library(rlang)
# create in Global
f <- function(x) pryr::promise_info(x)
# Case I
# call f in Global
f(x) # function env is Global
# Case II
# call f inside a differnt env
g <- function(z) {
print(current_env())
print(fn_env(f)) # Global
f(z)
}
g(x + y)
# f,g
env_names(current_env())
```
```{r}
# promise = expression, env (where called)
# Watch, once evaluate x, promise's env disappears
# Also, before evaluation, current_env not same as promise env
# Mailmund cont
```