Created
December 14, 2010 07:09
-
-
Save swannodette/740099 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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