Meikel  Brandmeyer avatar Meikel Brandmeyer committed fc5d1a1

Added MRunnable interface

* monad.clj (run): new multimethod

* monad/state.clj <State>: derive also from MRunnable
(eval-state, exec-state, bind): use new run interface
(run-state): removed function

Comments (0)

Files changed (2)

src/de/kotka/monad.clj

   (:use
      [clojure.contrib.def :only (defstruct-)]))
 
-(declare Monad MZero)
+(declare Monad MZero MRunnable)
 
 (derive `MZero `Monad)
 
   [m f & ms]
   (let [xs (take (count ms) (drop 1 (iterate gensym "lift__")))]
     `(let-bind ~(vec (interleave xs ms)) (return ~m (~f ~@xs)))))
+
+(defmulti
+  #^{:arglists '([monad & args])
+     :doc
+  "Applies the value of the given monad to the arguments and returns
+  the result. Not all monad types are `Runnable."}
+  run
+  (fn [m _] (monad-type m)))
+
+(defmethod run `MRunnable
+  [m & args]
+  (apply (monad m) args))

src/de/kotka/monad/state.clj

 (declare State)
 
 (derive `State `Monad)
+(derive `State `MRunnable)
 
 (defmethod return `State
   [t x]
   [s]
   (make-monad `State (fn [_] [nil s])))
 
-(defn run-state
-  "Apply a state monad to the given initial state. Returns a vector
-  consisting of the value and the final state."
-  [m s]
-  ((monad m) s))
-
 (defvar
   eval-state
-  (comp first run-state)
+  (comp first run)
   "Run the given state monad and return its value. Equivalent to run-state
   followed by first.")
 
 (defvar
   exec-state
-  (comp second run-state)
+  (comp second run)
   "Run the given state monad and return its final state. Equivalent to
   run-state followed by second.")
 
   [m f]
   (make-monad `State
               (fn [s0]
-                (let [[a s1] (run-state m s0)]
-                  (run-state (f a) s1)))))
+                (let [[a s1] (run m s0)]
+                  (run (f a) s1)))))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.