Esempio n. 1
0
    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)))
Esempio n. 2
0
    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()
        )
Esempio n. 3
0
    def test_nothing_functor_map(self):
        f = lambda x: x+2
        x = Nothing()

        self.assertEquals(
            x.map(f),
            x
        )
Esempio n. 4
0
    def test_nothing_monad_bind(self):
        m = Nothing()
        f = lambda x: unit(x*10)

        self.assertEqual(
            m.bind(f),
            Nothing()
        )
Esempio n. 5
0
    def test_nothing_monad_bind(self):
        m = Nothing()
        f = lambda x: unit(x*10)

        self.assertEqual(
            m.bind(f),
            Nothing()
        )
Esempio n. 6
0
    def test_nothing_functor_map(self):
        f = lambda x: x + 2
        x = Nothing()

        self.assertEqual(
            x.map(f),
            x
        )
Esempio n. 7
0
    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()
        )
Esempio n. 8
0
    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)
        )
Esempio n. 9
0
    def test_nothing_monad_law_right_identity(self):
        # m >>= return is no different than just m.

        m = Nothing()

        self.assertEqual(
            m.bind(unit),
            m
        )
Esempio n. 10
0
    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)
        )
Esempio n. 11
0
    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)
        )
Esempio n. 12
0
    def test_nothing_monad_law_right_identity(self):
        # m >>= return is no different than just m.

        m = Nothing()

        self.assertEqual(
            m.bind(unit),
            m
        )
Esempio n. 13
0
    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))
        )
Esempio n. 14
0
    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))
Esempio n. 15
0
    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)
        )
Esempio n. 16
0
    def test_maybe_monoid_just_append_nothing(self):
        m = Just("Python")

        self.assertEqual(
            m + Nothing(),
            m
        )
Esempio n. 17
0
    def test_nothing_applicative_law_identity(self):
        # pure id <*> v = v
        v = Nothing()

        self.assertEqual(
            pure(identity).apply(v),
            v
        )
Esempio n. 18
0
    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)))
Esempio n. 19
0
File: dp.py Progetto: vulogov/core.F
 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)
Esempio n. 20
0
File: dp.py Progetto: vulogov/core.F
def dpget(ns, path):
    res = ns.V(path)
    if res == Nothing():
        raise KeyError(path)
    return res
Esempio n. 21
0
 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))
Esempio n. 22
0
    def test_maybe_monoid_nothing_append_just(self):
        m = Just("Python")

        self.assertEquals(Nothing().append(m), m)
Esempio n. 23
0
 def test_nothing_functor_law1(self):
     # fmap id = id
     self.assertEqual(
         Nothing().map(identity),
         Nothing()
     )
Esempio n. 24
0
 def test_just_applicative_3(self):
     a = Just.pure(lambda x, y: x+y).apply(Just(42)).apply(Nothing())
     self.assertEqual(a, Nothing())
Esempio n. 25
0
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)
Esempio n. 26
0
File: dp.py Progetto: vulogov/core.F
 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)