Ejemplo n.º 1
0
 def test_split_with_state_monad(self):
     value = 7
     arrow = kleisli_split(state_return)
     state = KleisliArrow.runKleisli(arrow, value)
     result = State.runState(state, list())
     target = ((value, value), list())
     self.assertEquals(target, result)
Ejemplo n.º 2
0
    def test_right_with_state_monad(self):
        s1 = "*2"
        w = lambda a: a * 2
        f = lambda a: State(lambda s: (w(a), s.append(s1) or s))
        k = KleisliArrowChoice(state_return, f).right()

        value = 3.141
        left_state = KleisliArrow.runKleisli(k, Left(value))
        left = State.runState(left_state, [])
        left_target = (Left(value), [])
        self.assertEquals(left_target, left)

        right_state = KleisliArrow.runKleisli(k, Right(value))
        right = State.runState(right_state, [])
        right_target = (Right(w(value)), [s1])
        self.assertEquals(right_target, right)
Ejemplo n.º 3
0
    def test_with_return(self):
        value = 7
        state = list()
        m = return_(value)

        result = State.runState(m, state)
        self.assertEquals((value, state), result)
Ejemplo n.º 4
0
    def test_exec_state(self):
        value = 7
        state = list()
        msg = "*2"
        m_one = return_(value)
        m_two = m_one >= (lambda a: State(lambda s: (a * 2, s.append(msg) or s)))

        self.assertEquals([msg], State.execState(m_two, state))
Ejemplo n.º 5
0
    def test_single_mutable_state(self):
        # Value/state pair
        value = 7
        state = list()

        # Build stuff
        m = State(lambda s: (value, s.append(value)))
        target = (value, state.append(value))

        result = State.runState(m, state)
        self.assertEquals(target, result)
Ejemplo n.º 6
0
    def test_first_with_state_monad(self):
        w = lambda a: a * 2
        s1 = "*2"
        wk = lambda a: State(lambda s: (w(a), s.append(s1) or s))
        arrow = KleisliArrow(state_return, wk).first()

        value = 9
        state = KleisliArrow.runKleisli(arrow, (value, value))
        result = State.runState(state, list())
        target = ((w(value), value), [s1])
        self.assertEquals(target, result)
Ejemplo n.º 7
0
    def test_single_immutable_state(self):
        # Value/state pair
        value = 7
        state = list()

        # Build stuff
        m = return_(value)
        target = (value, state)

        result = State.runState(m, state)
        self.assertEquals(target, result)
Ejemplo n.º 8
0
    def test_unsplit_with_state_monad(self):
        value = 7

        k1 = kleisli_split(state_return)

        f = lambda x, y: x * y
        k2 = kleisli_unsplit(state_return, f)

        arrow = k1 >> k2

        state = KleisliArrow.runKleisli(arrow, value)
        result = State.runState(state, list())
        target = (f(value, value), list())
        self.assertEquals(target, result)
Ejemplo n.º 9
0
    def test_triple_pipe_with_state_monad(self):
        s1 = "*2"
        w = lambda a: a * 2
        f = lambda a: State(lambda s: (w(a), s.append(s1) or s))
        k1 = KleisliArrowChoice(state_return, f)

        s2 = "-9"
        y = lambda a: a - 9
        h = lambda a: State(lambda s: (y(a), s.append(s2) or s))
        k2 = KleisliArrowChoice(state_return, h)
        
        arrow = k1 | k2

        value = 19
        left_state = KleisliArrow.runKleisli(arrow, Left(value))
        left = State.runState(left_state, [])
        left_target = (w(value), [s1])
        self.assertEquals(left_target, left)

        right_state = KleisliArrow.runKleisli(arrow, Right(value))
        right = State.runState(right_state, [])
        right_target = (y(value), [s2])
        self.assertEquals(right_target, right)
Ejemplo n.º 10
0
    def test_many_mutable_state(self):
        # Build this:
        # state (\s -> (1, s ++ ["Initial value 1"]))
        #    >>= (\a -> state (\s -> (a * 2, s ++ ["Mult by 2"])))
        #    >>= (\a -> state (\s -> (a - 9, s ++ ["Minus 9"])))
        s_one = "Initial value 1"
        s_two = "Multiply by 2"
        s_three = "Minus 9"

        m_one = State(lambda s: (1, s.append(s_one) or s))
        m_two = m_one >= (lambda a: State(lambda s: (a * 2, s.append(s_two) or s)))
        m_three = m_two >= (lambda a: State(lambda s: (a - 9, s.append(s_three) or s)))

        result = State.runState(m_three, list())
        self.assertEquals((-7, [s_one, s_two, s_three]), result)
Ejemplo n.º 11
0
    def test_state_monad(self):
        s1 = "*2"
        w = lambda a: a * 2
        f = lambda a: State(lambda s: (w(a), s.append(s1) or s))
        k1 = KleisliArrow(state_return, f)

        s2 = "-9"
        x = lambda a: a - 9
        h = lambda a: State(lambda s: (x(a), s.append(s2) or s))
        k2 = KleisliArrow(state_return, h)

        arrow = k1 >> k2

        value = 5
        state = list()
        state_monad = KleisliArrow.runKleisli(arrow, value) # This is the value
        self.assertEquals((x(w(value)), [s1, s2]), State.runState(state_monad, state)) # This is the state
Ejemplo n.º 12
0
    def test_triple_ampersand_with_state_monad(self):
        s1 = "*2"
        w = lambda a: a * 2
        f = lambda a: State(lambda s: (w(a), s.append(s1) or s))
        k1 = KleisliArrow(state_return, f)

        s2 = "-9"
        x = lambda a: a - 9
        h = lambda a: State(lambda s: (x(a), s.append(s2) or s))
        k2 = KleisliArrow(state_return, h)

        arrow = k1 & k2

        value = 5
        state = list()
        state_monad = KleisliArrow.runKleisli(arrow, value)

        target = ((w(value), x(value)), [s1, s2])
        result = State.runState(state_monad, state)
        self.assertEquals(target, result)
Ejemplo n.º 13
0
def exec_pipeline(state_monad, state):
    return State.execState(state_monad, state)
Ejemplo n.º 14
0
def eval_pipeline(state_monad, state):
    return State.evalState(state_monad, state)
Ejemplo n.º 15
0
def run_pipeline(state_monad, state):
     return State.runState(state_monad, state)
Ejemplo n.º 16
0
    def test_eval_state(self):
        value = 7
        state = list()
        m = return_(value)

        self.assertEquals(value, State.evalState(m, state))
Ejemplo n.º 17
0
def exec_pipeline(state_monad, state):
    wrapped_state = State.execState(state_monad, state)
    return wrapped_state.state
Ejemplo n.º 18
0
def eval_pipeline(state_monad, state):
    future = State.evalState(state_monad, state)
    return __handle_output(future)
Ejemplo n.º 19
0
def run_pipeline(state_monad, state):
     output = State.runState(state_monad, state)
     return (__handle_output(output[0]), output[1].state)