Skip to content

Instantly share code, notes, and snippets.

@mpjdem
Created September 3, 2019 16:39
Show Gist options
  • Save mpjdem/8cb5607e05696fc31c5c6a0fb46cd71a to your computer and use it in GitHub Desktop.
Save mpjdem/8cb5607e05696fc31c5c6a0fb46cd71a to your computer and use it in GitHub Desktop.
'Strategy' design pattern in Clojure
(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