def test_nothing_monad_law_associativity(self): # (m >>= f) >>= g is just like doing m >>= (\x -> f x >>= g) m = Nothing() f = lambda x: unit(x + 1000) g = lambda y: unit(y * 42) self.assertEqual(m.bind(f).bind(g), m.bind(lambda x: f(x).bind(g)))
def test_nothing_applicative_law_homomorphism(self): # pure f <*> pure x = pure (f x) f = lambda x: x * 42 self.assertEqual( pure(f).apply(Nothing()), Nothing() )
def test_nothing_functor_map(self): f = lambda x: x+2 x = Nothing() self.assertEquals( x.map(f), x )
def test_nothing_monad_bind(self): m = Nothing() f = lambda x: unit(x*10) self.assertEqual( m.bind(f), Nothing() )
def test_nothing_functor_map(self): f = lambda x: x + 2 x = Nothing() self.assertEqual( x.map(f), x )
def test_nothing_monad_law_left_identity(self): # return x >>= f is the same thing as f x f = lambda x: unit(x+100000) self.assertEqual( Nothing().bind(f), Nothing() )
def test_nothing_applicative_law_functor(self): # pure f <*> x = fmap f x x = Nothing() f = lambda x: x * 42 self.assertEquals( pure(f).apply(x), x.map(f) )
def test_nothing_monad_law_right_identity(self): # m >>= return is no different than just m. m = Nothing() self.assertEqual( m.bind(unit), m )
def test_nothing_applicative_law_interchange(self): # u <*> pure y = pure ($ y) <*> u u = unit(lambda x: x*42) self.assertEqual( u.apply(Nothing()), Nothing().apply(u) )
def test_nothing_applicative_law_functor(self): # pure f <*> x = fmap f x x = Nothing() f = lambda x: x * 42 self.assertEqual( pure(f).apply(x), x.map(f) )
def test_nothing_monad_law_associativity(self): # (m >>= f) >>= g is just like doing m >>= (\x -> f x >>= g) m = Nothing() f = lambda x: unit(x+1000) g = lambda y: unit(y*42) self.assertEqual( m.bind(f).bind(g), m.bind(lambda x: f(x).bind(g)) )
def test_nothing_functor_law2(self): # fmap (f . g) x = fmap f (fmap g x) def f(x): return x + 10 def g(x): return x * 10 x = Nothing() self.assertEquals(x.map(compose(f, g)), x.map(g).map(f))
def test_nothing_functor_law2(self): # fmap (f . g) x = fmap f (fmap g x) def f(x): return x+10 def g(x): return x*10 x = Nothing() self.assertEquals( x.map(compose(f, g)), x.map(g).map(f) )
def test_maybe_monoid_just_append_nothing(self): m = Just("Python") self.assertEqual( m + Nothing(), m )
def test_nothing_applicative_law_identity(self): # pure id <*> v = v v = Nothing() self.assertEqual( pure(identity).apply(v), v )
def test_identity_applicative_law_composition(self): # pure (.) <*> u <*> v <*> w = u <*> (v <*> w) w = Nothing() u = pure(lambda x: x * 42) v = pure(lambda x: x + 42) self.assertEquals( pure(fmap).apply(u).apply(v).apply(w), u.apply(v.apply(w)))
def _get(self, _p, _fp, _d, default): if len(_p) == 1: try: if isinstance(_d, dict) is True: return Just(_d[_p[0]]) if isinstance(_d, DP) is True: return Just(_d.raw()[_p[0]]) else: return Nothing() except KeyError: if isinstance(default, Monad) is True: return default else: return Just(default) d = _d.get(_p[0], None) if _d.get(_p[0], None) is None: _fp.append(_p[0]) d = self._mkdir(_d, _p[0], _fp) return self._get(_p[1:], _fp, d, default)
def dpget(ns, path): res = ns.V(path) if res == Nothing(): raise KeyError(path) return res
def test_just_applicative_1(self): a = Just.pure(lambda x, y: x + y).apply(Just(2)).apply(Just(40)) self.assertNotEquals(a, Nothing()) self.assertEquals(a, Just(42))
def test_maybe_monoid_nothing_append_just(self): m = Just("Python") self.assertEquals(Nothing().append(m), m)
def test_nothing_functor_law1(self): # fmap id = id self.assertEqual( Nothing().map(identity), Nothing() )
def test_just_applicative_3(self): a = Just.pure(lambda x, y: x+y).apply(Just(42)).apply(Nothing()) self.assertEqual(a, Nothing())
from oslash.maybe import Just, Nothing Value = Just NONE = Nothing() TRUE = Just(True) FALSE = Just(False) def isNothing(x): return x is None or x == NONE or isinstance(x, Nothing)
def get(self, path, default=Nothing()): _p = os.path.abspath(path).split("/") _p = [i for i in _p if i] return self._get(_p, [""], self._value, default)