示例#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
文件: memcache.py 项目: ericgj/fungi
 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
文件: user.py 项目: ericgj/fungi
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
文件: oauth.py 项目: ericgj/fungi
        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
文件: memcache.py 项目: ericgj/fungi
 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))