From ee07e3bef678d70e74c68b24915b10a7f7ec905a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kirill=20M=C3=BCller?= Date: Sun, 15 Nov 2020 13:41:58 +0100 Subject: [PATCH] Use () after all function calls --- README.Rmd | 6 +++--- README.md | 36 ++++++++++++++++++------------------ 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/README.Rmd b/README.Rmd index c9d6e6b..3e342f9 100644 --- a/README.Rmd +++ b/README.Rmd @@ -72,9 +72,9 @@ devtools::install_github("tidyverse/magrittr") ### Basic piping -* `x %>% f` is equivalent to `f(x)` +* `x %>% f()` is equivalent to `f(x)` * `x %>% f(y)` is equivalent to `f(x, y)` -* `x %>% f %>% g %>% h` is equivalent to `h(g(f(x)))` +* `x %>% f() %>% g() %>% h()` is equivalent to `h(g(f(x)))` Here, "equivalent" is not technically exact: evaluation is non-standard, and the left-hand side is evaluated before passed on to the right-hand side @@ -109,7 +109,7 @@ be used to apply the pipeline to values. Building functions in magrittr is therefore similar to building other values. ```{r} -f <- . %>% cos %>% sin +f <- . %>% cos() %>% sin() # is equivalent to f <- function(.) sin(cos(.)) ``` diff --git a/README.md b/README.md index cf7b23a..aa3468f 100644 --- a/README.md +++ b/README.md @@ -18,18 +18,18 @@ status](https://github.com/tidyverse/magrittr/workflows/R-CMD-check/badge.svg)]( The magrittr package offers a set of operators which make your code more readable by: - - structuring sequences of data operations left-to-right (as opposed +- structuring sequences of data operations left-to-right (as opposed to from the inside and out), - - avoiding nested function calls, - - minimizing the need for local variables and function definitions, +- avoiding nested function calls, +- minimizing the need for local variables and function definitions, and - - making it easy to add steps anywhere in the sequence of operations. +- making it easy to add steps anywhere in the sequence of operations. The operators pipe their left-hand side values forward into expressions -that appear on the right-hand side, i.e. one can replace `f(x)` with `x -%>% f()`, where `%>%` is the (main) pipe-operator. When coupling several -function calls with the pipe-operator, the benefit will become more -apparent. Consider this pseudo example: +that appear on the right-hand side, i.e. one can replace `f(x)` with +`x %>% f()`, where `%>%` is the (main) pipe-operator. When coupling +several function calls with the pipe-operator, the benefit will become +more apparent. Consider this pseudo example: ``` r the_data <- @@ -66,9 +66,9 @@ devtools::install_github("tidyverse/magrittr") ### Basic piping - - `x %>% f` is equivalent to `f(x)` - - `x %>% f(y)` is equivalent to `f(x, y)` - - `x %>% f %>% g %>% h` is equivalent to `h(g(f(x)))` +- `x %>% f()` is equivalent to `f(x)` +- `x %>% f(y)` is equivalent to `f(x, y)` +- `x %>% f() %>% g() %>% h()` is equivalent to `h(g(f(x)))` Here, “equivalent” is not technically exact: evaluation is non-standard, and the left-hand side is evaluated before passed on to the right-hand @@ -77,8 +77,8 @@ implication. ### The argument placeholder - - `x %>% f(y, .)` is equivalent to `f(y, x)` - - `x %>% f(y, z = .)` is equivalent to `f(y, z = x)` +- `x %>% f(y, .)` is equivalent to `f(y, x)` +- `x %>% f(y, z = .)` is equivalent to `f(y, z = x)` ### Re-using the placeholder for attributes @@ -87,14 +87,14 @@ right-hand side expression. However, when the placeholder only appears in a nested expressions magrittr will still apply the first-argument rule. The reason is that in most cases this results more clean code. -`x %>% f(y = nrow(.), z = ncol(.))` is equivalent to `f(x, y = nrow(x), -z = ncol(x))` +`x %>% f(y = nrow(.), z = ncol(.))` is equivalent to +`f(x, y = nrow(x), z = ncol(x))` The behavior can be overruled by enclosing the right-hand side in braces: -`x %>% {f(y = nrow(.), z = ncol(.))}` is equivalent to `f(y = nrow(x), z -= ncol(x))` +`x %>% {f(y = nrow(.), z = ncol(.))}` is equivalent to +`f(y = nrow(x), z = ncol(x))` ### Building (unary) functions @@ -103,7 +103,7 @@ later be used to apply the pipeline to values. Building functions in magrittr is therefore similar to building other values. ``` r -f <- . %>% cos %>% sin +f <- . %>% cos() %>% sin() # is equivalent to f <- function(.) sin(cos(.)) ```