def test_fold_of_Just_gives_result(self): result = Just(10).fold(5, lambda x, y: x + y) self.assertEqual(15, result)
def test_Just_in_a_list(self): self.assertIn(Just(5), [Just(5)])
def test_map_flatmap_on_Just(self): result = Just(10).flatmap(lambda x: Just(5).map(lambda y: x + y)) self.assertEqual(Just(15), result)
def test_map_flatmap_on_Nothing(self): result = Just(10).flatmap(lambda x: Nothing.map(lambda y: x + y)) self.assertEqual(Nothing, result)
import os, sys sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) from maybe import Just from mite import Worker def func_a(x): x["a"] = 1 return x def func_b(x): x["b1"] = 2 return x pipeline = lambda x: Just(x).do(func_a).do(func_b) worker = Worker(pipeline=pipeline) worker.run()
def test_flatmap_of_Nothing_is_Nothing(self): self.assertEqual(Nothing, Nothing.flatmap(lambda x: Just(x + 5)))
def test_Just_is_defined(self): self.assertTrue(Just(5).is_defined())
def test_Just_as_key_in_a_dict(self): numbers = {Just(5): "five"} self.assertEqual("five", numbers.get(Just(5)))
def test_Just_is_equal_not_to_Nothing(self): self.assertNotEqual(Just(5), Nothing)
def test_iter_on_Just(self): has_been_changed = False for value in Just(10): has_been_changed = True self.assertTrue(has_been_changed)
def test_Just_is_not_equal_to_Just_with_different_value(self): self.assertNotEqual(Just(5), Just(10))
def test_Just_is_equal_to_Just_with_same_value(self): self.assertEqual(Just(5), Just(5))
def saveMax(l): return Just(max(l)) if len(l) > 0 else Nothing()
def test_Just_in_a_set(self): numbers = {Just(5), Just(5)} self.assertEqual(1, len(numbers))
def test_map_of_Just_is_another_Just(self): self.assertEqual(Just(15), Just(10).map(lambda x: x + 5))
def test_Maybe_of_not_None_is_Just(self): self.assertEqual(Just(5), Maybe.pure(5))
def test_flatmap_of_Just_is_another_Just(self): self.assertEqual(Just(15), Just(10).flatmap(lambda x: Just(x + 5)))
pass @Monad.instance_for(Maybe) class MaybeMonad(Monad): def flatmap(self, callable, monad): return monad.flatmap(callable) @Monad.instance_for(Validation) class ValidationMonad(Monad): def flatmap(self, callable, monad): return monad.flatmap(callable) def flatmap(callable, monad): return Monad.get_instance_for(monad).flatmap(callable, monad) # ---- if __name__ == '__main__': from maybe import Just from validation import Success print(map(lambda x: x + 5, Just(10))) print(map(lambda x: x + 5, Success(10))) print(flatmap(lambda x: Just(x + 5), Just(10))) print(flatmap(lambda x: Success(x + 5), Success(10)))
import pudb from maybe import Just, Nothing, lift from toolz import thread_first, partial def add (a, b): return a + b a = Just(1) b = Just(2) c = Nothing maybe_add = lift(add) d = maybe_add (a,b) e = maybe_add (a,c) pu.db f = a.bind(partial(add, b)) print ("d = ", d) print ("e = ", type(e)) print ("f = ", f)