Skip to content

Commit 11845eb

Browse files
committed
Add f.u.fn/=to, exposing c.l.Util/equivPred
1 parent ecbdaf1 commit 11845eb

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/flatland/useful/fn.clj

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,3 +176,19 @@
176176
(partial apply f))
177177

178178
(def ap "A shorthand version of applied" applied)
179+
180+
(defn =to
181+
"Produces an equality predicate from a single object. ((=to x) y) is
182+
the same as (= x y), but if the returned function will be called many
183+
times it may be more efficient than repeated calls to =, because =to
184+
can short-circuit many irrelevant code paths based on knowing the type
185+
of x.
186+
187+
Just a wrapper for clojure.lang.Util/equivPred."
188+
[x]
189+
(if (keyword? x) ;; a case myssteriously not covered in equivPred
190+
(fn [y]
191+
(identical? x y))
192+
(let [p (clojure.lang.Util/equivPred x)]
193+
(fn [y]
194+
(.equiv p x y)))))

test/flatland/useful/fn_test.clj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,9 @@
9090
by-square (key-comparator :ascending square)]
9191
(is (= (sort-by square [-9 -5 1 -2])
9292
(sort by-square [-9 -5 1 -2])))))
93+
94+
(deftest test-=to
95+
(let [objs [1 :x "x" [5] nil (Object.) {:x 1} '((a b c) d)]]
96+
(doseq [x objs
97+
y objs]
98+
(is (= (= x y) ((=to x) y))))))

0 commit comments

Comments
 (0)