-
Notifications
You must be signed in to change notification settings - Fork 11
/
core.clj
40 lines (34 loc) · 1.07 KB
/
core.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
(ns xiana.core
(:require
[cats.core :as m]
[cats.monad.either :as me]))
;; state/context record definition
(defrecord State
[request request-data response session-data deps])
;; monad.either/right container alias
;; don't stop the sequence of executions, continue! (implicit)
(def ok me/right)
;; monad.either/left container alias
;; and stop the sequence of executions (implicit)
;; used by >>= function
(def error me/left)
;; monad.extract alias
;; unwrap monad container
(def extract m/extract)
(defmacro apply-flow->
"Simple macro that applies Haskell-style left associative bind to a
queue of functions."
[state & queue]
`(apply m/>>= (ok ~state) ~@queue))
(defmacro flow->
"Expand a single form to (form) or the sequence of forms to:
(lambda (x) (form1 x), (lambda (x) (form2 x)) ...
and perform Haskell-style left-associative bind using the
monad.either/right context (wrapped)."
[state & forms]
`(m/>>=
(ok ~state)
~@(for [form forms]
(if (seq? form)
`(fn [~'x] (~(first form) ~'x ~@(rest form)))
form))))