Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use () after all function calls #232

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(.))
```
Expand Down
36 changes: 18 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <-
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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

Expand All @@ -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(.))
```
Expand Down