forked from clojerl/clojerl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfn.clje
373 lines (273 loc) · 8.77 KB
/
fn.clje
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
(ns examples.fn)
(def one 1)
(def prn (fn* [x] (io/format "~s~n" #erl((clj_rt/str x)))))
;; Resolve var in another ns
(prn {:a 2, #{1 2 3} one})
;; Provide the value of an fn-var as an argument
(prn prn)
;; Use if
(prn (if :test
(do
(prn ::then)
:then)
:else))
;; Assert uses throw
(def assert (fn* [result] (if result nil (erlang/error :failed-assertion))))
(assert (erlang/== (clj_rt/str one) "1"))
(def fixed-arity
(fn* [x y] (clj_rt/str [x y])))
;; Call a fn with single fixed arity
(prn (fixed-arity ::fixed ::arity))
(def multiple-fixed-arities
(fn*
([x] (clj_rt/str [:multiple-fixed-arities 1 x]))
([x y] (clj_rt/str [:multiple-fixed-arities 2 x y]))
([x y z] (clj_rt/str [:multiple-fixed-arities 3 x y z]))))
;; Call a fn with multiple fixed arities
(prn (multiple-fixed-arities :mult-fixed))
(prn (multiple-fixed-arities :a :b))
(prn (multiple-fixed-arities 1 2 3))
(def variadic-arity
(fn* [& xs] (clj_rt/str [:variadic-arity xs])))
(def variadic-arity-2
(fn* [x & xs] (clj_rt/str [:variadic-arity-2 x xs])))
;; Call a fn with a single variadic argument
(prn (variadic-arity 1 2 4))
(prn (variadic-arity 1 2 3))
(prn (variadic-arity 1 2 3 4))
(prn (variadic-arity))
(prn (variadic-arity-2 1))
(prn (variadic-arity-2 1 2 3))
(def multiple-variadic
(fn*
([x] (clj_rt/str [:multiple-variadic-1 x]))
([x y] (clj_rt/str [:multiple-variadic-2 x y]))
([x y z] (clj_rt/str [:multiple-variadic-3 x y z]))
([x y z & w] (clj_rt/str [:multiple-variadic-n x y z w]))))
;; Call a variadic fn that also has fixed arities
(prn (multiple-variadic 1))
(prn (multiple-variadic 1 2))
(prn (multiple-variadic 1 2 3))
(prn (multiple-variadic 1 2 3 4))
(prn (multiple-variadic 1 2 3 4 5 6 7 8 9 :a :b :c))
;; Call an anonymous fn with a single fixed arity
(prn ((fn* [x] x) :anon-fn-fixed))
;; Call an anonymous fn with multiple fixed arities
(prn ((fn* ([] :anon-fn-mult-0)
([_x] :anon-fn-mult-1))))
(prn ((fn* ([] :anon-fn-mult-0)
([_x] :anon-fn-mult-1))
:arg))
;; Call an anonymous fn with a single variadic arity
(prn ((fn* [& xs] [:anon-variadic xs]) 1 2 3))
(prn ((fn*
([] :anon-variadic-arity-0)
([& xs] xs))))
(prn ((fn*
([] :anon-variadic-arity-0)
([& xs] [:anon-variadic-arity xs])) 1))
(def apply-f
(fn*
([f x] (f x))))
;; Provide an anonymous fn as an argument to be used as a function
(apply-f (fn* [x]
(prn [:apply-f-anon-fixed x]))
:apply!!!)
(def apply (fn*
([f args] (clojerl.IFn/apply f (clj_rt/to_list args)))
([f x args] (clojerl.IFn/apply f (clj_rt/to_list (clj_rt/cons x args))))))
(apply (fn* [x]
(prn [:apply-anon-fixed x]))
[:apply!!!])
(apply (fn*
([x] (prn [:apply-anon-variadic-1 x]))
([x y] (prn [:apply-anon-variadic-2 x y]))
([x y & z] (prn [:apply-anon-variadic-n x y z])))
[1])
(apply (fn*
([x] (prn [:apply-anon-variadic-1 x]))
([x y] (prn [:apply-anon-variadic-2 x y]))
([x y & z] (prn [:apply-anon-variadic-n x y z])))
[1 2])
(apply (fn*
([x] (prn [:apply-anon-variadic-1 x]))
([x y] (prn [:apply-anon-variadic-2 x y]))
([x y & z] (prn [:apply-anon-variadic-n x y z])))
[1 2 3 4 5])
;; Recursively call anonymous fn
(prn
((fn* count-10
([x]
(if (erlang/> x 0)
(do
(prn [:anon-recur x])
(count-10 (erlang/- x 1)))
(prn [:anon-recur :done]))))
10
))
;; Recursively call variadic anonymous fn
(prn
((fn* count-10-variadic
([x & xs]
(if (erlang/> x 0)
(do
(prn [:variadic-anon-recur x xs])
(count-10-variadic (erlang/- x 1) x xs))
(prn [:variadic-anon-recur :done]))))
10
))
;; Provide an erlang function as an argument to be used as a function
(apply-f io/format.1 "io:format/1 FTW!!!~n")
(apply io/format.2
"io:format/2 FTW!!!: ~s~n"
[#erl("lala")])
;; (apply-f io/format.2 "io:format/1 FTW!!!~n") ;; This should fail
;; Provide a fn var as an argument to be used as a function
(apply-f prn :apply!!!)
;; Provide a fn variadic var as an argument to be used as a function
(prn (apply-f variadic-arity :apply-f-variadic))
(prn (apply variadic-arity []))
(prn
(apply variadic-arity
[:apply-variadic 1 2 3]))
(prn
(apply variadic-arity-2
[:apply-variadic-2 1 2 3]))
(prn
(apply variadic-arity-2
[:apply-variadic-2]))
(prn
(apply multiple-variadic
[:apply-multi-variadic 1]))
(prn
(apply multiple-variadic
[:apply-multi-variadic 1 2]))
(prn
(apply multiple-variadic
[:apply-multi-variadic 1 2 3]))
(prn
(apply multiple-variadic
[:apply-multi-variadic 1 2 3 4 5 6]))
;; Keywords as a function for maps
(prn (:a {:a 1}))
(prn (:a {:b 2} :not-found-a))
(prn (apply :b [{:b 2}]))
(prn (apply :b {:c 3} [:not-found-b]))
;; (prn (:a)) ;; This should fail
;; Define and call a recursive fn var
(def recursive
(fn* [x]
(if (erlang/> x 0)
(do
(prn [:recursive-fn x])
(recursive (erlang/- x 1)))
(prn [:recursive-fn :done]))))
(recursive 15)
(def variadic-recursive
(fn* y [x & xs]
(if (erlang/> x 0)
(do
(prn [:variadic-recursive-fn x xs])
(variadic-recursive (erlang/- x 1) x xs))
(prn [:variadic-recursive-fn :done xs]))))
(variadic-recursive 15)
;; Define a function with a named fn* that uses the same name
(def same-name-fn
(fn* same-name-fn
([x & xs]
(if (erlang/> x 0)
(do
(prn [:same-name-fn x xs])
(same-name-fn (erlang/- x 1) x xs))
(prn [:same-name-fn :done xs])))))
(same-name-fn 3)
;; Define a HOF that returns a function
(def dec-fn
(fn* [] (fn* [x] (erlang/- x 1))))
(prn ((dec-fn) 10))
;; Use a recursive fn* inside a var fn
(def recur-fn
(fn* recur-fn []
(let* [f (fn* [x]
(if (erlang/< 0 x)
(recur (erlang/- x 1))
x))]
(f 5))))
(prn (recur-fn))
(def ^{:macro true} should-throw
(fn* [_&form _&env & body]
(let* [tail '((throw :error)
(catch t e
(if (erlang/== e :error)
(throw "Error not generated"))))
body (clj_rt/cons 'do body)
eval-form (clj_rt/cons 'clj_compiler/eval
#erl((clj_rt/cons 'quote #erl(body))))]
(clj_rt/cons
'try
(clj_rt/cons eval-form tail)))))
(def neg? (fn* [x] (erlang/< x 0)))
(def dec (fn* [x] (erlang/- x 1)))
;; [#453] Check that not all fn forms are bound to the var
(def recur-in-let
(let* [y (fn* [x]
(if (neg? x) :fin (recur (dec x))))]
(y 10)))
;; [#453] `for` generates an internal loop, the def fails without the fix
;; (def def-using-for
;; (into {} (for [x (range 10)] [x (inc x)])))
;; [#453] fn inside a vector
(def def-vector-with-fn
[((fn* [x]
(if (neg? x) :fin (recur (dec x))))
10)])
;; [#453] fn inside a map
(def def-map-with-fn-1
{:foo ((fn* [x]
(if (neg? x) :fin (recur (dec x))))
10)})
(should-throw
(def def-map-with-fn-2
{:foo (fn* [x]
(if (neg? x) :fin (recur (dec x))))}))
;; [#453] fn inside an Erlang map
(def def-erl-map-with-fn-1
#erl{:foo ((fn* [x]
(if (neg? x) :fin (recur (dec x))))
10)})
(should-throw
(def def-erl-map-with-fn-2
#erl{:foo (fn* [x]
(if (neg? x) :fin (recur (dec x))))}))
;; [#453] fn inside a set
(def def-set-with-fn-1
#{:foo ((fn* [x]
(if (neg? x) :fin (recur (dec x))))
10)})
(should-throw
(def def-set-with-fn-2
#{:foo (fn* [x]
(if (neg? x) :fin (recur (dec x))))}))
;; [#453] fn inside a tuple
(def def-tuple-with-fn-1
#erl[:foo ((fn* [x]
(if (neg? x) :fin (recur (dec x))))
10)])
(should-throw
(def def-tuple-with-fn-2
#erl[:foo (fn* [x]
(if (neg? x) :fin (recur (dec x))))]))
;; [#490] `args` shouldn't get a new binding inside fn
(let* [args :foo]
((fn* [y] (assert (erlang/=:= args :foo))) 1))
;; [#679] Unreadable error message when calling undefined local function
(def no-args-fn
(fn* []))
(def call-no-args-fn-with-one-arg
(fn* [] (no-args-fn 1)))
;; [#778] fn returned by macro
(def ^{:macro true} return-fn
(fn* [_&form _&env & body]
(fn* [x] x)))
(should-throw
(def def- (return-fn)))