|
| 1 | +(ns cats.monad.state |
| 2 | + (:refer-clojure :exclude [eval get]) |
| 3 | + (:require [cats.context :as ctx :refer [*context*]] |
| 4 | + [cats.core :as m] |
| 5 | + [cats.data :as d] |
| 6 | + [cats.protocols :as p] |
| 7 | + [cats.util :as util])) |
| 8 | + |
| 9 | +(declare context) |
| 10 | + |
| 11 | +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 12 | +;; Protocol declaration |
| 13 | +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 14 | + |
| 15 | +(defprotocol MonadState |
| 16 | + "A specific case of Monad abstraction for |
| 17 | + work with state in pure functional way." |
| 18 | + (-get-state [m] "Return the current state.") |
| 19 | + (-put-state [m newstate] "Update the state.") |
| 20 | + (-swap-state [m f] "Apply a function to the current state and update it.")) |
| 21 | + |
| 22 | +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 23 | +;; Type constructors and functions |
| 24 | +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 25 | + |
| 26 | +(defrecord State [mfn state-context] |
| 27 | + p/Contextual |
| 28 | + (-get-context [_] state-context) |
| 29 | + |
| 30 | + p/Extract |
| 31 | + (-extract [_] mfn)) |
| 32 | + |
| 33 | +(alter-meta! #'->State assoc :private true) |
| 34 | + |
| 35 | +(defn state |
| 36 | + "The State type constructor. |
| 37 | + The purpose of State type is wrap a simple |
| 38 | + function that fullfill the state signature. |
| 39 | + It exists just for avoid extend the clojure |
| 40 | + function type because is very generic type." |
| 41 | + ([f] |
| 42 | + (State. f context)) |
| 43 | + ([f state-context] |
| 44 | + (State. f state-context))) |
| 45 | + |
| 46 | +(defn state? |
| 47 | + "Return true if `s` is instance of |
| 48 | + the State type." |
| 49 | + [s] |
| 50 | + (instance? State s)) |
| 51 | + |
| 52 | +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 53 | +;; Monad definition |
| 54 | +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 55 | + |
| 56 | +(def ^{:no-doc true} |
| 57 | + context |
| 58 | + (reify |
| 59 | + p/Context |
| 60 | + |
| 61 | + p/Extract |
| 62 | + (-extract [mv] (p/-extract mv)) |
| 63 | + |
| 64 | + p/Functor |
| 65 | + (-fmap [_ f fv] |
| 66 | + (state (fn [s] |
| 67 | + (let [[v ns] ((p/-extract fv) s)] |
| 68 | + (d/pair (f v) ns))))) |
| 69 | + |
| 70 | + p/Monad |
| 71 | + (-mreturn [_ v] |
| 72 | + (state (partial d/pair v))) |
| 73 | + |
| 74 | + (-mbind [_ self f] |
| 75 | + (state (fn [s] |
| 76 | + (let [p ((p/-extract self) s) |
| 77 | + value (.-fst p) |
| 78 | + newstate (.-snd p)] |
| 79 | + ((p/-extract (f value)) newstate))))) |
| 80 | + |
| 81 | + MonadState |
| 82 | + (-get-state [_] |
| 83 | + (state #(d/pair %1 %1))) |
| 84 | + |
| 85 | + (-put-state [_ newstate] |
| 86 | + (state #(d/pair % newstate))) |
| 87 | + |
| 88 | + (-swap-state [_ f] |
| 89 | + (state #(d/pair %1 (f %1)))) |
| 90 | + |
| 91 | + p/Printable |
| 92 | + (-repr [_] |
| 93 | + #"<State>"))) |
| 94 | + |
| 95 | +(util/make-printable (type context)) |
| 96 | + |
| 97 | +(defn ^:private get-context |
| 98 | + "Default to context if no context set" |
| 99 | + [] |
| 100 | + (if (nil? *context*) |
| 101 | + context |
| 102 | + *context*)) |
| 103 | + |
| 104 | +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 105 | +;; Public Api |
| 106 | +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 107 | + |
| 108 | +(defn get |
| 109 | + "Return a State instance with computation that returns |
| 110 | + the current state." |
| 111 | + [] |
| 112 | + (ctx/with-context (get-context) |
| 113 | + (-get-state (ctx/infer)))) |
| 114 | + |
| 115 | +(defn put |
| 116 | + "Return a State instance with computation that replaces |
| 117 | + the current state with specified new state." |
| 118 | + [newstate] |
| 119 | + (ctx/with-context (get-context) |
| 120 | + (-put-state (ctx/infer) newstate))) |
| 121 | + |
| 122 | +(defn swap |
| 123 | + "Return a State instance with computation that applies the |
| 124 | + specified function to state and returns the old state." |
| 125 | + [f] |
| 126 | + (ctx/with-context (get-context) |
| 127 | + (-swap-state (ctx/infer) f))) |
| 128 | + |
| 129 | +(defn run |
| 130 | + "Given a State instance, execute the |
| 131 | + wrapped computation and returns a cats.data.Pair |
| 132 | + instance with result and new state. |
| 133 | + (def computation (mlet [x (get-state) |
| 134 | + y (put-state (inc x))] |
| 135 | + (return y))) |
| 136 | + (def initial-state 1) |
| 137 | + (run-state computation initial-state) |
| 138 | + This should return something to: #<Pair [1 2]>" |
| 139 | + [state seed] |
| 140 | + ((p/-extract state) seed)) |
| 141 | + |
| 142 | +(defn eval |
| 143 | + "Given a State instance, execute the |
| 144 | + wrapped computation and return the resultant |
| 145 | + value, ignoring the state. |
| 146 | + Equivalent to taking the first value of the pair instance |
| 147 | + returned by `run-state` function." |
| 148 | + [state seed] |
| 149 | + (first (run state seed))) |
| 150 | + |
| 151 | +(defn exec |
| 152 | + "Given a State instance, execute the |
| 153 | + wrapped computation and return the resultant |
| 154 | + state. |
| 155 | + Equivalent to taking the second value of the pair instance |
| 156 | + returned by `run-state` function." |
| 157 | + [state seed] |
| 158 | + (second (run state seed))) |
| 159 | + |
| 160 | +(defn gets |
| 161 | + "State monad that returns the result of applying |
| 162 | + a function to a state" |
| 163 | + [projfn] |
| 164 | + (m/mlet [s (get)] |
| 165 | + (m/return (projfn s)))) |
| 166 | + |
| 167 | +(defn wrap-fn |
| 168 | + "Wraps a (possibly side-effecting) function to a state monad" |
| 169 | + [my-fn] |
| 170 | + (state (fn [s] |
| 171 | + (d/pair (my-fn) s)))) |
0 commit comments