Created
September 3, 2019 16:39
-
-
Save mpjdem/8cb5607e05696fc31c5c6a0fb46cd71a to your computer and use it in GitHub Desktop.
'Strategy' design pattern in Clojure
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
(def users [{:name "John Doe" :subscription 0} | |
{:name "Jane Doe" :subscription 1} | |
{:name "John Dur" :subscription 1} | |
{:name "Sara Der" :subscription 1}]) | |
(defn alphabetic-sort [u1 u2] | |
(cond | |
(= (:subscription u1) | |
(:subscription u2)) (neg? (compare (:name u1) | |
(:name u2))) | |
(:subscription u1) true | |
:else false)) | |
(defn reverse-sort [u1 u2] | |
(cond | |
(= (:subscription u1) | |
(:subscription u2)) (pos? (compare (:name u1) | |
(:name u2))) | |
(:subscription u1) true | |
:else false)) | |
(sort alphabetic-sort users) | |
(sort reverse-sort users) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
As discussed here:
https://www.mpj-analytics.co.uk/2019/09/04/design-patterns-r-strategy/
Inspired by:
http://mishadoff.com/blog/clojure-design-patterns/#episode-2-strategy