Пример #1
0
    def test_others(self):
        f = Obj.split(' ') >> P.map(len) >> sum >> If(
            (P < 15).Not(), "Great! Got {0} letters!".format).Else(
                "Too short, need at-least 15 letters")

        assert f("short frase") == "Too short, need at-least 15 letters"
        assert f("some longer frase") == "Great! Got 15 letters!"
Пример #2
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]
Пример #3
0
    def test_random(self):

        assert 9 == P.Pipe(
            "Hola Cesar",
            P.Obj.split(" "),
            P.map(len)
            .sum()
        )
Пример #4
0
    def test_example_1(self):

        text = "a bb ccc"
        avg_word_length = P.Pipe(
            text,
            Obj.split(" "),  #['a', 'bb', 'ccc']
            P.map(len),  #[1, 2, 3]
            P._(sum) / len  #6 / 3 == 2
        )

        assert 2 == avg_word_length

        text = "a bb ccc"
        avg_word_length = P.Pipe(
            text,
            Obj.split(" "),  #['a', 'bb', 'ccc']
            P.map(len),  #[1, 2, 3]
            M(sum) / len  #6 / 3 == 2
        )

        assert 2 == avg_word_length

        text = "a bb ccc"
        avg_word_length = P.Pipe(
            text,
            Obj.split(" "),  #['a', 'bb', 'ccc']
            P.map(len),  #[1, 2, 3]
            P.sum() / P.len()  #6 / 3 == 2
        )

        assert 2 == avg_word_length

        avg_word_length = P.Pipe(
            "1 22 333",
            Obj.split(' '),  # ['1', '22', '333']
            P.map(len),  # [1, 2, 3]
            [
                sum  # 1 + 2 + 3 == 6
                ,
                len  # len([1, 2, 3]) == 3
            ],
            P[0] / P[1]  # sum / len == 6 / 3 == 2
        )

        assert avg_word_length == 2
Пример #5
0
    def test_fluent(self):

        f = Dict(x=2 * P, y=P + 1).Tuple(Rec.x + Rec.y, Rec.y / Rec.x)

        assert f(1) == (4, 1)  # ( x + y, y / x) == ( 2 + 2, 2 / 2) == ( 4, 1 )

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

        f = Obj.split(' ') >> P.map(len) >> sum >> If(
            (P < 15).Not(), "Great! Got {0} letters!".format).Else(
                "Too short, need at-least 15 letters")

        assert f("short frase") == "Too short, need at-least 15 letters"
        assert f("some longer frase") == "Great! Got 15 letters!"

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

        f = (Obj.split(' ').map(len).sum().If(
            (P < 15).Not(), "Great! Got {0} letters!".format).Else(
                "Too short, need at-least 15 letters"))

        assert f("short frase") == "Too short, need at-least 15 letters"
        assert f("some longer frase") == "Great! Got 15 letters!"
Пример #6
0
    def test_math(self):
        import math

        f = P.map(P**2) >> list >> P[0] + P[1] >> math.sqrt

        assert f([3, 4]) == 5