An arbitrary generator for producing sequential results on-the-fly. More...
#include <generator.hpp>
Public Member Functions | |
generator (const S &initial, std::function< const yield< T >(S &)> lambda) | |
Constructor with an initial state. | |
generatorIter< T, S > | begin () const noexcept override |
Begin iterator (start of the range) | |
generatorIter< T, S > | end () const noexcept override |
End iterator (end of the range) | |
yield< T > | next () |
Get the next item from the generator. | |
long | count () |
Get the total count of items that will be generated. | |
array< T > | collect () |
Concatenate all generator elements into an array. | |
long | consume () |
Consume and discard all items from the generator. | |
array< T > | take (int count) |
Take a certain number of items from the generator. | |
template<typename U > | |
generator< U, S > | map (std::function< U(const T &)> mapLambda) |
Applies a transformation function to each item that comes out of the generator. | |
generator | filter (std::function< T(const T &)> filterLambda) |
Filters the generatred items based on a predicate and returns a new generator that yields only the items that satisfy the predicate. | |
T | reduce (const T &defaultValue, std::function< T(const T &, const T &)> reduceLambda) |
Reduces the generator to a single value by applying a binary operation cumulatively to all yielded values. | |
generator & | forEach (std::function< void(const T &)> newLambda) |
Binds a function to run each time an item comes out of the generator. | |
generator< T, countedState > | skip (long count) |
Skips a certain number of items from the generator. | |
generator< T, countedState > | limit (long count) |
Limits the number of items that the generator will yield. | |
template<typename U , typename S2 > | |
generator< std::pair< T, U >, generator< U, S2 > > | zip (generator< U, S2 > &other) |
Zip this generator with another generator. | |
generator< std::pair< long, T >, std::pair< long, generator< T, S > > > | enumerate () |
Enumerate the items in this generator. | |
generator< T, std::pair< generator, yield< T > > > | diff (generator &other) |
List the items in this generator which differ from another generator. | |
generator< array< T >, generator > | chunk (long chunkSize) |
Get chunks of items from the generator. | |
generator< long, generator< long, long >::countedState > | skip (long count) |
Template specialization for the skip function with generators that only output longs. | |
![]() | |
virtual | ~iterable () noexcept |
Virtual destructor. | |
An arbitrary generator for producing sequential results on-the-fly.
This class encapsulates generator functionality with any state data the generator may require. Some ideal uses for this class include looping over large sets of data that can't all be loaded at runtime, or loading data in batches while still outputting only a small bit at a time.
T | The type of data that the generator returns. |
S | The state data for the generator. |
|
inline |
Constructor with an initial state.
initial | The initial value of the generator's state. |
lambda | The generator function. |
|
inlineoverridevirtualnoexcept |
Begin iterator (start of the range)
Implements z::core::iterable< generatorIter< T, S > >.
Get chunks of items from the generator.
This function will yield chunks of items from the generator, where each chunk is an array of items of at most the specified size. If the generator runs out of items, the last chunk may contain fewer items.
chunkSize | The size of each chunk. |
Concatenate all generator elements into an array.
Consume and discard all items from the generator.
Get the total count of items that will be generated.
|
inline |
List the items in this generator which differ from another generator.
This function is only useful for generators that yield items of the same type. (if the generators yielded different types, ALL items would be considered different!)
As an example of how this works, if you have two generators that yield strings, generator1 yields "apple", "banana", "cherry", "melon", and generator2 yields "banana", "cherry", "date", "fig", then calling generator1.diff(generator2) will yield "apple" and "melon".
other | The other generator to compare against. |
|
inlineoverridevirtualnoexcept |
End iterator (end of the range)
Implements z::core::iterable< generatorIter< T, S > >.
|
inline |
Enumerate the items in this generator.
This function wraps the existing generator in another generator that yields pairs of indices and items. The first item will have index 0, the second item will have index 1, and so on.
Filters the generatred items based on a predicate and returns a new generator that yields only the items that satisfy the predicate.
This function wraps the existing generator in another generator, and as each item is generated, applies the given lambda function as a predicate, and only yields items that satisfy the predicate.
filterLambda | A function that takes a constant reference to an item of type T and returns a boolean indicating whether the item should be yielded. |
Binds a function to run each time an item comes out of the generator.
This function wraps the existing generator function in another function, effectively binding extra logic to this generator. each item on-the-fly as it's generated.
newLambda | A function that takes a constant reference to an element of type T and returns nothing. |
Limits the number of items that the generator will yield.
count | The maximum number of items to yield. |
Applies a transformation function to each item that comes out of the generator.
This function wraps the existing generator in another generator, effectively transforming each item on-the-fly as it's generated.
U | The type of items that this new generator yields. |
mapLambda | A function that takes a constant reference to an element of type T and returns an element of type U . |
Get the next item from the generator.
If there are no more items, the .done
field of this yield object will be true.
|
inline |
Reduces the generator to a single value by applying a binary operation cumulatively to all yielded values.
This function applies a binary operation (provided as a lambda) to combine all yielded items into a single value. If the generator doesn yield anything, the provided default value is returned.
defaultValue | The value to return if the array is empty. |
reduceLambda | A function that takes two elements of type T and returns their combined result of type T . |
Skips a certain number of items from the generator.
This function will skip a certain number of items from the generator, or all items if there are fewer than the requested count.
count | The number of items to skip. |
generator< long, generator< long, long >::countedState > z::core::generator< long, long >::skip | ( | long | count | ) |
Template specialization for the skip function with generators that only output longs.
count | The number of items to skip. |
Take a certain number of items from the generator.
This function will take a certain number of items from the generator, or all items if there are fewer than the requested count.
count | The number of items to take. |
|
inline |
Zip this generator with another generator.
This function combines two generators into a single generator that yields pairs of items from both generators. If one generator runs out of items, the resulting generator will stop yielding items.
other | The other generator to zip with. |