// E03-Enumerate.cpp -- demonstrates the use of Containers::enumerate method

#include <Platform.h>
using namespace Platform;
using namespace Platform::Containers;

// declare a type to be put in a container:
//  this can be any platform value type such as Byte, Int, String etc
typedef Int             MyType;

// declare a container type specialized with MyType declared above:
//  this can be any platform container type implementing GeneralSequenceType
//  or GeneralSetType concepts such as Array, List, HashSet etc
typedef Array<MyType>   MyContainer;


// define a functor that will be used by the enumerate method below
//====================================================================================================
template <typename Type>
struct Sum
{
    // accumulates the sum of values
    Type sum;

    // default constructor, initializes the sum member variable to zero
    Sum():
        sum()
    {}

    // writes the given value to standard output and accumulates the result
    Void operator()(const Type& value)
    {
        // write the value to the standard output
        Runtime::StdOut::println(Lang::toString(value));

        // accumulate the sum of values
        sum += value;
    }
};


// program entry point
//====================================================================================================
AppMain(args)
{
    // define an array of some values
    const Int values[] = { 1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37 };

    // create and initialize a container
    MyContainer container(values, appElementsIn(values));

    // enumerate the values in the container
    //  the enumerate method uses container's Enumerator to iterate over the elements in the
    //  container, calling the Sum functor for each element
    Sum<MyContainer::ElementType> result = enumerate(container, Sum<MyContainer::ElementType>());

    // write the sum to the standard output
    Runtime::StdOut::println(appText("The sum is: ") + Lang::toString(result.sum));

    return 0;
}


/* EOF */