-
Notifications
You must be signed in to change notification settings - Fork 13
/
README.Rmd
233 lines (162 loc) · 4.47 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
221
222
223
224
225
226
227
228
229
230
231
232
233
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
knitr::knit_engines$set(
clojure = llr::knitr_language_engine()
)
```
# llr
<!-- badges: start -->
<!-- badges: end -->
llr is a small, work in progress and just for fun clojure-like lisp on top of R's abstract syntax trees. Expressions are not interpreted, but are translated to R's AST and then interpreted by the R interpreter.
Most implementation details are sub-optimal, but the focus is on having fun and producing results instead writing perfect code. There are also many bugs and inconsistencies!
## Installation
``` r
remotes::install_github("dirkschumacher/llr")
```
## Intro
```{clojure}
(->
r/datasets::mtcars
(r/dplyr::filter (r/base::`>` hp 100))
(r/dplyr::summarise :count (r/dplyr::n) :mean_mpg (r/mean mpg))
(r/tibble::as_tibble))
```
Or run it from R
```{r}
library(llr)
interp <- llr_env$new()
interp$eval("(+ 1 1)")
```
Also see some [Advent Of Code solutions](https://github.com/dirkschumacher/aoc2020) in llr.
### REPL
It also has a (limited) REPL
```{r, eval=FALSE}
interp <- llr_env$new()
interp$repl()
```
## Special forms
### Data Types
#### Lists
```{clojure}
; this is a list
'(1 2 3 4 5 6)
; an unquoted list is a function call
(+ 1 2 3 4 5 6)
```
#### Vectors
```{clojure}
[1 2 3 4]
```
#### Maps
```{clojure}
{:a 1 :b 2}
```
### Symbols
```{clojure, eval = FALSE}
x
```
```{clojure, eval = FALSE}
namespaced.variable/x
```
```{clojure, eval = FALSE}
:keyword
```
```{clojure, eval = FALSE}
"character"
```
```{clojure, eval = FALSE}
10 ; integer
```
```{clojure, eval = FALSE}
10.42 ; double
```
### Functions
```{clojure, eval = FALSE}
(fn [a b] (+ a b))
(fn this
([] 0)
([a] a)
([a b] (+ a b))
([a b & more] (reduce + (concat [a b] more))))
```
### def
`def` defines a symbol in a namespace and assignes it a name.
```{clojure}
(def x 1)
(def plus (fn [a b] (+ a b)))
(plus x x)
```
### Meta-data
Symbols and values can hold meta-data. That meta-data needs to be a map at the moment.
```{clojure}
(def ^{:const true} x ^{:meta "hello"} [ 1 2 3])
(meta x)
```
Meta-data on symbols is currently only available to the reader.
### Macros
Macros are also supported. Macros are functions bound to a name with meta data `{:macro true}`.
In a macro you can use syntax-quote `<backtick>` together with the unquote `~` and unquote-splice `~@` operators.
```{clojure}
(defmacro infix [operand1 operator operand2]
`(~operator ~operand1 ~operand2))
(infix 1 + 1)
```
### Recursion
Similar to Clojure llr uses `recur` to jump to a recursion point currently only defined by `loop`.
```{clojure}
(def is-even
(fn [number]
(loop [cnt number]
(if (zero? cnt)
true
(if (< cnt 0) false (recur (- cnt 2)))))))
```
```{clojure}
(is-even 5001)
```
```{clojure}
(is-even 5000)
```
### Namespaces
Every top level definition is part of a namespace
```{clojure}
(ns product.lib)
(defn compute [a b] (+ a b))
(ns user)
(product.lib/compute 10 32)
```
### Reader Dispatch
The reader switches to a different set of interpretations of the next symbol when reading the character `#`.
### `#_` ignores the next form
```{clojure}
#_ (r/stop "error")
"Yay"
```
### R interop
All symbols starting with the namespace `r/` are treated slightly differently. You can use that to refer to external R functions and symbols. In addition keywords are interpreted as named arguments.
```{clojure}
(r/set.seed 1)
(def rand-numbers (r/stats::rnorm :n 10))
(r/mean rand-numbers)
```
## Design Goals
* Have fun, experiment and learn :)
* Build a clojure-like language that supports R-interop using the `r/` namespace.
* Thus the core language should feel like clojure and support some of clojures's core
functions, but still make it easy to work with R's internal data structures.
## Contributing
* Please read the code-of-conduct and also be aware that this a fun project,
so things will break and progress is valued prefect code (at the moment).
* However everyone is invited to play around with the language, learn together,
extend it, document things, fix bugs and propose features.
## Code of Conduct
Please note that the llr project is released with a [Contributor Code of Conduct](https://contributor-covenant.org/version/2/0/CODE_OF_CONDUCT.html). By contributing to this project, you agree to abide by its terms.