Commits

Meikel Brandmeyer  committed 2137ca4

Added sequence to turn a sequence of monads into a monad of the result sequence

  • Participants
  • Parent commits f7e8a0f

Comments (0)

Files changed (1)

File src/de/kotka/monad.clj

       `(bind ~monad (fn [~v] ~@body))
       `(bind ~monad (fn [~v] (let-bind ~clauses ~@body))))))
 
+(defn sequence
+  "Convert the given sequence of monads into a monad of the given
+  type with the value set to the sequence of the values. There must
+  be at least one monad in the monad collection."
+  [monads]
+  (let [f (fn [ms m]
+            (let-bind [result  m
+                       results ms]
+              (return (monad-type m) (conj results result))))]
+    (reduce f (return (-> monads first monad-type) nil) (reverse monads))))
+
 (defmacro lift-into
   "Call the given function f into the given monad with the values of
   all given monads in the same order."