Skip to content

Instantly share code, notes, and snippets.

@tomjack

tomjack/test.clj Secret

Created May 26, 2010 22:53
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 tomjack/ad9cad85dc72c41dd52b to your computer and use it in GitHub Desktop.
Save tomjack/ad9cad85dc72c41dd52b to your computer and use it in GitHub Desktop.
(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