File: sectioning_fun.R

package info (click to toggle)
r-cran-doby 4.7.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,400 kB
  • sloc: makefile: 2
file content (105 lines) | stat: -rw-r--r-- 2,969 bytes parent folder | download
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
options("digits"=3)
library(doBy)
library(microbenchmark)
#devtools::load_all()

## -----------------------------------------------------------------------------
fun  <- function(a, b, c=4, d=9) {
    a + b + c + d
}

## -----------------------------------------------------------------------------
fun_def <- section_fun(fun, list(b=7, d=10))
fun_def
fun_body <- section_fun(fun, list(b=7, d=10), method="sub")
fun_body
fun_env <- section_fun(fun, list(b=7, d=10), method = "env")
fun_env

## -----------------------------------------------------------------------------
get_section(fun_env) 
## same as: attr(fun_env, "arg_env")$args 
get_fun(fun_env) 
## same as: environment(fun_env)$fun

## -----------------------------------------------------------------------------
fun(a=10, b=7, c=5, d=10)
fun_def(a=10, c=5)
fun_body(a=10, c=5)
fun_env(a=10, c=5)

## -----------------------------------------------------------------------------
inv_toep <- function(n) {
    solve(toeplitz(1:n))
}

## ----eval=F-------------------------------------------------------------------
# microbenchmark(
#     inv_toep(4), inv_toep(8), inv_toep(16),
#     times=3
# )

## -----------------------------------------------------------------------------
n.vec  <- c(4, 8, 16)
fun_list <- lapply(n.vec,
                   function(ni) {
                       section_fun(inv_toep, list(n=ni))
                   })
fun_list

## -----------------------------------------------------------------------------
fun_list[[1]]
fun_list[[1]]()

## -----------------------------------------------------------------------------
bquote_list <- function(fun_list) {
    lapply(fun_list, function(g){
        bquote(.(g)())
    })
}

## -----------------------------------------------------------------------------
bq_fun_list <- bquote_list(fun_list)
bq_fun_list
bq_fun_list[[1]]
eval(bq_fun_list[[1]])

## -----------------------------------------------------------------------------
microbenchmark(
  list = bq_fun_list,
  times = 5
)

## -----------------------------------------------------------------------------
n.vec  <- seq(20, 80, by=20)
fun_def <- lapply(n.vec,
                  function(n){
                      section_fun(inv_toep, list(n=n), method="def")
                  })
fun_body <- lapply(n.vec,
                  function(n){
                      section_fun(inv_toep, list(n=n), method="sub")
                  })
fun_env <- lapply(n.vec,
                  function(n){
                      section_fun(inv_toep, list(n=n), method="env")
                  })

names(fun_def)  <- paste0("def", n.vec)
names(fun_body) <- paste0("body", n.vec)
names(fun_env)  <- paste0("env", n.vec)

bq_fun_list <- bquote_list(c(fun_def, fun_body, fun_env))
bq_fun_list |> head()

mb <- microbenchmark(
  list  = bq_fun_list,
  times = 2
)
mb