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