www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Another little test

To remove possible bugs from Phobos2 and improve its flexibility and make it as
handy as possible, it's necessary to try to use it to do a bit "complex" things
too. This is a tiny Python program that may be useful for that (but see
references at the bottom):

from operator import add
from itertools import tee, chain, islice, imap

def fibonacci():
    def deferred_output():
        for i in output:
            yield i

    result, c1, c2 = tee(deferred_output(), 3)
    paired = imap(add, c1, islice(c2, 1, None))
    output = chain([0, 1], paired)
    return result

print list(islice(fibonacci(), 50))

Is it possible to implement that code with Phobos2 in a handy enough way?

Another similar but a little more complex function is the hamming_numbers()
function here:
http://code.activestate.com/recipes/576961/
(There's no need to use multiprecision integers in a first D2 implementation).
It's an implementation of ideas common in Haskell:
http://en.wikipedia.org/wiki/Corecursion

Bye,
bearophile
Dec 16 2009