-
Notifications
You must be signed in to change notification settings - Fork 172
/
Copy pathControlMacros.carp
286 lines (256 loc) · 9.14 KB
/
ControlMacros.carp
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
(hidden thread-first-internal)
(defndynamic thread-first-internal [xs]
(if (= (length xs) 2)
(if (list? (last xs))
(cons (caadr xs)
(cons (car xs)
(cdadr xs)))
(list (cadr xs) (car xs)))
(if (list? (last xs))
(append
(list
(car (last xs))
(thread-first-internal (all-but-last xs)))
(cdr (last xs)))
(list (last xs) (thread-first-internal (all-but-last xs))))))
(hidden thread-last-internal)
(defndynamic thread-last-internal [xs]
(if (= (length xs) 2)
(if (list? (last xs))
(cons-last (car xs) (last xs))
(list (cadr xs) (car xs)))
(if (list? (last xs))
(cons-last (thread-last-internal (all-but-last xs)) (last xs))
(list (last xs) (thread-last-internal (all-but-last xs))))))
(deprecated => "deprecated in favor of `->`.")
(defmacro => [:rest forms]
(thread-first-internal forms))
(deprecated ==> "deprecated in favor of `-->`.")
(defmacro ==> [:rest forms]
(thread-last-internal forms))
(doc -> "threads the first form through the following ones, making it the first
argument.
Example:
```
(-> 1
(- 10)
(* 5)
) ; => -45
```")
(defmacro -> [:rest forms]
(thread-first-internal forms))
(doc --> "threads the first form through the following ones, making it the last
argument.
Example:
```
(--> 1
(- 10)
(/ 45)
) ; => 5
```")
(defmacro --> [:rest forms]
(thread-last-internal forms))
(hidden comp-internal)
(defndynamic comp-internal [sym fns]
(if (= (length fns) 0)
sym
(list (car fns) (comp-internal sym (cdr fns)))))
(doc comp "Composes the functions `fns` into one `fn`.")
(defmacro comp [:rest fns]
(let [x (gensym)]
(list 'fn [x] (comp-internal x fns))))
(doc doto
"Evaluates `thing`, then calls all of the functions on it and"
"returns it. Useful for chaining mutating, imperative functions, and thus"
"similar to `->`. If you need `thing` to be passed as a `ref` into `expressions`"
"functions, use [`doto-ref`](#doto-ref) instead."
""
"```"
"(let [x @\"hi\"]"
" @(doto &x"
" (string-set! 0 \o)"
" (string-set! 1 \y)))"
"```")
(defmacro doto [thing :rest expressions]
(let [s (gensym)]
(list 'let [s thing]
(cons-last
s
(cons 'do (map (fn [expr] (cons (car expr) (cons s (cdr expr)))) expressions))))))
(doc doto-ref
"Evaluates `thing`, then calls all of the functions on it and"
"returns it. Useful for chaining mutating, imperative functions, and thus"
"similar to `->`. If you need `thing` not to be passed as a `ref` into"
"`expressions` functions, use [`doto`](#doto) instead."
""
"```"
"(doto-ref @\"hi\""
" (string-set! 0 \o)"
" (string-set! 1 \y))"
"```")
(defmacro doto-ref [thing :rest expressions]
(let [s (gensym)]
(list 'let [s thing]
(cons-last
s
(cons 'do
(map (fn [expr] (cons (car expr) (cons (list 'ref s) (cdr expr))))
expressions))))))
(doc until "Executes `body` until the condition `cnd` is true.")
(defmacro until [cnd body]
(list 'while (list 'not cnd)
body))
(doc let-do "is a `let` with an implicit `do` body.")
(defmacro let-do [bindings :rest forms]
(list 'let bindings
(cons 'do forms)))
(doc while-do "is a `while` with an implicit `do` body.")
(defmacro while-do [condition :rest forms]
(list 'while condition
(cons 'do forms)))
(doc defn-do "is a `defn` with an implicit `do` body.")
(defmacro defn-do [name arguments :rest body]
(eval (list 'defn name arguments (cons 'do body))))
(doc forever-do "is a `forever` with an implicit `do` body.")
(defmacro forever-do [:rest forms]
(list 'while true (cons 'do forms)))
(doc ignore-do
("Wraps side-effecting `forms` in a `do`, " false)
"ignoring all of their results."
"In other words, executes `forms` only for their side effects.")
(defmacro ignore-do [:rest forms]
(cons 'do (expand (apply ignore* forms))))
(doc when "is an `if` without an else branch.")
(defmacro when [condition form]
(list 'if condition form (list)))
(doc unless "is an `if` without a then branch.")
(defmacro unless [condition form]
(list 'if condition (list) form))
(hidden treat-case-handler)
(defndynamic treat-case-handler [name handler]
(if (and (list? handler) (> (length handler) 1) (= ':or (car handler)))
(cons 'or (map (fn [val] (list '= name val)) (cdr handler)))
(list '= name handler)))
(hidden case-internal)
(defndynamic case-internal [name xs]
(if (= (length xs) 0)
(list)
(if (= (length xs) 2)
(macro-error "case has even number of branches; add an else branch")
(if (= (length xs) 1)
(car xs)
(list 'if
(treat-case-handler name (car xs))
(cadr xs)
(case-internal name (cddr xs)))))))
(doc case "takes a form and a list of branches which are value and operation
pairs. If a value matches (or any in a list of values preced by `:or`), the
operation is executed. It takes a catch-all else branch that is executed if
nothing matches.
Example:
```
(case (+ 10 1)
10 (println* \"nope\")
11 (println* \"yup\")
(:or 12 13) (println* \"multibranch, but nope\")
(println* \"else branch\")
)
```")
(defmacro case [form :rest branches]
(let [name (gensym)]
(list 'let [name form]
(case-internal name branches))))
(defmodule Dynamic
(doc flip
"Flips the arguments of a function `f`."
"```"
"((flip Symbol.prefix) 'Bar 'Foo)"
"=> (Foo.Bar)"
"```")
(defndynamic flip [f]
(fn [x y]
(f y x)))
;; Higher-order functions can't currently accept primitives
;; For now, wrapping primitives in a function allows us to pass them
;; to HOFs like map.
(doc compose
"Returns the composition of two functions `f` and `g` for functions of any"
"arity; concretely, returns a function accepting the correct number of"
"arguments for `g`, applies `g` to those arguments, then applies `f` to the"
"result."
""
"If you only need to compose functions that take a single argument (unary arity)"
"see `comp`. Comp also generates the form that corresponds to the composition,"
"compose contrarily evaluates 'eagerly' and returns a computed symbol."
"```"
";; a silly composition"
"((compose empty take) 3 [1 2 3 4 5])"
";; => []"
""
"(String.join (collect-into ((compose reverse map) Symbol.str '(p r a c)) array))"
";; => 'carp'"
""
";; comp for comparison"
"((comp (curry + 1) (curry + 2)) 4)"
";; => (+ 1 (+ 2 4))"
"```")
(defndynamic compose [f g]
;; Recall that **unquoted** function names evaluate to their definitions in
;; dynamic contexts, e.g. f = (dyanmic f [arg] body)
;;
;; Right now, this cannot handle anonymous functions because they cannot be passed to apply.
;; and not anonymous functions.
;; commands expand to (command <name>), fns expand to a non-list.
;;
;; TODO: Support passing anonymous functions.
(if (not (or (list? f) (list? g)))
(macro-error "compose can only compose named dynamic functions. To
compose anonymous functions, such as curried functions,
see comp.")
(let [f-name (cadr f)
g-name (cadr g)
arguments (caddr g)]
(list 'fn arguments
;; Since we call an eval to apply g immediately, we wrap the args in an
;; extra quote, otherwise, users would need to double quote any sequence of
;; symbols such as '(p r a c)
(list f-name (list 'eval (list 'apply g-name (list 'quote arguments))))))))
(doc curry
"Returns a curried function accepting a single argument, that applies `f` to `x`"
"and then to the following argument."
""
"```"
"(map (curry Symbol.prefix 'Foo) '(bar baz))"
"=> (Foo.bar Foo.baz)"
"```")
(defndynamic curry [f x]
(fn [y]
(f x y)))
(doc curry*
"Curry functions of any arity."
""
"```"
"(map (curry* Dynamic.zip + '(1 2 3)) '((4 5) (6)))"
"=> (((+ 1 4) (+ 2 5)) ((+ 1 6)))"
""
"((curry* Dynamic.zip cons '(1 2 3)) '((4 5) (6)))"
"=> ((cons 1 (4 5)) (cons (2 (6))))"
""
"(defndynamic add-em-up [x y z] (+ (+ x y) z))"
"(map (curry* add-em-up 1 2) '(1 2 3))"
"=> (4 5 6)"
"```")
(defndynamic curry* [f :rest args]
(let [f-name (cadr f)
all-args (caddr f)
unfilled-args (- (length all-args) (length args))
remaining (take unfilled-args all-args)
;; Quote the arguments to retain expected behavior and avoid the need
;; for double quotes in curried higher-orders, e.g. zip.
quote-args (map quoted args)]
(list 'fn remaining
;; eval to execute the curried function.
;; otherwise, this resolves to the form that will call the function, e.g. (add-three-vals 2 3 1)
(list 'eval (list 'apply f-name (list 'quote (append quote-args (collect-into
remaining list))))))))
)