Пример #1
0
    def test_thens(arg):
        def repeat_word(word, times, upper=False):
            if upper:
                word = word.upper()

            return [word] * times

        f = P[::-1] >> Then(repeat_word, 3)
        g = P[::-1] >> Then(repeat_word, 3, upper=True)

        assert f("ward") == ["draw", "draw", "draw"]
        assert g("ward") == ["DRAW", "DRAW", "DRAW"]

        ###########################

        # since map and filter receive the iterable on their second argument, you have to use `Then2`
        f = Then2(filter, P % 2 == 0) >> Then2(
            map, P**2
        ) >> list  #lambda x: map(lambda z: z**2, filter(lambda z: z % 2 == 0, x))

        assert f([1, 2, 3, 4, 5]) == [4, 16]  #[2**2, 4**2] == [4, 16]

        ######################################

        f = P.filter(P % 2 == 0) >> P.map(
            P**2
        ) >> list  #lambda x: map(lambda z: z**2, filter(lambda z: z % 2 == 0, x))

        assert f([1, 2, 3, 4, 5]) == [4, 16]  #[2**2, 4**2] == [4, 16]
Пример #2
0
    def test_contains(self):

        from phi import P

        assert False == P.Pipe(
            [1, 2, 3, 4],
            P.filter(P % 2 != 0)  #[1, 3], keeps odds
            .Contains(4)  #4 in [1, 3] == False
        )