Skip to content

Commit

Permalink
fix for CRAN
Browse files Browse the repository at this point in the history
  • Loading branch information
Bruno Rodrigues committed Mar 13, 2024
1 parent d3785e1 commit c2dba9f
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 55 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ Config/testthat/edition: 3
Encoding: UTF-8
LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
4 changes: 2 additions & 2 deletions dev/flat_chronicle.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ test_that("first and only error message is ok", {
r_select(bm)
expect_true(grepl("Can't subset columns that don't exist", result_pipe$log_df$message))
expect_true(grepl("Can't select columns that don't exist", result_pipe$log_df$message))
})
Expand All @@ -147,7 +147,7 @@ test_that("if multiple error messages, next ones are 'pipe failed'", {
r_mutate(bm = 3)
expect_true(grepl("Can't subset columns that don't exist", result_pipe$log_df$message[1]))
expect_true(grepl("Can't select columns that don't exist", result_pipe$log_df$message[1]))
expect_true(grepl("Pipeline failed upstream", result_pipe$log_df$message[2]))
expect_true(grepl("Pipeline failed upstream", result_pipe$log_df$message[3]))
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-record.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ test_that("first and only error message is ok", {
r_select(bm)


expect_true(grepl("Can't subset columns that don't exist", result_pipe$log_df$message))
expect_true(grepl("Can't select columns that don't exist", result_pipe$log_df$message))

})

Expand All @@ -25,7 +25,7 @@ test_that("if multiple error messages, next ones are 'pipe failed'", {
r_mutate(bm = 3)


expect_true(grepl("Can't subset columns that don't exist", result_pipe$log_df$message[1]))
expect_true(grepl("Can't select columns that don't exist", result_pipe$log_df$message[1]))
expect_true(grepl("Pipeline failed upstream", result_pipe$log_df$message[2]))
expect_true(grepl("Pipeline failed upstream", result_pipe$log_df$message[3]))

Expand Down
52 changes: 26 additions & 26 deletions vignettes/a-non-mathematician-s-introduction-to-monads.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ library(chronicler)

<!-- WARNING - This vignette is generated by {fusen} from dev/advanced-topics.Rmd: do not edit by hand -->

```{r include = FALSE}
```{r parsermd-chunk-1, include = FALSE}
library(chronicler)
library(testthat)
```
Expand All @@ -45,7 +45,7 @@ Suppose for instance that you wish for your functions to provide a log when
they're run. If your function looks like this:


```{r}
```{r parsermd-chunk-2}
my_sqrt <- function(x){
sqrt(x)
Expand All @@ -57,7 +57,7 @@ my_sqrt <- function(x){
Then you would need to rewrite this function like this:


```{r}
```{r parsermd-chunk-3}
my_sqrt <- function(x, log = ""){
list(sqrt(x),
Expand All @@ -76,7 +76,7 @@ There are two problems with such an implementation:
What do I mean with "these functions don't compose"? Consider another such function `my_log()`:


```{r}
```{r parsermd-chunk-4}
my_log <- function(x, log = ""){
list(log(x),
Expand All @@ -90,7 +90,7 @@ my_log <- function(x, log = ""){
`sqrt()` and `log()` compose, or rather, they can be chained:


```{r}
```{r parsermd-chunk-5}
10 |>
sqrt() |>
log()
Expand All @@ -100,7 +100,7 @@ my_log <- function(x, log = ""){
while this is not true for `my_sqrt()` and `my_log()`:


```{r eval = FALSE}
```{r parsermd-chunk-6, eval = FALSE}
10 |>
my_sqrt() |>
my_log()
Expand All @@ -120,7 +120,7 @@ having to rewrite every function, can be tackled using
Let's write one for our problem:


```{r}
```{r parsermd-chunk-7}
log_it <- function(.f, ..., log = NULL){
fstring <- deparse(substitute(.f))
Expand All @@ -138,7 +138,7 @@ log_it <- function(.f, ..., log = NULL){
We can now create our functions easily:


```{r}
```{r parsermd-chunk-8}
l_sqrt <- log_it(sqrt)
l_sqrt(10)
Expand All @@ -155,7 +155,7 @@ The second issue remains though; `l_sqrt()` and `l_log()` can't be composed/chai
this issue, we need another function, called `bind()`:


```{r}
```{r parsermd-chunk-9}
bind <- function(.l, .f, ...){
.f(.l$result, ..., .log = .l$log)
Expand All @@ -166,7 +166,7 @@ bind <- function(.l, .f, ...){
Using `bind()`, it is now possible to compose `l_sqrt()` and `l_log()`:


```{r}
```{r parsermd-chunk-10}
10 |>
l_sqrt() |>
bind(l_log)
Expand All @@ -178,7 +178,7 @@ We can check that the result is correct by comparing it the `$result` value
from the returned object to `log(sqrt(10))`:


```{r}
```{r parsermd-chunk-11}
log(sqrt(10))
```

Expand Down Expand Up @@ -210,7 +210,7 @@ I did not focus on `return`/`unit`. Also, using our function factory, it is easy
to implement `return/unit`:


```{r}
```{r parsermd-chunk-12}
unit <- log_it(identity)
```

Expand All @@ -228,7 +228,7 @@ data frames, so I used `flatten()` instead).
applies this undecorated function to the monadic value:


```{r}
```{r parsermd-chunk-13}
fmap <- function(m, f, ...){
fstring <- deparse(substitute(f))
Expand All @@ -243,7 +243,7 @@ fmap <- function(m, f, ...){
Let's first define a monadic value:


```{r}
```{r parsermd-chunk-14}
# Let’s use unit(), which we defined above, for this.
(m <- unit(10))
Expand All @@ -252,7 +252,7 @@ Let's first define a monadic value:
Let's now use `fmap()` to apply a non-decorated function to `m`:


```{r}
```{r parsermd-chunk-15}
fmap(m, log)
```

Expand All @@ -261,7 +261,7 @@ Suppose that instead of `log()` we used `l_log()` with `fmap()`
(so we’re using a decorated function instead of an undecorated one):


```{r}
```{r parsermd-chunk-16}
fmap(m, l_log)
```

Expand All @@ -270,7 +270,7 @@ itself a monadic value. We would like `flatten()/join()` to take care of this fo
be an implementation of `flatten()`:


```{r}
```{r parsermd-chunk-17}
flatten <- function(m){
list(result = m$result$result,
Expand All @@ -283,7 +283,7 @@ flatten <- function(m){
Let's try now:


```{r}
```{r parsermd-chunk-18}
flatten(fmap(m, l_log))
```

Expand All @@ -292,7 +292,7 @@ Great! Now, as explained earlier, `flatmap()` and `bind()` are the same thing. B
`flatmap()` is the composition of `flatten()` and `fmap()`:


```{r}
```{r parsermd-chunk-19}
# I first define a composition operator for functions
`%.%` <- \(f,g)(function(...)(f(g(...))))
Expand All @@ -305,7 +305,7 @@ flatmap <- flatten %.% fmap
So this means that we can now replace:


```{r}
```{r parsermd-chunk-20}
10 |>
l_sqrt() |>
bind(l_log)
Expand All @@ -315,7 +315,7 @@ So this means that we can now replace:
by:


```{r}
```{r parsermd-chunk-21}
10 |>
l_sqrt() |>
flatmap(l_log)
Expand All @@ -334,7 +334,7 @@ and `purrr::flatten()` is `flatten()`. This means we can obtain `flatmap()` from
`purrr::flatten()` and `purrr::map()`:


```{r}
```{r parsermd-chunk-22}
# Since I'm using `{purrr}`, might as well use purrr::compose() instead of my own implementation
flatmap_list <- purrr::compose(purrr::flatten, purrr::map)
Expand Down Expand Up @@ -374,7 +374,7 @@ The first law states that passing a monadic value to a monadic function using `b
function is the same.


```{r}
```{r parsermd-chunk-23}
a <- as_chronicle(10)
r_sqrt <- record(sqrt)
Expand All @@ -395,7 +395,7 @@ this package, in other words, the function that coerces values to chronicler obj
nothing. Here again we have an issue with the log, that's why I focus on the value:


```{r}
```{r parsermd-chunk-24}
test_that("second monadic law", {
expect_equal(bind_record(a, as_chronicle)$value, a$value)
})
Expand All @@ -408,7 +408,7 @@ The third law is about associativity; applying monadic functions successively or
first gives the same result.


```{r}
```{r parsermd-chunk-25}
a <- as_chronicle(10)
r_sqrt <- record(sqrt)
Expand Down Expand Up @@ -436,7 +436,7 @@ For exhaustivity's sake, I check that I can get `flatmap_record()` by composing
`fmap_record()`:


```{r}
```{r parsermd-chunk-26}
r_sqrt <- record(sqrt)
r_exp <- record(exp)
Expand Down
Loading

0 comments on commit c2dba9f

Please sign in to comment.