Commit | Line | Data |
---|---|---|
31690700 JM |
1 | (def! inc (fn* (a) (+ a 1))) |
2 | ||
3 | (def! dec (fn* (a) (- a 1))) | |
4 | ||
5 | (def! zero? (fn* (n) (= 0 n))) | |
6 | ||
7 | (def! reduce | |
8 | (fn* (f init xs) | |
9 | (if (> (count xs) 0) | |
10 | (reduce f (f init (first xs)) (rest xs)) | |
11 | init))) | |
12 | ||
13 | (def! identity (fn* (x) x)) | |
14 | ||
15 | (def! every? | |
16 | (fn* (pred xs) | |
17 | (if (> (count xs) 0) | |
18 | (if (pred (first xs)) | |
19 | (every? pred (rest xs)) | |
20 | false) | |
21 | true))) | |
22 | ||
23 | (def! not (fn* (x) (if x false true))) | |
24 | ||
25 | (def! some | |
26 | (fn* (pred xs) | |
27 | (if (> (count xs) 0) | |
28 | (let* (res (pred (first xs))) | |
29 | (if (pred (first xs)) | |
30 | res | |
31 | (some pred (rest xs)))) | |
32 | nil))) | |
33 | ||
34 | (defmacro! and | |
35 | (fn* (& xs) | |
36 | (if (empty? xs) | |
37 | true | |
38 | (if (= 1 (count xs)) | |
39 | (first xs) | |
2357142e DM |
40 | (let* (condvar (gensym)) |
41 | `(let* (~condvar ~(first xs)) | |
42 | (if ~condvar (and ~@(rest xs)) ~condvar))))))) | |
31690700 JM |
43 | |
44 | (defmacro! or | |
45 | (fn* (& xs) | |
46 | (if (empty? xs) | |
47 | nil | |
48 | (if (= 1 (count xs)) | |
49 | (first xs) | |
2357142e DM |
50 | (let* (condvar (gensym)) |
51 | `(let* (~condvar ~(first xs)) | |
52 | (if ~condvar ~condvar (or ~@(rest xs))))))))) | |
31690700 JM |
53 | |
54 | (defmacro! cond | |
55 | (fn* (& clauses) | |
56 | (if (> (count clauses) 0) | |
57 | (list 'if (first clauses) | |
58 | (if (> (count clauses) 1) | |
59 | (nth clauses 1) | |
60 | (throw "cond requires an even number of forms")) | |
61 | (cons 'cond (rest (rest clauses))))))) | |
62 | ||
63 | (defmacro! -> | |
64 | (fn* (x & xs) | |
65 | (if (empty? xs) | |
66 | x | |
67 | (let* (form (first xs) | |
68 | more (rest xs)) | |
69 | (if (empty? more) | |
70 | (if (list? form) | |
71 | `(~(first form) ~x ~@(rest form)) | |
72 | (list form x)) | |
73 | `(-> (-> ~x ~form) ~@more)))))) | |
74 | ||
75 | (defmacro! ->> | |
76 | (fn* (x & xs) | |
77 | (if (empty? xs) | |
78 | x | |
79 | (let* (form (first xs) | |
80 | more (rest xs)) | |
81 | (if (empty? more) | |
82 | (if (list? form) | |
83 | `(~(first form) ~@(rest form) ~x) | |
84 | (list form x)) | |
85 | `(->> (->> ~x ~form) ~@more)))))) | |
db4c329a | 86 |