Skip to content

Instantly share code, notes, and snippets.

@swannodette
Created December 14, 2010 07:09
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 swannodette/740099 to your computer and use it in GitHub Desktop.
Save swannodette/740099 to your computer and use it in GitHub Desktop.
(definterface IComplex
(a [^java.lang.Long x ^java.lang.Double y])
(a [^java.lang.Double x ^java.lang.Long y]))
(deftype Complex []
IComplex
(a [this ^java.lang.Long x ^java.lang.Double y] "a")
(a [this ^java.lang.Double x ^java.lang.Long y] "b"))
(defn a [x y]
(.a (Complex.) x y))
(a 1 1.5)
(a 1.5 1)
;; ~ 1s
(dotimes [_ 10]
(time
(dotimes [_ 1e6]
(a 1 1.5))))
(defprotocol IComplex
(a [this x]))
(declare complex?)
(deftype Complex []
IComplex
(a [this x]
(cond
(complex? x) "ca"
(float? x) "cb")))
(defn complex? [x]
(instance? Complex x))
(extend-type java.lang.Double
IComplex
(a [this x]
(cond
(complex? x) "fa"
(float? x) "fb")))
;; 13ms
(dotimes [_ 10]
(time
(dotimes [_ 1e6]
(a 1.5 (Complex.))
(a (Complex.) 1.5))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment