def test_compose_composition(self): u = lambda x: x * 42 v = lambda x: x + 42 w = 42 a = compose(u, v)(w) b = u(v(w)) self.assertEqual(a, b)
def test_compose_composition(self): u = lambda x: x * 42 v = lambda x: x + 42 w = 42 a = compose(u, v)(w) b = u(v(w)) self.assertEquals(a, b)
def test_list_functor_law2(self): # fmap (f . g) x = fmap f (fmap g x) def f(x): return x + 10 def g(x): return x * 10 # Singleton list x = unit(42) self.assertEquals(x.map(compose(f, g)), x.map(g).map(f)) # Empty list y = List([]) self.assertEquals(y.map(compose(f, g)), y.map(g).map(f)) # Long list z = List(range(42)) self.assertEquals(z.map(compose(f, g)), z.map(g).map(f))
def test_list_functor_law2(self): # fmap (f . g) x = fmap f (fmap g x) def f(x): return x + 10 def g(x): return x * 10 # Singleton list x = unit(42) self.assertEquals(x.map(compose(f, g)), x.map(g).map(f)) # Empty list y = List.empty() self.assertEquals(y.map(compose(f, g)), y.map(g).map(f)) # Long list z = List.from_iterable(range(42)) self.assertEquals(z.map(compose(f, g)), z.map(g).map(f))
def test_cont_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 = unit(42) self.assertEqual(x.map(compose(f, g)), x.map(g).map(f))
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))
def test_state_functor_law2(self) -> None: # fmap (f . g) x = fmap f (fmap g x) def f(x): return x + 10 def g(x): return x * 10 x: State[int, int] = unit(42) self.assertEqual( x.map(compose(f, g)).run(state), x.map(g).map(f).run(state))
def test_reader_functor_law2(self) -> None: # fmap (f . g) x = fmap f (fmap g x) def f(x): return x + 10 def g(x): return x * 10 x = unit(42) self.assertEqual( x.map(compose(f, g)).run(env), x.map(g).map(f).run(env))
def test_reader_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 = unit(42) self.assertEquals( x.map(compose(f, g)), x.map(g).map(f) )
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) )
def test_compose_1(self): f = lambda x: x*42 g = compose(f) self.assertEqual(420, g(10))
def test_compose_0(self): f_id = compose() self.assertEqual(42, f_id(42))
def test_compose_2(self): f = lambda x: x*42 g = lambda y: y+10 h = compose(g, f) self.assertEqual(430, h(10))
def test_compose_3(self): f = lambda x: x * 42 g = lambda y: y + 10 h = lambda z: z / 2 i = compose(h, g, f) self.assertEqual(215, i(10))
def test_compose_2(self): f = lambda x: x * 42 g = lambda y: y + 10 h = compose(g, f) self.assertEqual(430, h(10))
def test_compose_1(self): f = lambda x: x * 42 g = compose(f) self.assertEqual(420, g(10))
def build_dict(formatted_records: Iterable[Dict[str, str]], ) -> dict: keys = next(formatted_records).keys() if len(keys) > 2: raise ValueError('More than two keys given.') result = {} for record in formatted_records: k1, k2 = keys result[record[k1]] = record[k2] return result split = partial(methodcaller, 'split') split_newlines, split_commas = split('\n'), split(',') build_records = compose(dict, zip) build_rows = compose(map(split_commas), split_newlines, IOService.handle_outcome, IOService.read_file) def produce_records(csvfile: str) -> Iterable[Dict[str, str]]: rows = build_rows(csvfile) headers = next(rows) for record in map(partial(build_records, headers), rows): yield record def handle_csv(csvfile: str, *columns_and_formatters: Tuple[str, Callable[..., Any]]): rows = build_rows(csvfile) headers = next(rows)
def test_compose_3(self): f = lambda x: x*42 g = lambda y: y+10 h = lambda z: z/2 i = compose(h, g, f) self.assertEqual(215, i(10))