def test_first(self): lst = ['A', 'B', 'C', 'D'] self.assertTrue(iterators.first(lst) == 'A') gen = range(20, 30) self.assertTrue(iterators.first(gen) == 20) self.assertTrue(iterators.first([], None) == None) self.assertTrue(iterators.first([], 'poo') == 'poo') self.assertRaises(StopIteration, iterators.first, [])
def scan(func, sequence, init=None): if init is None: curr = first(sequence) else: curr = init yield init for element in sequence: curr = func(curr, element) yield curr