Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
Generator.h File Reference
#include "Stroika/Foundation/StroikaPreComp.h"
#include "Stroika/Foundation/Traversal/Iterable.h"
#include "Stroika/Foundation/Traversal/Iterator.h"
#include "Generator.inl"

Go to the source code of this file.

Namespaces

namespace  Stroika::Foundation
 

Functions

template<typename T >
Iterator< T > Stroika::Foundation::Traversal::CreateGeneratorIterator (const function< optional< T >()> &getNext)
 
template<typename T >
Iterable< T > Stroika::Foundation::Traversal::CreateGenerator (const function< optional< T >()> &getNext)
 Create an Iterable<T> from a function that returns optional<T> - treating nullopt as meaning the END of iteration.
 

Detailed Description

Note
Code-Status: Alpha

TODO:

    "Writeup details on how todo generator � as lambda-from-iteator � like I did for
    revision with queie and lambdas � except we have we construct new object with
    updstream-get-lambda and pass it to downstream one. I think that works. Try draft�."

    I'm now not quite sure what that means. This may have been more for the FunctionApplicaiton module?
    But keep for a little bit to see if it makes sense when I review this code later...

    -- LGP 2013-10-14

Definition in file Generator.h.

Function Documentation

◆ CreateGeneratorIterator()

template<typename T >
Iterator< T > Stroika::Foundation::Traversal::CreateGeneratorIterator ( const function< optional< T >()> &  getNext)

Note - if you need to maintain context for the iterator (typically yes) - bind it into the std::function lambda closure (with smart pointers).

Definition at line 14 of file Generator.inl.

◆ CreateGenerator()

template<typename T >
Iterable< T > Stroika::Foundation::Traversal::CreateGenerator ( const function< optional< T >()> &  getNext)

Create an Iterable<T> from a function that returns optional<T> - treating nullopt as meaning the END of iteration.

Note - if you need to maintain context for the iterator (typically yes) - bind it into the std::function lambda closure (with smart pointers).

Example Usage
constexpr int kMin = 1;
constexpr int kMax = 10;
auto myContext = make_shared<int> (kMin - 1);
auto getNext = [myContext] () -> optional<int> {
++(*myContext);
if (*myContext > 10)
{
return nullopt;
}
return *myContext;
};
int sum = 0;
for (auto i : CreateGenerator<int> (getNext)) {
EXPECT_TRUE (1 <= i and i <= 10);
sum += i;
}
EXPECT_TRUE (sum == (kMax - kMin + 1) * (kMax + kMin) / 2);
Iterable< T > CreateGenerator(const function< optional< T >()> &getNext)
Create an Iterable<T> from a function that returns optional<T> - treating nullopt as meaning the END ...
Definition Generator.inl:55

Definition at line 55 of file Generator.inl.