Skip to content

Instantly share code, notes, and snippets.

@aamedina
Last active December 28, 2015 22:09
Show Gist options
  • Select an option

  • Save aamedina/7569497 to your computer and use it in GitHub Desktop.

Select an option

Save aamedina/7569497 to your computer and use it in GitHub Desktop.
(defn unevaluated?
[expr]
(or (symbol? expr)
(and (seq? expr)
(not= (first expr) `quote))))
(defn literal?
[x]
(and (not (unevaluated? x))
(or (not (or (vector? x) (map? x)))
(every? literal? x))))
(defmacro some-identical?
[v coll]
(when (and (seq coll) (every? literal? (conj coll v)))
`(or ~@(map (fn [[val# item#]] `(identical? ~val# ~item#))
(->> (interleave (repeat (count coll) v) coll)
(partition-all 2))))))
(do (time (dotimes [i 100000]
(contains? #{:s :vs :cs :cq :cqs :oc} :cqs)))
(time (dotimes [i 100000]
(some #{:s :vs :cs :cq :cqs :oc} [:cqs])))
(time (dotimes [i 100000]
(some (partial identical? :cqs) #{:s :vs :cs :cq :cqs :oc})))
(time (dotimes [i 100000]
(some-identical? :cqs #{:s :vs :cs :cq :cqs :oc}))))
"Elapsed time: 8.983 msecs"
"Elapsed time: 12.917 msecs"
"Elapsed time: 23.419 msecs"
"Elapsed time: 2.178 msecs"
(macroexpand '(some-identical? :cqs #{:s :vs :cs :cq :cqs :oc}))
(let* [or__3945__auto__ (clojure.core/identical? :cqs :cqs)]
(if or__3945__auto__ or__3945__auto__
(clojure.core/or (clojure.core/identical? :cqs :vs)
(clojure.core/identical? :cqs :oc)
(clojure.core/identical? :cqs :cq)
(clojure.core/identical? :cqs :cs)
(clojure.core/identical? :cqs :s))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment