def test_chain(self): add_one = fun.map(lambda x: x + "1") upper = fun.map(str.upper) result = fun.chain( ["a", "b", "c", "d"], add_one, upper, list ) self.assertEqual(result, ["A1", "B1", "C1", "D1"]) big_transform = [add_one, upper] result = fun.chain( ["a", "b", "c", "d"], *big_transform, list ) self.assertEqual(result, ["A1", "B1", "C1", "D1"]) result = fun.chain( ["a", "b", "c", "d"], *big_transform, fun.filter(lambda x: x < "C1"), list ) self.assertEqual(result, ["A1", "B1"]) result = fun.chain( ["a", "b", "c", "d"], *big_transform, fun.sort(reverse=True), list ) self.assertEqual(result, ["D1", "C1", "B1", "A1"]) side_effects = [] save_value = fun.tap(side_effects.append) result = fun.chain( ["a", "b", "c", "d"], save_value, *big_transform, save_value, fun.sort(reverse=True), list ) self.assertEqual(result, ["D1", "C1", "B1", "A1"]) self.assertEqual(side_effects, [ ["a", "b", "c", "d"], ["A1", "B1", "C1", "D1"] ]) result = fun.chain( ["a", "b", "c", "d"], *big_transform, fun.reduce(lambda acc, x: acc + x), ) self.assertEqual(result, "A1B1C1D1")
def test_boop(self): data = { 'beep': 1, 'boop': 2, 'buup': 3, } add_one = fun.map(lambda k, v: (k, v + 1)) evens = fun.filter(lambda k, v: v % 2 == 0) beep_buup = fun.reduce(lambda acc, k, v: f'{acc}{k}{v}', '') result = fun.chain(data.items(), add_one, evens, add_one, beep_buup) self.assertEqual(result, 'beep3buup5')
def test_filter_with_tuples(self): filterer = fun.filter(lambda k, v: k == 'two' and v == 2) result = filterer([('one', 1), ('two', 2)]) self.assertEqual(list(result), [('two', 2)])
def test_filter(self): filterer = fun.filter(lambda x: x > 2) result = filterer([1, 2, 3, 4]) self.assertEqual(list(result), [3, 4])
def test_chain(self): result = fun.chain(self.data.items(), fun.map(TestDicts.move_north), fun.tap_each(self.log_y_pos), fun.filter(TestDicts.above_equator), dict) self.assertEqual(result, {"rover2": {"pos": (2, 4), "dir": "W"}})
def test_filter(self): mapper = fun.filter(TestDicts.above_equator) result = mapper(self.data.items()) self.assertEqual(dict(result), {"rover2": {"pos": (2, 3), "dir": "W"}})