Classical FP problem in python : Hamming problem

Nick Craig-Wood nick at craig-wood.com
Tue Jan 25 09:30:03 CET 2005


Francis Girard <francis.girard at free.fr> wrote:
>  def hamming():
>    def _hamming():
>      yield 1
>      hamming2 = hammingGenerators[0]
>      hamming3 = hammingGenerators[1]
>      hamming5 = hammingGenerators[2]
>      for n in imerge(imap(lambda h: 2*h, iter(hamming2)),
>                      imerge(imap(lambda h: 3*h, iter(hamming3)),
>                             imap(lambda h: 5*h, iter(hamming5)))):
>        yield n
>    hammingGenerators = tee(_hamming(), 4)
>    return hammingGenerators[3]

If you are after readability, you might prefer this...

def hamming():
  def _hamming():
    yield 1
    for n in imerge(imap(lambda h: 2*h, iter(hamming2)),
                    imerge(imap(lambda h: 3*h, iter(hamming3)),
                           imap(lambda h: 5*h, iter(hamming5)))):
      yield n
  hamming2, hamming3, hamming5, result = tee(_hamming(), 4)
  return result

PS interesting thread - never heard of Hamming sequences before!
-- 
Nick Craig-Wood <nick at craig-wood.com> -- http://www.craig-wood.com/nick



More information about the Python-list mailing list