-
Notifications
You must be signed in to change notification settings - Fork 13
/
state.clj
148 lines (121 loc) · 3.67 KB
/
state.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
(ns state-flow.state
(:refer-clojure :exclude [eval get when])
(:require [cats.core :as m]
[cats.monad.exception :as e]
[cats.monad.state :as state]
[cats.protocols :as p]
[cats.util :as util]))
(declare short-circuiting-context)
(defn- result-or-err [f & args]
(let [result ((e/wrap (partial apply f)) args)]
(if (e/failure? result)
result
@result)))
(defn error-catching-state [mfn]
(state/state
(fn [s]
(let [new-pair ((e/wrap mfn) s)]
(if (e/failure? new-pair)
[new-pair s]
@new-pair)))
short-circuiting-context))
(def short-circuiting-context
"Same as state monad context, but short circuits if error happens, place error in return value"
(reify
p/Context
p/Functor
(-fmap [_ f fv]
(error-catching-state
(fn [s]
(let [[v s'] ((p/-extract fv) s)]
(if (e/failure? v)
[v s']
[(result-or-err f v) s'])))))
p/Monad
(-mreturn [_ v]
(error-catching-state #(vector v %)))
(-mbind [_ self f]
(error-catching-state
(fn [s]
(let [[v s'] ((p/-extract self) s)]
(if (e/failure? v)
[v s']
((p/-extract (f v)) s'))))))
state/MonadState
(-get-state [_]
(error-catching-state #(vector %1 %1)))
(-put-state [_ newstate]
(error-catching-state #(vector % newstate)))
(-swap-state [_ f]
(error-catching-state #(vector %1 (f %1))))
p/Printable
(-repr [_]
"#<State-E>")))
(util/make-printable (type short-circuiting-context))
(defn get
"Creates a flow that returns the value of state. "
[]
(state/get short-circuiting-context))
(defn gets
"Creates a flow that returns the result of applying f (default identity)
to state with any additional args."
([]
(gets identity))
([f & args]
(state/gets #(apply f % args) short-circuiting-context)))
(defn put
"Creates a flow that replaces state with new-state. "
[new-state]
(state/put new-state short-circuiting-context))
(defn modify
"Creates a flow that replaces state with the result of applying f to
state with any additional args."
[f & args]
(state/swap #(apply f % args) short-circuiting-context))
(defn return
"Creates a flow that returns v. Use this as the last
step in a flow that you want to reuse in other flows, in
order to clarify the return value, e.g.
(def increment-count
(flow \"increments :count and returns it\"
(state/modify update :count inc)
[new-count (state/gets :count)]
(state-flow/return new-count)))"
[v]
(m/return short-circuiting-context v))
(defn invoke
"Creates a flow that invokes a function of no arguments and returns the
result. Used to invoke side effects e.g.
(state-flow.core/invoke #(Thread/sleep 1000))"
[my-fn]
(error-catching-state (fn [s] [(my-fn) s])))
(defn when
"Given an expression `e` and a flow, if the expression is logical true, return the flow. Otherwise, return nil in a monadic context."
[e flow]
(if e
flow
(return nil)))
(def
^{:doc "Creates a flow that returns the application of f to the return of flow"
:arglists '([f flow])}
fmap
m/fmap)
(defn ^:deprecated swap
"DEPRECATED: use state-flow.state/modify instead."
[f]
(modify f))
(def ^{:deprecated true
:doc "DEPRECATED: Use state-flow.state/invoke instead."}
wrap-fn
invoke)
(def state? state/state?)
(def run state/run)
(def eval state/eval)
(def exec state/exec)
(defn ensure-step
"Internal use only.
Given a state-flow step, returns value as/is, else wraps value in a state-flow step."
[value]
(if (state? value)
value
(return value)))