-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathREADME.Rmd
220 lines (156 loc) · 5.27 KB
/
README.Rmd
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "README-"
)
```
> Unless you need `curry()` or `curry_fn()`, you should use the more versatile
[gestalt](https://github.com/egnha/gestalt) package, which includes `fn()`.
[](https://travis-ci.org/egnha/nofrills)
[](https://app.codecov.io/gh/egnha/nofrills)
[](https://cran.r-project.org/package=nofrills)
# nofrills <img src="inst/logo.png" align="right" />
_Low-Cost Anonymous Functions_
## Overview
_nofrills_ is a lightweight R package that provides `fn()`, a more powerful
variation of `function()` that:
- **costs less** — enables tidyverse quasiquotation so you don’t pay the price
of [functional impurity](#pure-functions-via-quasiquotation)
- has the **same great taste** — supports a superset of `function()`’s syntax
and capabilities
- is **less filling** —
```{r, eval = FALSE}
fn(x, y = 1 ~ x + y)
```
is equivalent to
```{r, eval = FALSE}
function(x, y = 1) x + y
```
## Installation
```{r, eval = FALSE}
install.packages("nofrills")
```
Alternatively, install the development version from GitHub:
```{r gh-installation, eval = FALSE}
# install.packages("devtools")
devtools::install_github("egnha/nofrills")
```
## Usage
```{r, echo = FALSE}
library(nofrills)
```
### Same syntax as `function()` but shorter
```{r}
fn(x ~ x + 1)
fn(x, y ~ x + y)
fn(x, y = 2 ~ x + y)
fn(x, y = 1, ... ~ log(x + y, ...))
# the only exception, cf. alist()
fn(x, ... = , y ~ log(x + y, ...))
fn(~ NA)
```
### Supports quasiquotation
#### Unquote values
```{r}
z <- 0
fn(x, y = !!z ~ x + y)
fn(x ~ x > !!z)
```
#### Unquote argument names
```{r}
arg <- "y"
fn(x, !!arg := 0 ~ x + !!as.name(arg))
```
#### Splice in argument lists
```{r}
args <- alist(x, y = 0)
fn(!!!args, ~ x + y) # note the one-sided formula
```
#### Literally unquote with `QUQ()`, `QUQS()`
```{r message = FALSE}
library(dplyr)
summariser <- quote(mean)
my_summarise <- fn(df, ... ~ {
group_by <- quos(...)
df %>%
group_by(QUQS(group_by)) %>%
summarise(a = (!!summariser)(a))
})
my_summarise
```
(Source:
[_Programming with dplyr_](https://dplyr.tidyverse.org/articles/programming.html))
### [Curry](https://en.wikipedia.org/wiki/Currying) functions
#### Declare a curried function with `curry_fn()`
The syntax is the same as `fn()`. Using the literal unquoting operators `QUQ()`,
`QUQS()`, you can “delay” unquoting to embed argument values in the innermost
function:
```{r}
compare_to <- curry_fn(target, x ~ identical(x, QUQ(target)))
is_this <- compare_to("this")
# The embedded value "this" renders the source comprehensible
is_this
```
#### Curry a function with `curry()`
```{r}
curry(function(x, y, z = 0) x + y + z)
double <- curry(`*`)(2)
double(3)
```
## Pure functions via quasiquotation
Functions in R are generally
[impure](https://en.wikipedia.org/wiki/Pure_function), i.e., the return value of
a function will _not_ in general be determined by the value of its inputs alone.
This is because a function may depend on mutable objects in its [lexical
scope](https://adv-r.hadley.nz/functions.html#lexical-scoping). Normally this
isn’t an issue. But if you are working interactively and sourcing files into the
global environment, say, or using a notebook interface (like Jupyter or R
Notebook), it can be tricky to ensure that you haven’t unwittingly mutated an
object that an earlier function depends upon.
- Consider the following function:
```{r}
a <- 1
foo <- function(x) x + a
```
What is the value of `foo(1)`? It is not necessarily `2` because the value
of `a` may have changed between the _creation_ of `foo()` and the _calling_
of `foo(1)`:
```{r}
foo(1)
a <- 0
foo(1)
```
In other words, `foo()` is impure because the value of `foo(x)` depends not
only on the value of `x` but also on the _externally mutable_ value of `a`.
`fn()` enables you to write **pure(r)** functions by using quasiquotation to
eliminate such indeterminacy.
- With `fn()`, you can unquote `a` to capture its value at the point of
creation:
```{r}
a <- 1
foo <- fn(x ~ x + !!a)
```
Now `foo()` is a pure function, unaffected by changes in its lexical scope:
```{r}
foo(1)
a <- 0
foo(1)
```
## Alternatives to nofrills
Alternative anonymous-function constructors (which don’t support quasiquotation)
include:
- [`pryr::f()`](https://github.com/hadley/pryr)
- [`lambda::f()`](https://github.com/jimhester/lambda)
- [`rlang::as_function()`](https://rlang.r-lib.org/reference/as_function.html)
## Acknowledgement
The [rlang](https://github.com/r-lib/rlang) package by [Lionel
Henry](https://github.com/lionel-) and [Hadley
Wickham](https://github.com/hadley) makes nofrills possible. Crucially, rlang
provides the engine for quasiquotation and expression capture.
## License
MIT Copyright © 2017–22 [Eugene Ha](https://github.com/egnha)