Skip to content

Instantly share code, notes, and snippets.

@hugoduncan
Created April 11, 2021 13:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hugoduncan/5fb1afd91e9da89a19a2987a52150c23 to your computer and use it in GitHub Desktop.
Save hugoduncan/5fb1afd91e9da89a19a2987a52150c23 to your computer and use it in GitHub Desktop.
Predicate for testing whether a value is primitive in clojure
(definterface IPrimitive
(is_primitive [^boolean x])
(is_primitive [^float x])
(is_primitive [^double x])
(is_primitive [^int x])
(is_primitive [^long x])
(is_primitive [^Object x]))
(deftype IsPrimitive []
IPrimitive
(is_primitive [_ ^float x] true)
(is_primitive [_ ^double x] true)
(is_primitive [_ ^int x] true)
(is_primitive [_ ^long x] true)
(is_primitive [_ ^boolean x] true)
(is_primitive [_ ^Object x] false))
(def ^IsPrimitive is-primitive (IsPrimitive.))
(defmacro primitive?
"Predicate for x being of primitive type."
[x]
`(.is_primitive is-primitive ~x))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment