-
-
Save tomjack/ad9cad85dc72c41dd52b 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
(defprotocol Arithmetic | |
"Basic arithmetic operations" | |
(add [a b] "Addition") | |
(sub [a b] "Subtraction") | |
(mul [a b] "Multiplication") | |
(div [a b] "Division")) | |
;; Number is the class java.lang.Number | |
(extend Number Arithmetic | |
{:add +, :sub -, :mul *, :div /}) | |
(defrecord Complex [#^double real #^double imaginary] | |
Arithmetic | |
(add [a b] (Complex. (+ (:real a) (:real b)) | |
(+ (:imaginary a) (:imaginary b)))) | |
(sub [a b] (Complex. (- (:real a) (:real b)) | |
(- (:imaginary a) (:imaginary b)))) | |
(mul [a b] (Complex. (- (* (:real a) (:real b)) | |
(* (:imaginary a) (:imaginary b))) | |
(+ (* (:real a) (:imaginary b)) | |
(* (:imaginary a) (:real b)))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment