README: typo.
[jackhill/mal.git] / tests / stepA_more.mal
CommitLineData
31690700
JM
1;;
2;; Testing try*/catch*
3
4(try* (abc 1 2) (catch* exc (prn exc))))
5; "'abc' not found"
6;=>nil
7
8;;;TODO: fix so long lines don't trigger ANSI escape codes
9;;;(try* (throw {"data" "foo"}) (catch* exc (do (prn "exc is:" exc) 7)))
10;;;; "exc is:" {"data" "foo"}
11;;;;=>7
12
13(try* (throw "my exception") (catch* exc (do (prn "exc:" exc) 7)))
14; "exc:" "my exception"
15;=>7
16
17
18;;
19;; Testing builtin functions
20
21(symbol? 'abc)
22;=>true
23(symbol? "abc")
24;=>false
25
26(nil? nil)
27;=>true
28(nil? true)
29;=>false
30
31(true? true)
32;=>true
33(true? false)
34;=>false
35(true? true?)
36;=>false
37
38(false? false)
39;=>true
40(false? true)
41;=>false
42
43(sequential? (list 1 2 3))
44;=>true
45(sequential? [15])
46;=>true
47(sequential? sequential?)
48;=>false
49(sequential? nil)
50;=>false
51(sequential? "abc")
52;=>false
53
54
55;; Testing apply function
56(apply + (list 2 3))
57;=>5
58(apply + 4 (list 5))
59;=>9
60(apply prn (list 1 2 "3" (list)))
61; 1 2 "3" ()
62;=>nil
63
64
65;; Testing map function
66(def! nums (list 1 2 3))
67(def! double (fn* (a) (* 2 a)))
68(double 3)
69;=>6
70(map double nums)
71;=>(2 4 6)
72
73
31690700
JM
74;; Testing conj function
75(conj (list) 1)
76;=>(1)
77(conj (list 1) 2)
9528bb14 78;=>(2 1)
31690700 79(conj (list 2 3) 4)
9528bb14 80;=>(4 2 3)
31690700 81(conj (list 2 3) 4 5 6)
9528bb14 82;=>(6 5 4 2 3)
31690700 83(conj (list 1) (list 2 3))
9528bb14
JM
84;=>((2 3) 1)
85
86(conj [] 1)
87;=>[1]
88(conj [1] 2)
89;=>[1 2]
90(conj [2 3] 4)
91;=>[2 3 4]
92(conj [2 3] 4 5 6)
93;=>[2 3 4 5 6]
94(conj [1] [2 3])
95;=>[1 [2 3]]
31690700 96
31690700
JM
97
98
99;;
100;; Testing hash-maps
101(hash-map "a" 1)
102;=>{"a" 1}
103
104{"a" 1}
105;=>{"a" 1}
106
107(assoc {} "a" 1)
108;=>{"a" 1}
109
110(def! hm1 (hash-map))
111;=>{}
112
113(map? hm1)
114;=>true
115(map? 1)
116;=>false
117(map? [])
118;=>false
119
0027e8fe
JM
120(get nil "a")
121;=>nil
122
31690700
JM
123(get hm1 "a")
124;=>nil
125
126(contains? hm1 "a")
127;=>false
128
129(def! hm2 (assoc hm1 "a" 1))
130;=>{"a" 1}
131
132(get hm1 "a")
133;=>nil
134
135(contains? hm1 "a")
136;=>false
137
138(get hm2 "a")
139;=>1
140
141(contains? hm2 "a")
142;=>true
143
144(keys hm2)
145;=>("a")
146
147(vals hm2)
148;=>(1)
149
9528bb14
JM
150(count (keys (assoc hm2 "b" 2 "c" 3)))
151;=>3
152
31690700
JM
153(def! hm3 (assoc hm2 "b" 2))
154(count (keys hm3))
155;=>2
156(count (vals hm3))
157;=>2
158
159(dissoc hm3 "a")
160;=>{"b" 2}
161
162(dissoc hm3 "a" "b")
163;=>{}
164
5ce65382
JM
165(dissoc hm3 "a" "b" "c")
166;=>{}
167
31690700
JM
168(count (keys hm3))
169;=>2
170
171
172;;
173;; Testing metadata
174(meta [1 2 3])
175;=>nil
176
a34b0200
JM
177(meta (fn* (a) a))
178;=>nil
179
0027e8fe
JM
180(meta +)
181;=>nil
182
31690700
JM
183(with-meta [1 2 3] {"a" 1})
184;=>[1 2 3]
185
186(meta (with-meta [1 2 3] {"a" 1}))
187;=>{"a" 1}
188
a34b0200
JM
189(meta (with-meta [1 2 3] "abc"))
190;=>"abc"
191
b079f510
JM
192(meta (with-meta (list 1 2 3) {"a" 1}))
193;=>{"a" 1}
194
195(meta (with-meta {"abc" 123} {"a" 1}))
196;=>{"a" 1}
197
198;;; Not actually supported by Clojure
199;;;(meta (with-meta (atom 7) {"a" 1}))
200;;;;=>{"a" 1}
201
a34b0200 202(def! l-wm (with-meta [4 5 6] {"b" 2}))
31690700 203;=>[4 5 6]
a34b0200
JM
204(meta l-wm)
205;=>{"b" 2}
206
207(meta (with-meta l-wm {"new_meta" 123}))
208;=>{"new_meta" 123}
209(meta l-wm)
210;=>{"b" 2}
211
31690700
JM
212
213(def! f-wm (with-meta (fn* [a] (+ 1 a)) {"abc" 1}))
214(meta f-wm)
215;=>{"abc" 1}
216
a34b0200
JM
217(meta (with-meta f-wm {"new_meta" 123}))
218;=>{"new_meta" 123}
17ae845e
JM
219(meta f-wm)
220;=>{"abc" 1}
221
222
b079f510 223(def! f-wm2 ^{"abc" 1} (fn* [a] (+ 1 a)))
5ce65382 224(meta f-wm2)
b079f510 225;=>{"abc" 1}
5ce65382 226
a34b0200
JM
227;;
228;; Make sure closures and metadata co-exist
229(def! gen-plusX (fn* (x) (with-meta (fn* (b) (+ x b)) {"meta" 1})))
230(def! plus7 (gen-plusX 7))
231(def! plus8 (gen-plusX 8))
232(plus7 8)
233;=>15
234(meta plus7)
235;=>{"meta" 1}
236(meta plus8)
237;=>{"meta" 1}
238(meta (with-meta plus7 {"meta" 2}))
239;=>{"meta" 2}
240(meta plus8)
241;=>{"meta" 1}
242
31690700
JM
243
244;;
245;; Testing atoms
246
247(def! inc3 (fn* (a) (+ 3 a)))
248
249(def! a (atom 2))
250;=>(atom 2)
251
252;;;(type a)
253;;;;=>"atom"
254
255(deref a)
256;=>2
257
258@a
259;=>2
260
261(reset! a 3)
262;=>3
263
264@a
265;=>3
266
267(swap! a inc3)
268;=>6
269
270@a
271;=>6
272
273(swap! a (fn* (a) a))
274;=>6
275
276(swap! a (fn* (a) (* 2 a)))
277;=>12
278
17ae845e
JM
279(swap! a (fn* (a b) (* a b)) 10)
280;=>120
281
5ce65382 282(swap! a + 3)
17ae845e 283;=>123
31690700 284
0027e8fe
JM
285;; Testing swap!/closure interaction
286(def! inc-it (fn* (a) (+ 1 a)))
287(def! atm (atom 7))
288(def! f (fn* [] (swap! atm inc-it)))
289(f)
290;=>8
291(f)
292;=>9
293
31690700
JM
294;;
295;; Testing read-str and eval
9528bb14
JM
296(read-string "(1 2 (3 4) nil)")
297;=>(1 2 (3 4) nil)
31690700
JM
298
299(eval (read-string "(+ 4 5)"))
300;=>9
301
302;;
303;; Testing readline
304(readline "mal-user> ")
305"hello"
306;=>"\"hello\""
307
308;;
309;; Testing macros cond and or
310(cond 1 2 3 4)
311;=>2
312(cond false 2 3 4)
313;=>4
314(cond false 2 false 4)
315;=>nil
316
317(or)
318;=>nil
319(or 1)
320;=>1
321(or 1 2)
322;=>1
323(or nil 2)
324;=>2