Search code examples
clojurehashmapbigdecimal

compare Clojure maps with bigdecimal and integers values


In my REPL :

(== 1 1M)
;;=>true
(= {:a 1 :b 2} {:a 1 :b 2})
;;=>true
(= {:a 1 :b 2} {:a 1 :b 3})
;;=>false
(= {:a 1M :b 2M} {:a 1 :b 2})
;;=>false

How can I compare these maps so that the result of the last example would return true ?


Solution

  • You want to make sure that:

    1) The maps have the same keys.

    2) every value for a key has an equivalent (==) value in both maps.

    Here's my first thought, I'm sure it could be made more succint:

    (defn number-equivalent
      [m1 m2]
      (let [k1 (keys m1)]
        (and (= k1 (keys m2))
              (every? true?
                      (for [k k1]
                        (== (m1 k)
                            (m2 k)))))))