Skip to content

Commit c77afd6

Browse files
committed
add protocols
1 parent 36844fe commit c77afd6

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

src/state_flow/protocols.clj

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
(ns state-flow.protocols)
2+
3+
(defprotocol Functor
4+
"A data type that can be mapped over without altering its context."
5+
(-fmap [ftor f fv] "Applies function f to the value(s) inside the context of the functor fv."))
6+
7+
(defprotocol Applicative
8+
"The Applicative abstraction."
9+
(-fapply [app af av]
10+
"Applies the function(s) inside af's context to the value(s)
11+
inside av's context while preserving the context.")
12+
(-pure [app v]
13+
"Takes any context or monadic value `app` and any value `v`, and puts
14+
the value `v` in the most minimal context (normally `mempty`) of same type of `app`"))
15+
16+
(defprotocol Monad
17+
"The Monad abstraction."
18+
(-mreturn [m v])
19+
(-mbind [m mv f]))
20+
21+
(defprotocol Contextual
22+
"Abstraction that establishes a concrete type as a member of a context.
23+
24+
A great example is the Maybe monad type Just. It implements
25+
this abstraction to establish that Just is part of
26+
the Maybe monad."
27+
(-get-context [_] "Get the context associated with the type."))
28+
29+
(defprotocol Extract
30+
"A type class to extract the
31+
value from a monad context."
32+
(-extract [mv] "Extract the value from monad context."))
33+
34+
(defprotocol Printable
35+
"An abstraction to make a type printable in a platform
36+
independent manner."
37+
(-repr ^String [_] "Get the repl ready representation of the object."))
38+
39+
(defprotocol MonadState
40+
"A specific case of Monad abstraction for
41+
work with state in pure functional way."
42+
(-get-state [m] "Return the current state.")
43+
(-put-state [m newstate] "Update the state.")
44+
(-swap-state [m f] "Apply a function to the current state and update it."))

0 commit comments

Comments
 (0)