def test_maybe_monoid_just_append_nothing(self): m = Just("Python") self.assertEquals( m.append(Nothing()), m )
def test_just_functor_law1(self): # fmap id = id x = Just(3) self.assertEquals( x.map(identity), x )
def test_just_functor_law1(self): # fmap id = id x = Just(3) self.assertEqual( x.map(identity), x )
def test_maybe_monoid_just_append_just(self): m = Just("Python") n = Just(" rocks!") self.assertEqual( m + n, Just("Python rocks!") )
def test_just_functor_map(self): f = lambda x: x*2 x = Just(21) self.assertEquals( x.map(f), Just(42) )
def test_just_functor_map(self): f = lambda x: x * 2 x = Just(21) self.assertEqual( x.map(f), Just(42) )
def test_maybe_monoid_just_append_just(self): m = Just("Python") n = Just(" rocks!") self.assertEquals( m.append(n), Just("Python rocks!") )
def test_just_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 = Just(42) self.assertEquals(x.map(compose(f, g)), x.map(g).map(f))
def test_just_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 = Just(42) 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 get_url(search_results): rtn = [] for a in search_results.find_all('a'): rtn.append(a['href']) if len(rtn) == 0: return Nothing else: return Just(rtn)
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 get_result(name_version): theurl = "http://gpo.zugaina.org/Search?search=%s" % name_version thepage = urllib.request.urlopen(theurl) soup = BeautifulSoup(thepage, "lxml") # <div id="search_results"> search_results = soup.find('div', id="search_results") if search_results: return Just(search_results) else: return Nothing
def _set(self, _p, _fp, _d, value): if len(_p) == 1: if isinstance(value, Monad) is True: _d[_p[0]] = value.value return value else: #print("GGG",_p[0], type(_d), _d,value) _d[_p[0]] = value return Just(value) 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._set(_p[1:], _fp, d, value)
def test_maybe_monoid_concat(self): self.assertEqual( Maybe.concat([Just(2), Just(40)]), Just(42) )
def test_combine_just_and_just_rule1(self): self.assertEquals(Just(5) and Just(6), Just(6))
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_just_applicative_3(self): a = Just.pure(lambda x, y: x+y).apply(Just(42)).apply(Nothing()) self.assertEquals(a, Nothing())
def test_just_applicative_3(self): a = Just.pure(lambda x, y: x+y).apply(Just(42)).apply(Nothing()) self.assertEqual(a, Nothing())
def test_combine_just_or_nothing_rule2(self): self.assertEqual( Just(0) or Nothing, Nothing )
def get_category(url_list): if len(url_list) == 1: return Just(url_list[0]) else: return Nothing
def test_combine_just_and_just_rule2(self): self.assertEqual( Just(0) and Just(6), Just(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)
def test_combine_just_or_nothing_rule1(self): self.assertEqual( Just(5) or Nothing, Just(5) )