Created
September 21, 2012 15:28
-
-
Save bnyeggen/3762181 to your computer and use it in GitHub Desktop.
Functions can implement protocols too
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 TwoPersonFn | |
(operate [this a b])) | |
(defn shorter? [a b]) | |
(defn marry! [a b]) | |
(defn same-age? [a b]) | |
(extend-type (class shorter?) TwoPersonFn | |
(operate [this a b] (this a b))) | |
(extend-type (class marry!) TwoPersonFn | |
(operate [this a b] (this a b))) | |
(extend-type (class same-age?) TwoPersonFn | |
(operate [this a b] (this a b))) | |
(satisfies? TwoPersonFn shorter?) ; => true | |
(satisfies? TwoPersonFn marry!) ; => true | |
(satisfies? TwoPersonFn same-age?) ; => true | |
(operate shorter? {:height 1} {:height 2}) ;or whatever |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment