def test_apply(self): def add(x, y): return x + y self.assertEqual(apply(add, [1, 2])[0], 3) addclosure = interp(["fun", ["x", "y"], ["+", "x", "y"]], topLevelEnv)[0] self.assertEqual(apply(addclosure, [1, 2])[0], 3)
def racket_foldr(function, base, lst): acc = base for elem in lst[::-1]: acc = racython.apply(function, [elem, acc])[0] return acc
def racket_foldl(function, base, lst): return reduce(lambda x, acc: racython.apply(function, [x, acc])[0], lst, base)
def racket_filter(function, lst): return list(filter(lambda x: racython.apply(function, [x])[0], lst))
def racket_map(function, list): return [racython.apply(function, [x])[0] for x in list]
def racket_buildList(num, func): return [racython.apply(func, [x])[0] for x in range(num)]
def test_apply(self): def add(x,y): return x+y self.assertEqual(apply(add, [1, 2])[0], 3) addClosure = eval(["lambda", ["x", "y"], ["+", "x", "y"]], topLevelEnv)[0] self.assertEqual(apply(addClosure, [1, 2])[0], 3)