forked from kanaka/mal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathequality.mal
77 lines (65 loc) · 2.49 KB
/
equality.mal
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
;; equality.mal
;; This file checks whether the `=` function correctly implements equality of
;; hash-maps and sequences (lists and vectors). If not, it redefines the `=`
;; function with a pure mal (recursive) implementation that only relies on the
;; native original `=` function for comparing scalars (integers, booleans,
;; symbols, strings, keywords, atoms, nil).
;; Save the original (native) `=` as scalar-equal?
(def! scalar-equal? =)
;; A faster `and` macro which doesn't use `=` internally.
(defmacro! bool-and ; boolean
(fn* [& xs] ; interpreted as logical values
(if (empty? xs)
true
`(if ~(first xs) (bool-and ~@(rest xs)) false))))
(defmacro! bool-or ; boolean
(fn* [& xs] ; interpreted as logical values
(if (empty? xs)
false
`(if ~(first xs) true (bool-or ~@(rest xs))))))
(def! starts-with?
(fn* [a b]
(bool-or (empty? a)
(bool-and (mal-equal? (first a) (first b))
(starts-with? (rest a) (rest b))))))
(def! hash-map-vals-equal?
(fn* [a b map-keys]
(bool-or (empty? map-keys)
(let* [key (first map-keys)]
(bool-and (contains? b key)
(mal-equal? (get a key) (get b key))
(hash-map-vals-equal? a b (rest map-keys)))))))
;; This implements = in pure mal (using only scalar-equal? as native impl)
(def! mal-equal?
(fn* [a b]
(cond
(sequential? a)
(bool-and (sequential? b)
(scalar-equal? (count a) (count b))
(starts-with? a b))
(map? a)
(let* [keys-a (keys a)]
(bool-and (map? b)
(scalar-equal? (count keys-a) (count (keys b)))
(hash-map-vals-equal? a b keys-a)))
true
(scalar-equal? a b))))
(def! hash-map-equality-correct?
(fn* []
(try*
(bool-and (= {:a 1} {:a 1})
(not (= {:a 1} {:a 1 :b 2})))
(catch* _ false))))
(def! sequence-equality-correct?
(fn* []
(try*
(bool-and (= [:a :b] (list :a :b))
(not (= [:a :b] [:a :b :c])))
(catch* _ false))))
;; If the native `=` implementation doesn't support sequences or hash-maps
;; correctly, replace it with the pure mal implementation
(if (not (bool-and (hash-map-equality-correct?)
(sequence-equality-correct?)))
(do
(def! = mal-equal?)
(println "equality.mal: Replaced = with pure mal implementation")))