-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathlambda.Rmd
More file actions
50 lines (36 loc) · 1.52 KB
/
lambda.Rmd
File metadata and controls
50 lines (36 loc) · 1.52 KB
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
---
title: "lambda Function Builder"
author: "John Mount"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{lambda Function Builder}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
The [`CRAN`](https://cran.r-project.org) version of the [`R`](https://www.r-project.org) package [`wrapr`](https://CRAN.R-project.org/package=wrapr) package now includes a concise anonymous function constructor: `l()`.
To use it please do the following: attach `wrapr` and ask it to place a definition for `l()` in your environment:
```{r wrapri}
library("wrapr")
wrapr::defineLambda(name = "l")
ls()
```
Note: throughout this document we are using the letter "`l`" as a stand-in for the Greek letter lambda, as this non-ASCII character can cause formatting problems in some situations.
You can use `l()` to define functions. The syntax is: `l(arg [, arg]*, body [, env=env])`. That
is we write a `l()`-call (which you can do by cutting and pasting) and list the desired function arguments and then the function body. For example the function that squares numbers is:
```{r fsq1}
l(x, x^2)
```
We can use such a function to square the first four positive integers as follows:
```{r fsq2}
sapply(1:4, l(x, x^2))
```
Dot-pipe style notation does not need the `l()` factory as it treats pipe stages
as expressions parameterized over the variable "`.`":
```{r fsqp}
1:4 %.>% { .^2 }
```
And we can also build functions that take more than one argument as follows:
```{r ft}
l(x, y, x + 3*y)
```