Links

Categories

Tags


« | Main | »

Co-functions

By Jewe | October 29, 2012

import stdlib;
using stdlib;

cofunction string TipGenerator(string[] tips)
{
    // infinitely do...
    for (;;)
    {
        // loop over the tip array
        for (int i = 0; i < tips.length; i++)
        {
            // return current tip
            yield tips[i];
        }
    }
}

function string main(const string[] args)
{
    TipGenerator nextTip = new TipGenerator({
        "Hello world",
        "This is a tip",
        "And this is another tip!",
        "Quite useful, these tips...",
        "Last tip."
    });

    // print a tip
    println(nextTip());

    // print another tip
    println(nextTip());

    // print some more tips
    for (int i = 0; i < 5; i++)
    {
        println(nextTip());
    }
    
    /* The output will look like this:
        Hello world
        This is a tip
        And this is another tip!
        Quite useful, these tips...
        Last tip.
        Hello world
        This is a tip
    */
    return null;
}

Topics: code examples | Comments Off on Co-functions

Comments are closed.