예제 #1
0
    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)
예제 #2
0
파일: test_util.py 프로젝트: cbenz/OSlash
    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)
예제 #3
0
파일: test_list.py 프로젝트: hsavit1/OSlash
    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))
예제 #4
0
    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))
예제 #5
0
    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))
예제 #6
0
    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))
예제 #7
0
    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))
예제 #8
0
    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))
예제 #9
0
파일: test_reader.py 프로젝트: cbenz/OSlash
    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)
        )
예제 #10
0
파일: test_maybe.py 프로젝트: cbenz/OSlash
    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)
        )
예제 #11
0
파일: test_util.py 프로젝트: cbenz/OSlash
 def test_compose_1(self):
     f = lambda x: x*42
     g = compose(f)
     self.assertEqual(420, g(10))
예제 #12
0
 def test_compose_0(self):
     f_id = compose()
     self.assertEqual(42, f_id(42))
예제 #13
0
파일: test_util.py 프로젝트: cbenz/OSlash
 def test_compose_2(self):
     f = lambda x: x*42
     g = lambda y: y+10
     h = compose(g, f)
     self.assertEqual(430, h(10))
예제 #14
0
 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))
예제 #15
0
 def test_compose_2(self):
     f = lambda x: x * 42
     g = lambda y: y + 10
     h = compose(g, f)
     self.assertEqual(430, h(10))
예제 #16
0
 def test_compose_1(self):
     f = lambda x: x * 42
     g = compose(f)
     self.assertEqual(420, g(10))
예제 #17
0
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)
예제 #18
0
파일: test_util.py 프로젝트: cbenz/OSlash
 def test_compose_0(self):
     f_id = compose()
     self.assertEqual(42, f_id(42))
예제 #19
0
파일: test_util.py 프로젝트: cbenz/OSlash
 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))