mal: rename macro? to _macro?. Also rename bool-and in lib/equality.mal.
authorNicolas Boulenguez <nicolas.boulenguez@free.fr>
Thu, 30 May 2019 17:27:45 +0000 (19:27 +0200)
committerNicolas Boulenguez <nicolas.boulenguez@free.fr>
Tue, 9 Jul 2019 12:05:29 +0000 (14:05 +0200)
lib/equality.mal
mal/core.mal
mal/step8_macros.mal
mal/step9_try.mal
mal/stepA_mal.mal
tests/lib/equality.mal

index 8dabb13..5f4adaa 100644 (file)
 (def! scalar-equal? =)
 
 ;; A faster `and` macro which doesn't use `=` internally.
-(defmacro! and2                        ; boolean
+(defmacro! bool-and                    ; boolean
   (fn* [& xs]                          ; interpreted as logical values
     (if (empty? xs)
       true
-      `(if ~(first xs) (and2 ~@(rest xs)) false))))
-(defmacro! or2                         ; boolean
+      `(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 (or2 ~@(rest xs))))))
+      `(if ~(first xs) true (bool-or ~@(rest xs))))))
 
 (def! starts-with?
   (fn* [a b]
-    (or2 (empty? a)
-         (and2 (mal-equal? (first a) (first b))
-               (starts-with? (rest a) (rest 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]
-    (or2 (empty? map-keys)
-         (let* [key (first map-keys)]
-           (and2 (contains? b key)
-                 (mal-equal? (get a key) (get b key))
-                 (hash-map-vals-equal? a b (rest 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?
     (cond
 
       (sequential? a)
-      (and2 (sequential? b)
-            (scalar-equal? (count a) (count b))
-            (starts-with? a b))
+      (bool-and (sequential? b)
+                (scalar-equal? (count a) (count b))
+                (starts-with? a b))
 
       (map? a)
       (let* [keys-a (keys a)]
-        (and2 (map? b)
-              (scalar-equal? (count keys-a) (count (keys b)))
-              (hash-map-vals-equal? a b 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*
-      (and2 (= {:a 1} {:a 1})
-            (not (= {:a 1} {:a 1 :b 2})))
+      (bool-and (= {:a 1} {:a 1})
+                (not (= {:a 1} {:a 1 :b 2})))
       (catch* _ false))))
 
 (def! sequence-equality-correct?
   (fn* []
     (try*
-      (and2 (= [:a :b] (list :a :b))
-            (not (= [:a :b] [:a :b :c])))
+      (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 (and2 (hash-map-equality-correct?) (sequence-equality-correct?)))
+(if (not (bool-and (hash-map-equality-correct?)
+                   (sequence-equality-correct?)))
   (do
     (def! = mal-equal?)
     (println "equality.mal: Replaced = with pure mal implementation")))
index 2373064..60c5e97 100644 (file)
@@ -3,7 +3,7 @@
     (not (get (meta x) "ismacro"))
     false)))
 
-(def! macro? (fn* [x]
+(def! _macro? (fn* [x]
   (if (fn? x)
     (if (get (meta x) "ismacro")
       true
@@ -23,7 +23,7 @@
    ['keyword keyword]
    ['keyword? keyword?]
    ['fn? _fn?]
-   ['macro? macro?]
+   ['macro? _macro?]
 
    ['pr-str pr-str]
    ['str str]
index c909943..80c1cf1 100644 (file)
@@ -29,7 +29,7 @@
     (let* [a0 (first ast)]
       (if (symbol? a0)
         (if (env-find env a0)
-          (macro? (env-get env a0))))))))
+          (_macro? (env-get env a0))))))))
 
 (def! MACROEXPAND (fn* [ast env]
   (if (is-macro-call ast env)
index a907adc..ad4d763 100644 (file)
@@ -29,7 +29,7 @@
     (let* [a0 (first ast)]
       (if (symbol? a0)
         (if (env-find env a0)
-          (macro? (env-get env a0))))))))
+          (_macro? (env-get env a0))))))))
 
 (def! MACROEXPAND (fn* [ast env]
   (if (is-macro-call ast env)
index 7f498cb..e053802 100644 (file)
@@ -29,7 +29,7 @@
     (let* [a0 (first ast)]
       (if (symbol? a0)
         (if (env-find env a0)
-          (macro? (env-get env a0))))))))
+          (_macro? (env-get env a0))))))))
 
 (def! MACROEXPAND (fn* [ast env]
   (if (is-macro-call ast env)
index 78a0b5d..52c42b1 100644 (file)
@@ -4,42 +4,42 @@
 (load-file "../lib/equality.mal")
 ;=>nil
 
-;; Testing and2
-(and2)
+;; Testing bool-and
+(bool-and)
 ;=>true
-(and2 true)
+(bool-and true)
 ;=>true
-(and2 false)
+(bool-and false)
 ;=>false
-(and2 nil)
+(bool-and nil)
 ;=>false
-(and2 1)
+(bool-and 1)
 ;=>true
-(and2 1 2)
+(bool-and 1 2)
 ;=>true
-(and2 nil (nth () 1))
+(bool-and nil (nth () 1))
 ;=>false
 
-;; Testing or2
-(or2)
+;; Testing bool-or
+(bool-or)
 ;=>false
-(or2 true)
+(bool-or true)
 ;=>true
-(or2 false)
+(bool-or false)
 ;=>false
-(or2 nil)
+(bool-or nil)
 ;=>false
-(or2 1)
+(bool-or 1)
 ;=>true
-(or2 1 (nth () 1))
+(bool-or 1 (nth () 1))
 ;=>true
-(or2 1 2)
+(bool-or 1 2)
 ;=>true
-(or2 false nil)
+(bool-or false nil)
 ;=>false
 
 ;; Breaking equality.
-(def! = (fn* [a b] (and2 (orig= a b) (cond (list? a) (list? b) (vector? a) (vector? b) true true))))
+(def! = (fn* [a b] (bool-and (orig= a b) (cond (list? a) (list? b) (vector? a) (vector? b) true true))))
 (= [] ())
 ;=>false