-
Notifications
You must be signed in to change notification settings - Fork 0
/
lfe_clj.txt
673 lines (416 loc) · 17.7 KB
/
lfe_clj.txt
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
lfe_clj(3) lfe_clj(3)
NAME
clj - LFE Clojure interface library.
SYNOPSIS
This module provides Clojure-inspired functions and macros for use in
LFE.
EXPORTS
N.B. Instead of making fully-qualified calls to the macros exported
from clj, you may (include-lib "lfe/include/clj.lfe") and then call
them directly, e.g.
(include-lib "lfe/include/clj.lfe")
(-> 2 (+ 2) (=:= 4)) ; 'true
Function Macros
(defn name [arg ...] {{doc-string}} ...)
(defn {{doc-string}} ([argpat ...] ...))
Define and automatically export a function.
(defn- name [arg ...] {{doc-string}} ...)
(defn- {{doc-string}} ([argpat ...] ...))
Equivalent to defun.
(fn (arg ...) ...)
Equivalent to lambda.
Threading Macros
Note: The original versions were copied from Tim Dysinger’s lfesl repo
here:
https://github.com/lfex/lfesl/blob/master/include/thread.lfe
They have since been modified to be safely exportable.
(-> ...)
Thread first.
Example usage, demonstrating ordering:
> (set o '(#(a 1) #(b 2) #(c 3)))
(#(a 1) #(b 2) #(c 3))
> (clj:-> o
> (++ '(#(d 4)))
> (++ '(#(e 5)))
> (++ '(#(f 6))))
(#(a 1) #(b 2) #(c 3) #(d 4) #(e 5) #(f 6))
Note that the use of -> in this example results in each successive val‐
ue being appended to the input list.
Another example showing how this works:
> (lists:sublist
> (lists:reverse
> (lists:sort
> (lists:merge
> (string:tokens
> (string:to_upper "a b c d e")
> " ")
> '("X" "F" "L"))))
> 2 3)
("L" "F" "E")
Can be rewritten as this:
> (clj:-> "a b c d e"
> (string:to_upper)
> (string:tokens " ")
> (lists:merge '("X" "F" "L"))
> (lists:sort)
> (lists:reverse)
> (lists:sublist 2 3))
("L" "F" "E")
(->> ...)
Thread last.
Example usage, demonstrating ordering:
> (set o '(#(a 1) #(b 2) #(c 3)))
(#(a 1) #(b 2) #(c 3))
> (clj:->> o
> (++ '(#(d 4)))
> (++ '(#(e 5)))
> (++ '(#(f 6))))
(#(f 6) #(e 5) #(d 4) #(a 1) #(b 2) #(c 3))
Note that the use of ->> in this example results in each successive
value being prepended to the input list.
Another example showing how this:
> (lists:foldl #'+/2 0
> (clj:take 10
> (lists:filter
> (clj:comp #'clj:even?/1 #'clj:round/1)
> (lists:map
> (lambda (x)
> (math:pow x 2))
> (clj:seq 42)))))
1540.0
Can be rewritten as this:
> (clj:->> (clj:seq 42)
> (lists:map (lambda (x) (math:pow x 2)))
> (lists:filter (clj:comp #'clj:even?/1 #'clj:round/1))
> (clj:take 10)
> (lists:foldl #'+/2 0))
1540.0
(as-> expr name . sexps)
Bind name to expr, evaluate the first sexp in the lexical context of
that binding, then bind name to that result, repeating for each succes‐
sive sexp in sexps, returning the result of the last sexp.
(cond-> expr . clauses)
Given an expression and a set of test/sexp pairs, thread x (via ->)
through each sexp for which the corresponding test expression is
truthy, i.e. neither 'false nor 'undefined. Note that, unlike cond
branching, cond-> threading does not short circuit after the first
truthy test expression.
(cond->> expr . clauses)
Given an expression and a set of test/sexp pairs, thread x (via ->>)
through each sexp for which the corresponding test expression is
truthy, i.e. neither 'false nor 'undefined. Note that, unlike cond
branching, cond->> threading does not short circuit after the first
truthy test expression.
(some-> x . sexps)
When x is not 'undefined, thread it into the first sexp (via ->), and
when that result is not 'undefined, through the next, etc.
(some->> x . sexps)
When x is not 'undefined, thread it into the first sexp (via ->>), and
when that result is not 'undefined, through the next, etc.
Conditional Macros
(if-let ((patt test)) then {{else}})
If test evaluates to anything other than 'false or 'undefined, evaluate
then with patt bound to the value of test, otherwise else, if supplied,
else 'undefined.
(iff-let ((patt test)) . body)
When test evaluates to anything other than 'false or 'undefined, evalu‐
ate body with patt bound to the value of test, otherwise return 'unde‐
fined.
(condp pred expr . clauses)
Given a binary predicate, an expression and a set of clauses of the
form:
test-expr result-expr
test-expr >> result-fn
where result-fn is a unary function, if (pred test-expr expr) returns
anything other than 'undefined or 'false, the clause is a match.
If a binary clause matches, return result-expr. If a ternary clause
matches, call result-fn with the result of the predicate and return the
result.
If no clause matches and a single default expression is given after the
clauses, return it. If no default expression is given and no clause
matches, throw a no-matching-clause error.
(if-not test then)
(if-not test then else)
If test evaluates to 'false or 'undefined, evaluate and return then,
otherwise else, if supplied, else 'undefined.
(iff test . body)
Like Clojure’s when. If test evaluates to anything other than 'false
or 'undefined, evaluate body in an implicit progn.
(when-not test . body)
If test evaluates to 'false or 'undefined, evaluate body in an implicit
progn. Otherwise return 'undefined.
(not= x)
(not= x y)
(not= x y . more)
Same as (not (== ...)).
Predicate Macros
Allowed in guards, unless otherwise stated.
(tuple? x)
Return 'true if x is a tuple.
(atom? x)
Return 'true if x is an atom.
(binary? x)
Return 'true if x is a binary.
(bitstring? x)
Return 'true if x is a bitstring.
(boolean? x)
(bool? x)
Return 'true if x is a boolean.
(float? x)
Return 'true if x is a float.
(function? f)
(func? f)
Return 'true if x is a function.
(function? f n)
(func? f n)
Return 'true if f is an n-ary function.
(integer? x)
(int? x)
Return 'true if x is an integer.
(number? x)
Return 'true if x is a number.
(record? x record-tag)
(record? x record-tag size)
Return 'true if x is a tuple and its first element is record-tag. If
size is given, check that x is a record-tag record of size size.
N.B. record?/2 may yield unexpected results, due to difference between
the Erlang and LFE compilers. As such, whenever possible, prefer
record?/3."
(reference? x)
Return 'true if x is a reference.
(map? x)
Return 'true if x is a map. Return 'false on versions of Erlang with‐
out maps.
(undefined? x)
(undef? x)
Return 'true if x is the atom 'undefined.
(nil? x)
Return 'true if x is the atom 'nil or the empty list.
(true? x)
Return 'true if x is the atom 'true.
(false? x)
Return 'true if x is the atom 'false.
(falsy? x)
Return 'true if x is one of the atoms 'false and 'undefined.
(odd? x)
Return 'true if x is odd.
(even? x)
Return 'true if x is even.
(zero? x)
Return 'true if x is zero.
(pos? x)
Return 'true if x is greater than zero.
(neg? x)
Return 'true if x is less than zero.
(identical? x)
Return 'true if x is exactly equal to y.
Other Macros
(str x1, x2 ... xn)
Given arbitrary number of arguments, return a string consisting of each
of their string representations.
N.B. Because Erlang characters are represented as integers, this will
not work for chars, e.g. #\a, which will be presented in the return
value as its integer value, i.e. "97".
> (clj:str #\a "bc")
"97bc"
> (clj:str "a" "bc")
"abc"
(lazy-seq)
(lazy-seq seq)
Return a (possibly infinite) lazy sequence from a given lazy sequence
seq or a finite lazy sequence from given list seq. A lazy sequence is
treated as finite if at any iteration it produces the empty list, in‐
stead of a cons cell with data as the head and a nullary function for
the next iteration as the tail.
(conj coll . xs)
conj[oin] a value onto an existing collection. Prepend to a list, ap‐
pend to a tuple, and merge maps.
Clojure-inspired if Macro
(if test then)
(if test then else)
If test evaluates to anything other than 'false or 'undefined, return
then, otherwise else, if given, else 'undefined.
Function Composition
(comp f g)
Right to left function composition.
(comp fs x)
Compose a list of functions fs, right to left, and apply the resulting
function to x.
(comp f g x)
Equivalent to (funcall (comp f g) x).
(comp fs)
Compose a list of functions fs from right to left.
(comp)
Equivalent to #'identity/1.
Usage
The following examples assume #'1+/1 is defined:
> (defun 1+ (x) (+ x 1))
1+
> (funcall (clj:comp #'math:sin/1 #'math:asin/1) 0.5)
0.49999999999999994
> (funcall (clj:comp (list #'1+/1 #'math:sin/1 #'math:asin/1) 0.5))
1.5
Or used in another function call:
> (lists:filter (clj:comp #'not/1 #'zero?/1)
'(0 1 0 2 0 3 0 4))
(1 2 3 4)
The usage above is best when comp will be called by higher-order func‐
tions like lists:foldl/3 or lists:filter/2, etc. However, one may also
call comp in the following manner, best suited for direct usage:
> (clj:comp #'math:sin/1 #'math:asin/1 0.5)
0.49999999999999994
> (clj:comp (list #'1+/1 #'math:sin/1 #'math:asin/1) 0.5)
1.5
Partial Application
(partial f args)
(partial f arg-1)
Partially apply f to a given argument arg-1 or list of args.
Usage
> (set f (clj:partial #'+/2 1))
#Fun<clj.3.121115395>
> (funcall f 2)
3
> (set f (clj:partial #'+/3 1))
#Fun<clj.3.121115395>
> (funcall f '(2 3))
6
> (set f (clj:partial #'+/3 '(2 3)))
#Fun<clj.3.121115395>
> (funcall f 4)
9
> (set f (clj:partial #'+/4 '(2 3)))
#Fun<clj.3.121115395>
> (funcall f '(4 5))
14
Note that to partially apply a function that expects a list, you must
wrap said list into a (singleton) list.
> (set double (clj:partial #'*/2 2))
#Fun<clj.5.16146786>
> (set f (clj:partial #'lists:map/2 double))
#Fun<clj.5.16146786>
> (funcall f '((1 2 3)))
(2 4 6)
Predicate Functions
N.B. These functions may not be used in guards.
(string? data)
Return 'true if data is a flat list of printable characters.
(unicode? data)
Return 'true if data is a flat list of printable Unicode characters.
(list? data)
Return 'true if data is a list and not a string.
(set? data)
Return 'true if data is appears to be a (possibly ordered) set.
(dict? data)
Return 'true if data is a dictionary.
(proplist? lst)
Return 'true if lst is a list where proplist-kv?/1 returns 'true for
all elements in lst.
(proplist-kv? data)
Return 'true if a data is a key/value tuple or an atom.
(queue? x)
Return 'true if x is a queue.
(empty? x)
Return 'true if x is the empty list, tuple, map, dictionary, queue, or
general balanced tree.
(every? pred lst)
(all? pred lst)
Return 'true if (pred x) returns 'true for every x in lst.
(any? pred lst)
Return 'true if (pred x) returns 'true for any x in lst.
(not-any? pred lst)
Return 'false if (pred x) returns 'true for any x in lst.
(element? elem data)
Return 'true if elem is an element of data, where data is a list, set
or ordset.
Sequence Functions
(seq end)
Equivalent to (seq 1 end).
(seq start end)
Equivalent to (seq start end 1).
(seq start end step)
Return a sequence of integers, starting with start, containing the suc‐
cessive results of adding step to the previous element, until end has
been reached or password. In the latter case, end is not an element of
the sequence.
(next func)
Equivalent to (next func 1 1).
(next func start)
Equivalent to (next func start 1).
(next func start step)
Return a nullary function that returns a cons cell with start as the
head and a nullary function, (next func (funcall func start step) step)
as the tail. The result can be treated as a (possibly infinite) lazy
list, which only computes subsequent values as needed.
(lazy-seq seq)
Return a lazy sequence (possibly infinite) from given lazy sequence seq
or finite lazy sequence from given list seq. Lazy sequence is treated
as finite if at any iteration it produces empty list instead of data as
its head and nullary function for next iteration as its tail.
(cycle lst)
Return a lazy infinite sequence with all elements from a given list lst
or another lazy sequence cycled.
See next/3 for details on the structure.
(range)
Equivalent to (range 1 1).
(range start)
Equivalent to (range start 1).
(range start step)
Return a lazy list of integers, starting with start and increasing by
step. Equivalent to (next #'+/2 start step). See also: next/3.
(drop n lst)
(drop ’all lst)
Return a list of all but the first n elements in lst. If n is the atom
all, return the empty list.
(take n lst)
(take ’all lst)
Given a (possibly lazy) list lst, return a list of the first n elements
of lst, or all elements if there are fewer than n. If n is the atom
all and lst is a “normal” list, return lst.
(split-at n lst)
Return a tuple of `#(,(take n lst) ,(drop n lst)).
(partition n lst)
Equivalent to (partition n n lst).
(partition n step lst)
Equivalent to (partition n step () lst).
(partition n step pad lst)
Return a list of lists of n items each, at offsets step apart. Use the
elements of pad as necessary to complete the last partition up to n el‐
ements. In case there are not enough padding elements, return a parti‐
tion with less than n items.
(partition-all n lst)
Equivalent to (partition-all n n lst).
(partition-all n step lst)
Return a list of lists like partition/3, possibly including partitions
with fewer than n elements at the end.
(interleave list-1 list-2)
Return a list of the first element of each list, then the second, etc.
(get-in data keys)
Equivalent to (get-in data keys 'undefined).
(get-in data keys not-found)
Return the value in a nested associative structure, where keys is a
list of keys or list indices. Return the atom not-found if the key is
not present or index is out of bounds, or the not-found value.
(reduce func (cons head tail))
Equivalent to (reduce func head tail).
(reduce func acc lst)
Equivalent to (lists:foldl func acc lst).
(repeat x)
Return a lazy infinite sequence of xs.
See next/3 for details on the structure.
(repeat n f)
Given a nullary function f, return a list of n applications of f.
(repeat n x)
Given a term x, return a list of n copies of x.
Other Functions
(identity x)
Identity function.
(constantly x)
Return a unary function that returns x. N.B. This is like Haskell’s
const rather than Clojure’s constantly.
(inc x)
Increment x by 1.
(dec x)
Decrement x by 1.
AUTHORS
Tim Dysinger, Duncan McGreggor, Eric Bailey.
2015-2016 lfe_clj(3)