Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Monadic plus for list transformer with set monad
(require '[clojure.algo.monads :as am])
(require '[monads.core :as m])
;-------------- algo.monads version #1 ---------------;
(def set-sequence-m (am/sequence-t am/set-m))
(am/with-monad set-sequence-m
(am/m-plus #{(list 1 2)} #{(list)} #{(list 3 4)}))
;; => #{() (3 4) (1 2)}
;-------------- algo.monads version #2 ---------------;
(def set-sequence-m (am/sequence-t am/set-m :m-plus-from-transformer))
(am/with-monad set-sequence-m
(am/m-plus #{(list 1 2)} #{(list)} #{(list 3 4)}))
;; => #{(1 2 3 4)}
;-------------- protocol-monads version --------------;
(def set-list (m/list-t hash-set))
@(m/plus [(set-list 1 2) (set-list) (set-list 3 4)])
;; => #{(1 2 3 4)}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.