-
Notifications
You must be signed in to change notification settings - Fork 0
/
stdlib.relisp
136 lines (114 loc) · 2.9 KB
/
stdlib.relisp
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
(def! *HOST* "rescript/javascript")
(defmacro! unless (fn* (pred a b) `(if ~pred ~b ~a)))
(defmacro! not (fn* [c] `(if ~c false true)))
(def! swap!
(fn* (a f & xs)
(reset! a (apply f (deref a) xs))))
(def! inc (fn* (a) (+ a 1)))
(def! dec (fn* (a) (- a 1)))
(def! zero? (fn* (n) (= 0 n)))
(def! reduce
(fn* (f acc xs)
(if (> (count xs) 0)
(reduce f (f acc (first xs)) (rest xs))
acc)))
(def! identity (fn* (x) x))
(def! every?
(fn* (pred xs)
(if (> (count xs) 0)
(if (pred (first xs))
(every? pred (rest xs))
false)
true)))
(def! not (fn* (x) (if x false true)))
(def! some
(fn* (pred xs)
(if (> (count xs) 0)
(let* (res (pred (first xs)))
(if (pred (first xs))
res
(some pred (rest xs))))
nil)))
(defmacro! and
(fn* (& xs)
(if (empty? xs)
true
(if (= 1 (count xs))
(first xs)
`(let* (and_FIXME ~(first xs))
(if and_FIXME (and ~@(rest xs)) and_FIXME))))))
(defmacro! or
(fn* (& xs)
(if (empty? xs)
nil
(if (= 1 (count xs))
(first xs)
`(let* (or_FIXME ~(first xs))
(if or_FIXME or_FIXME (or ~@(rest xs))))))))
(defmacro! cond
(fn* (& clauses)
(if (> (count clauses) 0)
(list 'if (first clauses)
(if (> (count clauses) 1)
(nth clauses 1)
(error "cond requires an even number of forms"))
(cons 'cond (rest (rest clauses)))))))
(defmacro! ->
(fn* (x & xs)
(if (empty? xs)
x
(let* (form (first xs)
more (rest xs))
(if (empty? more)
(if (list? form)
`(~(first form) ~x ~@(rest form))
(list form x))
`(-> (-> ~x ~form) ~@more))))))
(defmacro! ->>
(fn* (x & xs)
(if (empty? xs)
x
(let* (form (first xs)
more (rest xs))
(if (empty? more)
(if (list? form)
`(~(first form) ~@(rest form) ~x)
(list form x))
`(->> (->> ~x ~form) ~@more))))))
(defmacro! def
(fn* (name defination)
`(def! ~name ~defination)))
(defmacro! defn
(fn* (name args & body)
`(def! ~name (fn* ~args (do ~@body)))))
(defmacro! print-header
(fn* ()
`(do
(print "╭────────────┬─────────────╮\n│ ReLisp │ v0.1.0 Beta │\n╰────────────┴─────────────╯")
nil)))
(def! log/info
(fn* (s)
(print
(str "INFO: " s))))
(def! log/error
(fn* (s)
(print
(str "ERROR: " s))))
(def! log/warn
(fn* (s)
(print
(str "WARN: " s))))
(def! log/verbose
(fn* (s)
(print
(str "VERBOSE: " s))))
(defmacro! from
(fn* [start times body]
`(if (> ~start ~times)
nil
(do
(~body ~start)
(from (+ ~start 1) ~times ~body)))))
(defmacro! lambda
(fn* [args body]
`(fn* ~args ~body)))