Exemple #1
0
    def test_control_flow(self):
        self.assertEvalTo(['begin', ['setq','yyyy',9], 2, 'yyyy'], 9)
        self.eval(['dowhile',
            ['setq','yyyy',['+', 'yyyy', 1]],
            ['!=',['%','yyyy',7],0]
            ])
        self.assertEvalTo('yyyy',14)

        self.assertEvalTo(['if', ['==',3,3], 42], 42)
        self.assertEvalTo(['if', ['==',3,4], 42], None)
        self.assertEvalTo(['if', ['==',3,3], 42, 56], 42)
        self.assertEvalTo(['if', ['==',3,4], 42, 56], 56)

        self.assertEvalTo(['cond',
            [['==', 1, 2], q("foo")],
            [['==', 2, 2], q("bar")],
            [True, q("baz")],
            ], "bar")

        self.assertEvalTo(['cond',
            [['==', 1, 2], q("foo")],
            [['==', 2, 3], q("bar")],
            [True, q("baz")],
            ], "baz")

        self.assertEvalTo(['cond',
            [False, q("foo")],
            [False, q("bar")],
            [False, q("baz")],
            ], None)
Exemple #2
0
    def test_high_order_functions(self):
        self.eval(['setq', 'even',
            ['lambda', ['x'], ['==', ['%', 'x', 2], 0]]])

        self.assertEvalTo(['filter',
            ['lambda', ['x'], ['==', ['%', 'x', 2], 0]],
            q([1,2,3,4,5,6,7,8,9,10])], [2,4,6,8,10])

        self.assertEvalTo(['filter',
            'even',
            q([1,2,3,4,5,6,7,8,9,10])], [2,4,6,8,10])

        self.assertEvalTo(['filter',
            lambda ctx,x: x%2==0,
            q([1,2,3,4,5,6,7,8,9,10])], [2,4,6,8,10])

        self.assertEvalTo(['map',
            ['lambda', ['x'], ['+', 'x', 1]],
            q([1,2,3,4,5])], [2,3,4,5,6])

        self.assertEvalTo(['map',
            lambda ctx,x: x+1,
            q([1,2,3,4,5])], [2,3,4,5,6])

        self.assertEvalTo(['reduce',
            ['lambda', ['x','y'], ['+', 'x', 'y']],
            q([1,2,3,4,5])], 15)

        self.assertEvalTo(['reduce',
            '+',
            q([1,2,3,4,5])], 15)

        self.assertEvalTo(['reduce',
            lambda ctx,x,y: x+y,
            q([1,2,3,4,5])], 15)

        self.assertEvalTo(['partition', 'even', q([1,2,3,4,5,6,7,8,9])],
                [[2,4,6,8],[1,3,5,7,9]])
Exemple #3
0
    def test_set(self):
        self.eval(['set', q('blah'), 42])
        self.assertEvalTo("blah", 42)

        self.eval(['set', q('eight'), ["+", 3, 5]])
        self.assertEvalTo("eight", 8)
Exemple #4
0
 def test_func(self):
     self.assertEvalTo(['+', 3, ['-', 9, 7]], 3+(9-7))
     self.assertEvalTo(['+', ["*", 1, 2], ["*",3,4]], 1*2+3*4)
     self.assertEvalTo(['quote', "abc"], "abc")
     self.assertEvalTo(q([1,2,3]), [1,2,3])
Exemple #5
0
 def test_list_processing(self):
     self.assertEvalTo(['apply', '+', q([1,2])], 3)
     self.assertEvalTo(['list', 1,2,q(3),4,5], [1,2,3,4,5])
     self.assertEvalTo(['cons', q("abc"), q([1,2,3])], ["abc",1,2,3])
     self.assertEvalTo(['first', q([1,2,3])], 1)
     self.assertEvalTo(['rest', q([1,2,3])], [2,3])
     self.assertEvalTo(['null', q([])], True)
     self.assertEvalTo(['null', q([1])], False)
     self.assertEvalTo(['append', q([1]), q([2,3,4]), q([5,6])], [1,2,3,4,5,6])
     self.assertEvalTo(['concat', q([[1], [2,3,4], [5,6]])], [1,2,3,4,5,6])