-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextra.zp
340 lines (302 loc) · 9.86 KB
/
extra.zp
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
;; All definitions here are "borrowed" from
;; husk (github.com/justinethier/husk-scheme).
(define-syntax cond
(syntax-rules (else =>)
((cond (else result1 result2 ...))
((lambda () result1 result2 ...)))
((cond (test => result))
(let ((temp test))
(if (truthy? temp) (result temp))))
((cond (test => result) clause1 clause2 ...)
(let ((temp test))
(if (truthy? temp)
(result temp)
(cond clause1 clause2 ...))))
((cond (test)) test)
((cond (test) clause1 clause2 ...)
(let ((temp test))
(if temp
temp
(cond clause1 clause2 ...))))
((cond (test result1 result2 ...))
(if test ((lambda () result1 result2 ...))))
((cond (test result1 result2 ...)
clause1 clause2 ...)
(if test
((lambda () result1 result2 ...))
(cond clause1 clause2 ...)))))
(define-syntax case
(syntax-rules (else =>)
((case (key ...)
clauses ...)
(let ((atom-key (key ...)))
(case atom-key clauses ...)))
((case key
(else => result))
(result key))
((case key
(else result1 result2 ...))
(if #t ((lambda () result1 result2 ...))))
((case key
((atoms ...) result1 result2 ...))
(if (memv key '(atoms ...))
((lambda () result1 result2 ...))))
((case key
((atoms ...) => result)
clause clauses ...)
(if (memv key '(atoms ...))
(result key)
(case key clause clauses ...)))
((case key
((atoms ...) result1 result2 ...)
clause clauses ...)
(if (memv key '(atoms ...))
((lambda () result1 result2 ...))
(case key clause clauses ...)))))
(define-syntax when
(syntax-rules "when is equivalent to if without an else clause.
params:
- test: the predicate
- body: a vararg presenting the body that will be executed if <par>test</par> evaluates to true
complexity: O(1)
returns: an if clause" ()
((when test result1 result2 ...)
(if test
(begin result1 result2 ...)))))
(define-syntax letrec*
(syntax-rules ()
((letrec* ((var1 init1) ...) body1 body2 ...)
(let ((var1 #f) ...)
(set! var1 init1)
...
(let () body1 body2 ...)))))
; The vanilla SRFI 16 implementation
(define-syntax case-lambda
(syntax-rules "a version of lambda that allows for overloaded definitions.
Example:
<zepto>
(case-lambda
((x) (write \"called with one argument\"))
((x y) (write \"called with two arguments\"))
(r (write (++ \"called neither with one or two arguments, but \"
(->string (length r))))))
</zepto>
params:
- kvs: any amount of headers and bodys
complexity: O(n) where n is the amount of definitions
returns: A callable" ()
((case-lambda)
(lambda args
(error:from-string "case-lambda without any clauses.")))
((case-lambda
(?a1 ?e1 ...)
?clause1 ...)
(lambda args
(let ((l (length args)))
(case-lambda "CLAUSE" args l
(?a1 ?e1 ...)
?clause1 ...))))
((case-lambda "CLAUSE" ?args ?l
((?a1 ...) ?e1 ...)
?clause1 ...)
(if (= ?l (length '(?a1 ...)))
(apply (lambda (?a1 ...) ?e1 ...) ?args)
(case-lambda "CLAUSE" ?args ?l
?clause1 ...)))
((case-lambda "CLAUSE" ?args ?l
((?a1 . ?ar) ?e1 ...)
?clause1 ...)
(case-lambda "IMPROPER" ?args ?l 1 (?a1 . ?ar) (?ar ?e1 ...)
?clause1 ...))
((case-lambda "CLAUSE" ?args ?l
(?a1 ?e1 ...)
?clause1 ...)
(let ((?a1 ?args))
?e1 ...))
((case-lambda "CLAUSE" ?args ?l)
(error:from-string "Wrong number of arguments to case-lambda."))
((case-lambda "IMPROPER" ?args ?l ?k ?al ((?a1 . ?ar) ?e1 ...)
?clause1 ...)
(case-lambda "IMPROPER" ?args ?l (+ ?k 1) ?al (?ar ?e1 ...)
?clause1 ...))
((case-lambda "IMPROPER" ?args ?l ?k ?al (?ar ?e1 ...)
?clause1 ...)
(if (>= ?l ?k)
(apply (lambda ?al ?e1 ...) ?args)
(case-lambda "CLAUSE" ?args ?l
?clause1 ...)))))
;; These are the only homebrew definitions
(define-syntax unless
(syntax-rules "the inverse to if.
params:
- pred: the predicate
- body: the clause that should be evaluated if <par>pred</par> is false
- else: the clause that should be evaluated otherwise
complexity: O(1)
returns: an if clause" ()
((unless test result1 ...)
(if (not test)
result1
...))))
(define-syntax for
(syntax-rules "execute a body <par>n</par> times.
Exposes a variable <i>i</i> that evaluates to the current iteration.
params:
- n: the number of times <par>actions</par> should be evaluated
- actions: the actions (must be wrapped in <zepto>()</zepto>)
complexity: O(1)
returns: a <fun>do</fun> clause" ()
((for num (actions ...))
(do ((i 0 (+ i 1))) ((= i num)) actions ...))))
(define-syntax <|
(syntax-rules "pipe things from right to left.
params:
- args: a variable number of arguments that pipe into each other
complexity: O(1)
returns: the result of the last call" ()
((<| x) (x))
((<| x y) (x (y)))
((<| x y ...)
(x (<| y ...)))))
(define (|> y . x)
"pipe things from right to left
params:
- y: the first argument
- x: a vararg that holds any number of consecutive actions
complexity: the complexity of all input actions combined
returns: the result of the last call"
(define (all-but acc l)
(if (null? (cdr l))
acc
(all-but (+= acc (car l)) (cdr l))))
(if (null? x)
(if (procedure? y) (y) y)
((list:last x) (apply |> (cons y (all-but [] x))))))
(define-syntax receive
(syntax-rules ()
((receive formals expression body ...)
(call-with-values (lambda () expression)
(lambda formals body ...)))))
(define-syntax comment
(syntax-rules "ignore everything within this macro.
params:
- args: any number of arguments
complexity: O(1)
returns: nil" ()
((_ ...) (nil))))
(define (zepto:get-bindings str . env)
"get the bindings in <par>env</par> that match the prefix <par>str</par>.
params:
- str: the prefix to match
- env: an optional argument that holds the environment in which to match (defaults to the current env)
complexity: O(n) where n is the size of the environment
returns: a list of <zepto>[name binding]</zepto> bindings"
(hash:kv-reduce (lambda (acc kv)
(if (string:starts-with (car kv) str)
(+= acc kv)
acc))
[]
(env->hashmap (if (null? env) (current-env) (car env)))))
(define-syntax forever
(syntax-rules "runs <par>body</par> forever.
params:
- body: a variable argument that represents the body of the function to run
complexity: O(∞)
returns: never" ()
((_ body ...)
(letrec ((forever-fun (lambda () (begin body ... (forever-fun)))))
(forever-fun)))))
(define-syntax λ
(syntax-rules "a shorthand for lambda.
params:
- args: the lambda arguments
- body: the lambda body
complexity: O(1)
returns: a lambda" ()
((_ args body)
(lambda args body))))
(define-syntax ƒ
(syntax-rules "define a function.
params:
- args: the function arguments
- body: the function body
complexity: O(1)
returns: a function" ()
((_ args body)
(if (list? 'args)
(define args body)
(error "function head must be a list, was" 'args)))))
(define-syntax with-environment
(syntax-rules "call <par>body</par> with the current environment bound to <par>var</par>.
params:
- var: the variable name to which the environment is bound
- body: the body in which <par>var</par> is defined
complexity: O(1)
returns: the result of <par>body</par>" ()
((with-environment var body)
(let ((var (current-env)))
body))))
(define-syntax defmatch
(syntax-rules "matches <par>ks</par> with <par>vals</par> where vals can be
a regular list or expression.
params:
- ks: the names to bind to
- vals: the values to bind
complexity: O(n+k) (where n is the number of keys and k is the complexity of the expression in vals)
returns: nil" ()
((defmatch ks vals)
(with-environment env
(begin
(map ($ (eval `(define ,(list:car %) ,(list:cdr %)) env))
(zip-with cons 'ks vals))
(nil))))))
(define-syntax defined?
(syntax-rules "returns true if <par>name</par> is defined.
params:
- name: the name which is maybe defined
complexity: O(n) where n is the size of the environment
returns: a boolean" ()
((_ name)
(let ((env (current-env)))
(env:in? env 'name)))))
(define-syntax given
(syntax-rules "similar to let, but dispatches based on check functions.
Example:
<zepto>
(given a-value
(number? called-if-number)
(hash-map? called-if-hash-map)
(else the-wildcard-if-nothing-else-matches))
</zepto
params:
- args: varargs of the format described above
complexity: O(n*k) where n is the number of checks and k is their complexity
returns: the result of the matching function" (else)
((_ key (else res))
(res key))
((_ key (test result))
(if (test key)
(result key)))
((_ key (test result) clauses ...)
(if (test key)
(result key)
(given key clauses ...)))))
(define-syntax defmacro
(syntax-rules "simple version of the Common Lisp defmacro form.
params:
- name: the macro name
- args: the macro args
- docs: optional docstring
- form: the macro body
complexity: O(1)
returns: a macro" ()
((_ name (args ...) form)
(define-syntax name
(syntax-rules ()
((name args ...)
(eval (macro-expand `form))))))
((_ name (args ...) docs form)
(define-syntax name
(syntax-rules docs ()
((name args ...)
(eval (macro-expand `form))))))))