Merge of lazy streams (using generators) in Python
I'm playing with functional capacities of Python 3 and I tried to implement classical algorithm for calculating Hamming numbers. That's the numbers which have as prime factors only 2, 3 or 5. First Hamming numbers are 2, 3, 4, 5, 6, 8, 10, 12, 15, 16, 18, 20 and so on. My implementation is the following: def scale(s, m): return (x*m for x in s) def merge(s1, s2): it1, it2 = iter(s1), iter(s2) x1, x2 = next(it1), next(it2) if x1 < x2: x = x1 it = iter(merge(it1, s2)) elif x1 > x2: x = x2 it = iter(merge(s1, it2)) else: x = x1 it = iter(merge(it1, it2)) yield x while True: yield next(it) def