def curried(s): part = Just(s) part >>= expandCharacters(ce) part >>= replaceCharacters(cr) part >>= replaceCharacters(ci) return part.value
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)))
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
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))
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
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))
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
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)
def test_mplus_with_just_and_nothing(self): self.givenMonoids(First(Just(1)), Nothing) self.ensure_mconcat_equals(First(Just(1)))
def test_right_identity(self): self.givenMonoid(First(Just(9))) self.ensure_monoid_plus_zero_equals(First(Just(9)))
def replaceCharacters(table, s): b = unicodedata.normalize('NFKD', s) s = b.translate(table) return Just(s)
def testEqualityOfIdenticalTypes(self): self.givenMonads(Just(8), Just(8)) self.ensureMonadsAreEqual()
def monad_function_f(self, x): return Just(x + 10)
def test_mplus_with_two_just_values(self): self.givenMonoids(Last(Just(1)), Last(Just(2))) self.ensure_mconcat_equals(Last(Just(2)))
def testSafeRootTo9() -> bool: return all(testSafeRoot(n) == Just(2) for n in range(10))
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()
def test_left_identity(self): self.givenMonoid(Last(Just(9))) self.ensure_zero_plus_monoid_equals(Last(Just(9)))
def safeSqrt(val: float) -> Maybe: return Nothing if val < 0 else Just(sqrt(val))
def test_associativity(self): self.givenMonoids(Last(Just(1)), Last(Just(2)), Last(Just(3))) self.ensure_associativity()
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))
def test_mplus_with_nothing_and_just(self): self.givenMonoids(Nothing, Last(Just(1))) self.ensure_mconcat_equals(Last(Just(1)))
def replaceToken(table, s): return Just(table[s]) if s in table else Just(s)
def monad_function_g(self, x): return Just(x * 5)
def testInequalityOfJustAndNothing(self): self.givenMonads(Just(8), Nothing) self.ensureMonadsAreNotEqual()
def safediv(a: int, b: int) -> Maybe: if b == 0: return Nothing else: return Just((a // b))
def testMonadComparisonExceptionWithJust(self): self.givenMonads(Just(8), Reader(8)) self.ensureComparisonRaisesException()
def ignoreToken(table, s): return Nothing if s in table else Just(s)
def expandCharacters(table, s): expanded = [table[c] if c in table else c for c in s] return Just(''.join(expanded))