Skip to content

Instantly share code, notes, and snippets.

@leikind
Created March 26, 2014 21:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leikind/9794053 to your computer and use it in GitHub Desktop.
Save leikind/9794053 to your computer and use it in GitHub Desktop.
defprotocol/defrecord example
(defprotocol Inspect (inspect [c]))
(defprotocol Maths
(plus [this another])
(multiply [this another]))
(defrecord Rational [nom den]
Inspect
(inspect [this] (str (:nom this) "/" (:den this) ))
Maths
(plus [this another]
(Rational.
(+
(*
(:nom this)
(:den another))
(*
(:nom another)
(:den this)))
(*
(:den this)
(:den another ))))
(multiply [this another]
(Rational.
(*
(:nom this)
(:nom another))
(*
(:den this)
(:den another)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment