Пример #1
0
        def curried(s):
            part = Just(s)
            part >>= expandCharacters(ce)
            part >>= replaceCharacters(cr)
            part >>= replaceCharacters(ci)

            return part.value
Пример #2
0
def eval(exp: Expr) -> Maybe:
    if isinstance(exp, Val):
        return Just(exp.n)
    elif isinstance(exp, Div):
        return eval(exp.a) >> (lambda x:\
                eval(exp.b) >> (lambda y:\
                    safediv(x, y)))
Пример #3
0
def eval_do(exp: Expr) -> Maybe:
    if isinstance(exp, Val):
        return Just(exp.n)
    elif isinstance(exp, Div):
        n = eval_do(exp.a)
        m = eval_do(exp.b)
        return safediv(n.getValue(), m.getValue()) if n and m else None
Пример #4
0
 def _get_multi(rej, res):
     try:
         r = memcache.get_multi(keys, key_prefix=prefix)
         res([
             Nothing if r.get(k) is None else Just(r.get(k)) for k in keys
         ])
     except Exception as e:
         rej(err.wrap(e))
Пример #5
0
def current():
    # () -> Maybe User
    try:
        u = users.get_current_user()
        if u is None:
            return Nothing
        else:
            return Just(u)
    except Exception as e:
        return Nothing
Пример #6
0
        def _get(rej, res):
            try:
                ent = self._model.get_by_id(uid)
                if ent is None:
                    res(Nothing)
                else:
                    res(Just(ent))

            except Exception as e:
                rej(err.wrap(e))
Пример #7
0
        def curried(s):
            parts = []
            for token in expandToken(self.tokenExpand, tokenize(s)):
                part = Just(token.lower())
                part >>= replaceToken(self.tokenReplace)
                part >>= ignoreToken(self.tokenIgnore)
                part >>= expandCharacters(ce)
                part >>= replaceCharacters(cr)
                part >>= replaceCharacters(ci)
                parts.append(part)

            joined = ''.join([x.value for x in parts if x.value])
            return joined
Пример #8
0
        def curried(s):
            parts = []
            for token in expandToken(self.tokenExpand, tokenize(s)):
                part = Just(token.lower())
                part >>= replaceToken(self.tokenReplace)
                part >>= ignoreToken(self.tokenIgnore)
                part >>= expandCharacters(ce)
                part >>= replaceCharacters(cr)
                part >>= replaceCharacters(ci)
                part >>= expandCharacters(r)
                parts.append(part)

            delimited = []
            for pair in window([x.value for x in parts if x.value]):
                delimited.append(pair[0])
                if pair[1][0] not in ['.', '%']:
                    delimited.append('+')
            delimited.append(parts[-1].value)

            return ''.join(delimited)
Пример #9
0
 def test_mplus_with_just_and_nothing(self):
     self.givenMonoids(First(Just(1)), Nothing)
     self.ensure_mconcat_equals(First(Just(1)))
Пример #10
0
 def test_right_identity(self):
     self.givenMonoid(First(Just(9)))
     self.ensure_monoid_plus_zero_equals(First(Just(9)))
Пример #11
0
def replaceCharacters(table, s):
    b = unicodedata.normalize('NFKD', s)
    s = b.translate(table)
    return Just(s)
Пример #12
0
 def testEqualityOfIdenticalTypes(self):
     self.givenMonads(Just(8), Just(8))
     self.ensureMonadsAreEqual()
Пример #13
0
 def monad_function_f(self, x):
     return Just(x + 10)
Пример #14
0
 def test_mplus_with_two_just_values(self):
     self.givenMonoids(Last(Just(1)), Last(Just(2)))
     self.ensure_mconcat_equals(Last(Just(2)))
Пример #15
0
def testSafeRootTo9() -> bool:
    return all(testSafeRoot(n) == Just(2) for n in range(10))
Пример #16
0
from pymonad.Maybe import Just, Nothing
from pymonad.List import List


def add(x, y):
    return x + y


add << Just(7) & Just(8)  # returns Just(15)
add << Nothing & Just(8)  # returns Nothing
add << Just(7) & Nothing  # returns Nothing
add << List(1, 2, 3) & List(4, 5, 6)  # returns List(5, 6, 7, 6, 7, 8, 7, 8, 9)
 def testMonadComparisonException(self):
     self.givenMonads(StringWriter(8, "log message"), Just(8))
     self.ensureComparisonRaisesException()
Пример #18
0
 def test_left_identity(self):
     self.givenMonoid(Last(Just(9)))
     self.ensure_zero_plus_monoid_equals(Last(Just(9)))
Пример #19
0
def safeSqrt(val: float) -> Maybe:
    return Nothing if val < 0 else Just(sqrt(val))
Пример #20
0
 def test_associativity(self):
     self.givenMonoids(Last(Just(1)), Last(Just(2)), Last(Just(3)))
     self.ensure_associativity()
Пример #21
0
 def _get(rej, res):
     try:
         r = memcache.get(key)
         res(Nothing if r is None else Just(r))
     except Exception as e:
         rej(err.wrap(e))
Пример #22
0
 def test_mplus_with_nothing_and_just(self):
     self.givenMonoids(Nothing, Last(Just(1)))
     self.ensure_mconcat_equals(Last(Just(1)))
Пример #23
0
def replaceToken(table, s):
    return Just(table[s]) if s in table else Just(s)
Пример #24
0
 def monad_function_g(self, x):
     return Just(x * 5)
Пример #25
0
 def testInequalityOfJustAndNothing(self):
     self.givenMonads(Just(8), Nothing)
     self.ensureMonadsAreNotEqual()
Пример #26
0
def safediv(a: int, b: int) -> Maybe:
    if b == 0:
        return Nothing
    else:
        return Just((a // b))
Пример #27
0
 def testMonadComparisonExceptionWithJust(self):
     self.givenMonads(Just(8), Reader(8))
     self.ensureComparisonRaisesException()
Пример #28
0
def ignoreToken(table, s):
    return Nothing if s in table else Just(s)
Пример #29
0
def expandCharacters(table, s):
    expanded = [table[c] if c in table else c for c in s]
    return Just(''.join(expanded))