Skip to content

State

State dataclass

Bases: Generic[A, S]

State is a monad.

Source code in funclift/types/state.py
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
@dataclass
class State(Generic[A, S]):
    """State is a monad."""

    action: Callable[[S], Tuple[A, S]]

    def fmap(self, f: Callable[[A], B]) -> State[B, S]:
        def new_action(s: S) -> Tuple[B, S]:
            a, s2 = self.action(s)
            return f(a), s2
        return State(new_action)

    @staticmethod
    def pure(b: B) -> State[B, S2]:
        def new_action(s: S2) -> Tuple[B, S2]:
            return b, s
        return State(new_action)

    @staticmethod
    def read() -> State[S, S]:
        def new_action(s: S) -> Tuple[S, S]:
            return s, s
        return State(new_action)

    @staticmethod
    def write(s2: S) -> State[None, S]:
        def new_action(s: S) -> Tuple[None, S]:
            return None, s2
        return State(new_action)

    def run(self, s: S) -> Tuple[A, S]:
        return self.action(s)

    @staticmethod
    def run_state(sm: State[A, S], s: S) -> Tuple[A, S]:
        return sm.action(s)

    def flatmap(self: State[A, S],
                f: Callable[[A], State[B, S]]) -> State[B, S]:
        def new_action(s: S) -> Tuple[B, S]:
            a, s2 = self.action(s)
            return f(a).action(s2)
        return State(new_action)