-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME.Rmd
88 lines (61 loc) · 1.46 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
---
output:
md_document:
variant: markdown_github
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "README-"
)
```
# terser
A natural and terse syntax of function declaration in R for people who are interested in
shooting yourself in the foot :gun:.
You can use `f(x, y) = x + y` to define a function `f`.
```{r include=FALSE}
library(terser)
```
```{r}
f(x, y) = x + y
f(3, 4)
```

> How... ?
> Yes! I overwrite the `=` operator.
If the left hand side is a symbol, it just does normal assignment like the `<-` operator.
If the left hand side is a function call, it defines a function.
Some more examples:
```{r}
x = 3
x
f(x, y) = x + y
f(2, 3)
identical(f, function(x, y) x + y)
g(x = 231) = log(x)
g()
identical(g, function(x = 231) log(x))
h(a, b = a^2) = a + b
h(1)
h(1, 2)
identical(h, function(a, b = a^2) a + b)
tan2(a) = sin(a)/cos(a)
tan2(pi)
`∑`(...) = sum(...)
`∑`(2, 3, 4)
```
## Warning
The operator `=` can no longer do assignment for attributes and elements, the following
will no longer work. Use `<-` instead.
```{r, eval=FALSE}
names(x) = c("a", "b", "c")
x[[2]] = 32
```
## See also
This package is inspired by a syntax for defining a function in Julia.
```
julia> f(x, y) = x + y
f (generic function with 1 method)
```