forked from clojerl/clojerl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcase.clje
85 lines (70 loc) · 1.89 KB
/
case.clje
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
(ns examples.case)
(def assert-with-tags
(fn* [result & tags]
(if result
nil
(erlang/error #erl[:failed-assertion tags]))))
(let* [x :b
y (case* x
:a 1
:b 2
:c 3)
z (case* y
x :one
#erl [:default :value])]
(assert-with-tags (erlang/=:= x :b) :x)
(assert-with-tags (erlang/=:= y 2) :y)
(assert-with-tags (erlang/=:= z #erl[:default :value]) :z))
(def case-match-ignore
(fn* [x]
(case* x
#erl[_ y _] y)))
;; First and third don't have to be equal
(let* [2 (case-match-ignore #erl[1 2 3])])
(def case-match
(fn* [x]
(case* x
#erl[a y a] y)))
;; First and third have to be equal
(let* [2 (case-match #erl[1 2 1])])
;; [#684] Pattern matching ignores existing bound symbol
(def equal?
(fn* [x y]
(case* x
y :equal
_ :not_equal)))
(assert-with-tags (erlang/=:= (equal? 1 1) :equal))
(assert-with-tags (erlang/=:= (equal? 1 2) :not_equal))
(def multiple-locals-not-ordered
(fn* [t & a]
(let* [r 5]
(loop* [n 10]
(case* n
r #erl[:five n]
_ (recur (erlang/- n 1))
0 :zero)))))
(assert-with-tags (erlang/=:= (multiple-locals-not-ordered 1 1)
#erl[:five 5]))
(def two-bindings-same-name-in-let
(fn* []
(let* [x 1
x 2
y 1]
(case* y
x :x-is-one
:x-is-two))))
(assert-with-tags
(erlang/=:= (two-bindings-same-name-in-let)
:x-is-two))
(def three-bindings-same-name-in-let
(fn* []
(let* [x 1
x 2
x 3
y 1]
(case* y
x :x-is-one
:x-is-three))))
(assert-with-tags
(erlang/=:= (three-bindings-same-name-in-let)
:x-is-three))