@@ -20,7 +20,7 @@ library(chronicler)
20
20
21
21
<!-- WARNING - This vignette is generated by {fusen} from dev/advanced-topics.Rmd: do not edit by hand -->
22
22
23
- ``` {r include = FALSE}
23
+ ``` {r parsermd-chunk-1, include = FALSE}
24
24
library(chronicler)
25
25
library(testthat)
26
26
```
@@ -45,7 +45,7 @@ Suppose for instance that you wish for your functions to provide a log when
45
45
they're run. If your function looks like this:
46
46
47
47
48
- ``` {r}
48
+ ``` {r parsermd-chunk-2 }
49
49
my_sqrt <- function(x){
50
50
51
51
sqrt(x)
@@ -57,7 +57,7 @@ my_sqrt <- function(x){
57
57
Then you would need to rewrite this function like this:
58
58
59
59
60
- ``` {r}
60
+ ``` {r parsermd-chunk-3 }
61
61
my_sqrt <- function(x, log = ""){
62
62
63
63
list(sqrt(x),
@@ -76,7 +76,7 @@ There are two problems with such an implementation:
76
76
What do I mean with "these functions don't compose"? Consider another such function ` my_log() ` :
77
77
78
78
79
- ``` {r}
79
+ ``` {r parsermd-chunk-4 }
80
80
my_log <- function(x, log = ""){
81
81
82
82
list(log(x),
@@ -90,7 +90,7 @@ my_log <- function(x, log = ""){
90
90
` sqrt() ` and ` log() ` compose, or rather, they can be chained:
91
91
92
92
93
- ``` {r}
93
+ ``` {r parsermd-chunk-5 }
94
94
10 |>
95
95
sqrt() |>
96
96
log()
@@ -100,7 +100,7 @@ my_log <- function(x, log = ""){
100
100
while this is not true for ` my_sqrt() ` and ` my_log() ` :
101
101
102
102
103
- ``` {r eval = FALSE}
103
+ ``` {r parsermd-chunk-6, eval = FALSE}
104
104
10 |>
105
105
my_sqrt() |>
106
106
my_log()
@@ -120,7 +120,7 @@ having to rewrite every function, can be tackled using
120
120
Let's write one for our problem:
121
121
122
122
123
- ``` {r}
123
+ ``` {r parsermd-chunk-7 }
124
124
log_it <- function(.f, ..., log = NULL){
125
125
126
126
fstring <- deparse(substitute(.f))
@@ -138,7 +138,7 @@ log_it <- function(.f, ..., log = NULL){
138
138
We can now create our functions easily:
139
139
140
140
141
- ``` {r}
141
+ ``` {r parsermd-chunk-8 }
142
142
l_sqrt <- log_it(sqrt)
143
143
144
144
l_sqrt(10)
@@ -155,7 +155,7 @@ The second issue remains though; `l_sqrt()` and `l_log()` can't be composed/chai
155
155
this issue, we need another function, called ` bind() ` :
156
156
157
157
158
- ``` {r}
158
+ ``` {r parsermd-chunk-9 }
159
159
bind <- function(.l, .f, ...){
160
160
161
161
.f(.l$result, ..., .log = .l$log)
@@ -166,7 +166,7 @@ bind <- function(.l, .f, ...){
166
166
Using ` bind() ` , it is now possible to compose ` l_sqrt() ` and ` l_log() ` :
167
167
168
168
169
- ``` {r}
169
+ ``` {r parsermd-chunk-10 }
170
170
10 |>
171
171
l_sqrt() |>
172
172
bind(l_log)
@@ -178,7 +178,7 @@ We can check that the result is correct by comparing it the `$result` value
178
178
from the returned object to ` log(sqrt(10)) ` :
179
179
180
180
181
- ``` {r}
181
+ ``` {r parsermd-chunk-11 }
182
182
log(sqrt(10))
183
183
```
184
184
@@ -210,7 +210,7 @@ I did not focus on `return`/`unit`. Also, using our function factory, it is easy
210
210
to implement ` return/unit ` :
211
211
212
212
213
- ``` {r}
213
+ ``` {r parsermd-chunk-12 }
214
214
unit <- log_it(identity)
215
215
```
216
216
@@ -228,7 +228,7 @@ data frames, so I used `flatten()` instead).
228
228
applies this undecorated function to the monadic value:
229
229
230
230
231
- ``` {r}
231
+ ``` {r parsermd-chunk-13 }
232
232
fmap <- function(m, f, ...){
233
233
234
234
fstring <- deparse(substitute(f))
@@ -243,7 +243,7 @@ fmap <- function(m, f, ...){
243
243
Let's first define a monadic value:
244
244
245
245
246
- ``` {r}
246
+ ``` {r parsermd-chunk-14 }
247
247
# Let’s use unit(), which we defined above, for this.
248
248
249
249
(m <- unit(10))
@@ -252,7 +252,7 @@ Let's first define a monadic value:
252
252
Let's now use ` fmap() ` to apply a non-decorated function to ` m ` :
253
253
254
254
255
- ``` {r}
255
+ ``` {r parsermd-chunk-15 }
256
256
fmap(m, log)
257
257
```
258
258
@@ -261,7 +261,7 @@ Suppose that instead of `log()` we used `l_log()` with `fmap()`
261
261
(so we’re using a decorated function instead of an undecorated one):
262
262
263
263
264
- ``` {r}
264
+ ``` {r parsermd-chunk-16 }
265
265
fmap(m, l_log)
266
266
```
267
267
@@ -270,7 +270,7 @@ itself a monadic value. We would like `flatten()/join()` to take care of this fo
270
270
be an implementation of ` flatten() ` :
271
271
272
272
273
- ``` {r}
273
+ ``` {r parsermd-chunk-17 }
274
274
flatten <- function(m){
275
275
276
276
list(result = m$result$result,
@@ -283,7 +283,7 @@ flatten <- function(m){
283
283
Let's try now:
284
284
285
285
286
- ``` {r}
286
+ ``` {r parsermd-chunk-18 }
287
287
flatten(fmap(m, l_log))
288
288
```
289
289
@@ -292,7 +292,7 @@ Great! Now, as explained earlier, `flatmap()` and `bind()` are the same thing. B
292
292
` flatmap() ` is the composition of ` flatten() ` and ` fmap() ` :
293
293
294
294
295
- ``` {r}
295
+ ``` {r parsermd-chunk-19 }
296
296
# I first define a composition operator for functions
297
297
`%.%` <- \(f,g)(function(...)(f(g(...))))
298
298
@@ -305,7 +305,7 @@ flatmap <- flatten %.% fmap
305
305
So this means that we can now replace:
306
306
307
307
308
- ``` {r}
308
+ ``` {r parsermd-chunk-20 }
309
309
10 |>
310
310
l_sqrt() |>
311
311
bind(l_log)
@@ -315,7 +315,7 @@ So this means that we can now replace:
315
315
by:
316
316
317
317
318
- ``` {r}
318
+ ``` {r parsermd-chunk-21 }
319
319
10 |>
320
320
l_sqrt() |>
321
321
flatmap(l_log)
@@ -334,7 +334,7 @@ and `purrr::flatten()` is `flatten()`. This means we can obtain `flatmap()` from
334
334
` purrr::flatten() ` and ` purrr::map() ` :
335
335
336
336
337
- ``` {r}
337
+ ``` {r parsermd-chunk-22 }
338
338
# Since I'm using `{purrr}`, might as well use purrr::compose() instead of my own implementation
339
339
flatmap_list <- purrr::compose(purrr::flatten, purrr::map)
340
340
@@ -374,7 +374,7 @@ The first law states that passing a monadic value to a monadic function using `b
374
374
function is the same.
375
375
376
376
377
- ``` {r}
377
+ ``` {r parsermd-chunk-23 }
378
378
a <- as_chronicle(10)
379
379
r_sqrt <- record(sqrt)
380
380
@@ -395,7 +395,7 @@ this package, in other words, the function that coerces values to chronicler obj
395
395
nothing. Here again we have an issue with the log, that's why I focus on the value:
396
396
397
397
398
- ``` {r}
398
+ ``` {r parsermd-chunk-24 }
399
399
test_that("second monadic law", {
400
400
expect_equal(bind_record(a, as_chronicle)$value, a$value)
401
401
})
@@ -408,7 +408,7 @@ The third law is about associativity; applying monadic functions successively or
408
408
first gives the same result.
409
409
410
410
411
- ``` {r}
411
+ ``` {r parsermd-chunk-25 }
412
412
a <- as_chronicle(10)
413
413
414
414
r_sqrt <- record(sqrt)
@@ -436,7 +436,7 @@ For exhaustivity's sake, I check that I can get `flatmap_record()` by composing
436
436
` fmap_record() ` :
437
437
438
438
439
- ``` {r}
439
+ ``` {r parsermd-chunk-26 }
440
440
441
441
r_sqrt <- record(sqrt)
442
442
r_exp <- record(exp)
0 commit comments