-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.go
382 lines (331 loc) · 8.49 KB
/
lib.go
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
package lispy
//necessary evil to include non-go file in a package to expose the API other apps can use to run lispy code
const lib = `
(define caar [x] (car (car x)))
(define cadr [x] (car (cdr x)))
(define cdar [x] (cdr (car x)))
(define cddr [x] (cdr (cdr x)))
; basic expressions
(define sqrt [x] (# x 0.5))
(define square [x] (* x x))
(define inc [x] (+ x 1))
(define dec [x] (- x 1))
(define abs [x]
(if (>= x 0) x (* x -1))
)
(define neg [x] (- 0 x))
(define ! [x] (if x false true))
(define neg? [x] (< x 0))
(define pos? [x] (> x 0))
(define zero? [x] (= x 0))
(define divisible? [a b] (= (% a b) 0))
(define even? [x] (zero? (% x 2)))
(define odd? [x] (! (even? x)))
(define nil? [x] (= x ()))
(define list? [x] (= (type x) "list"))
(define int? [x] (= (type x) "int"))
(define float? [x] (= (type x) "float"))
(define symbol? [x] (= (type x) "symbol"))
; list methods
(define range [start stop step]
(if (< start stop)
(cons start (range (+ start step) stop step))
()
)
)
(define reduce [arr func current]
(if (nil? arr)
current
(reduce (cdr arr) func (func current (car arr)))
)
)
(define max [arr]
(if (nil? arr)
0
(reduce arr (fn [a b] (if (< a b) b a)) (car arr))
)
)
(define min [arr]
(if (nil? arr)
0
(reduce arr (fn [a b] (if (> a b) b a)) (car arr))
)
)
(define sum [arr]
(if (nil? arr)
0
(reduce arr + 0)
)
)
; defines a list from 0...x-1
(define seq [x] (range 0 x 1))
(define map [arr func]
(if (nil? arr)
()
(cons (func (car arr)) (map (cdr arr) func))
)
)
(define filter [arr func]
(if (nil? arr)
()
(if (func (car arr))
(cons (car arr) (filter (cdr arr) func))
(filter (cdr arr) func)
)
)
)
; O(n) operation, loop through entire list and add to end
(define append [arr el]
(if (nil? arr)
(list el)
(cons (car arr) (append (cdr arr) el))
)
)
; O(n^2) since each append is O(n)
(define reverse [arr]
(if (nil? arr)
()
(append (reverse (cdr arr)) (car arr))
)
)
(define each [arr func]
(if (nil? arr)
()
(
do
(println (func (car arr)))
(each (cdr arr) func)
)
)
)
; generate unique, not previously defined symbol
(define gensym []
(symbol (str "var" (* 10000 (rand))))
)
; get nth item in list (0-indexed)
(define nth [arr n]
(if (= n 0)
(car arr)
(nth (cdr arr) (dec n))
)
)
; get size of list
(define size [arr]
(do
(define iterSize [n arr]
(if (nil? arr)
n
(iterSize (inc n) (cdr arr))
)
)
(iterSize 0 arr)
)
)
; get index of item in list
(define index [arr item]
(do
(define getIndex [index arr item]
(if (nil? arr)
-1
(if (= (car arr) item)
index
(getIndex (inc index) (cdr arr) item)
)
)
)
(getIndex 0 arr item)
)
)
; get last item in list
(define last [arr]
(if (nil? (cdr arr))
(car arr)
(last (cdr arr))
)
)
; appends arr2 to the end of arr1
(define join [arr1 arr2]
(if (nil? arr2)
arr1
(join (append arr1 (car arr2)) (cdr arr2))
)
)
; adds element to the front of the array
(define addToFront [el arr]
(do
(define helper [arr]
(if (nil? arr)
()
(cons (car arr) (helper (cdr arr)))
)
)
(helper (cons el arr))
)
)
; macros
; (when (precondition) (postcondition))
(macro when [terms]
(list 'if (car terms) (cadr terms))
)
; local bindings within lexical scope
(macro let [terms]
(do
(define declname (caar terms))
(define declval (cdar terms))
(define body (cdr terms))
(list
(list 'fn [declname] body)
declval
)
)
)
; ex: (quasiquote (1 2 (unquote (+ 3 4)))) => (1 2 7)
; note, by design, don't include ' before it
(macro quasiquote [terms]
; note we do cons 'list so that map is called when evaluating the macro-expansion, not on the first call
(cons 'list
(map (car terms)
(fn [term]
(if (list? term)
(if (= (car term) 'unquote)
(cadr term)
(list 'quasiquote term)
)
(list 'quote term)
)
)
)
)
)
; special form of a funcCall where the last argument is a list that should be treated as parameters
; e.g. (apply fn 1 2 (3 4))
(define apply [& terms]
(do
(define funcCall (car terms))
(define helper [args]
(if (nil? args)
()
(if (list? (car args))
(cons (caar args) (helper (cdar args)))
(cons (car args) (helper (cdr args)))
)
)
)
(applyTo funcCall (helper (cdr terms)))
)
)
; (cond (precondition) (postcondition) (precondition2) (postcondition2)...)
(macro cond [terms]
(if (nil? terms)
()
(list 'if (car terms) (cadr terms) (cons 'cond (cddr terms)))
)
)
;(switch val (case1 result1) (case2 result2))
(macro switch [statements]
(do
(define val (gensym))
(define match [conditions]
(if (nil? conditions)
(list)
(list 'if (list '= val (caar conditions)) (cdar conditions) (match (cdr conditions)))
)
)
(let (val (car statements))
(match (cdr statements))
)
)
)
; thread-first
; inserts first form as the first argument (second in list) of the second form, and so forth
(macro -> [terms]
(do
(define apply-partials [partials expr]
(if (nil? partials)
expr
(if (symbol? (car partials))
(list (car partials) (apply-partials (cdr partials) expr))
; if it's a list with other parameters, insert expr (recursive call)
; as second parameter into partial (note need to use cons to ensure same list for func args)
(cons (caar partials) (cons (apply-partials (cdr partials) expr) (cdar partials)))
)
)
)
(apply-partials (reverse (cdr terms)) (car terms))
)
)
; thread-last
; same as -> but inserts first form as last argument (last in list) of second form, and so forth
(macro ->> [terms]
(do
(define apply-partials [partials expr]
(if (nil? partials)
expr
(if (symbol? (car partials))
(list (car partials) (apply-partials (cdr partials) expr))
; if it's a list with other parameters, insert expr (recursive call)
; as last form
(cons (caar partials) (append (cdar partials) (apply-partials (cdr partials) expr)))
)
)
)
(apply-partials (reverse (cdr terms)) (car terms))
)
)
; immutable key-value hashmap
; O(n) lookup with O(1) insert
; ex: (comp "key1" "val1" "key2" "val2")
(macro hash-map [terms]
(if (nil? terms)
()
(list 'cons (list 'cons (car terms) (cadr terms)) (cons 'hash-map (cddr terms)))
)
)
; O(n) recursive lookup
(define get [hm key]
(if (nil? hm)
()
(if (= key (caar hm))
(car (cdar hm))
(get (cdr hm) key)
)
)
)
; hash-maps are immutable, add returns a new hash-map with the new key, val pair
; it does not modify existing ones
(define add [hm key val]
(if (nil? (get hm key))
(cons (cons key val) hm)
)
)
; remove returns a hash-map with the key-value pair provided removed (if it exists in the hash-map)
(define remove [hm key]
(do
(define val (get hm key))
(define helper [hm]
(if (nil? hm)
()
(if (= (car (cdar hm)) val)
(helper (cdr hm))
(cons (car hm) (helper (cdr hm)))
)
)
)
(helper hm)
)
)
; return list of keys in the hash-map
(define keys [hm]
(if (nil? hm)
()
(cons (caar hm) (keys (cdr hm)))
)
)
; return list of values in the hash-map
(define values [hm]
(if (nil? hm)
()
(cons (car (cdar hm)) (values (cdr hm)))
)
)
`