28template <
typename T,
typename S>
30 std::function<const yield<T>(
S &)> lambda;
41 explicit generatorIter(std::function<
const yield<T>(
S &)> lambda,
const S &state,
bool dummy =
false) : lambda(lambda), state(state), current_yield({
false, {}}) {
52 return current_yield.value;
60 current_yield = lambda(state);
70 return !current_yield.done;
84template <
typename T,
typename S>
87 std::function<const yield<T>(
S &)> lambda;
126 return lambda(state);
136 ->
map<
long>([](
const T &state) {
140 .
reduce(0, std::plus<long>());
150 for (
auto i : *
this) {
163 for (
auto _ : *
this) {
202 template <
typename U>
204 auto lambda = this->lambda;
207 auto item = lambda(state);
227 auto lambda = this->lambda;
230 auto val = lambda(state);
256 auto result = lambda(state);
262 auto value =
result.value;
287 auto lambda = this->lambda;
288 this->lambda = [lambda,
newLambda](
S &state) {
289 auto item = lambda(state);
310 auto lambda = this->lambda;
313 for (
long i = 0;
i < state.count;
i++) {
314 auto item = lambda(state.state);
321 return lambda(state.state);
332 auto lambda = this->lambda;
335 if (state.count <= 0) {
339 auto item = lambda(state.state);
359 template <
typename U,
typename S2>
388 auto item = state.second.next();
421 if (state.second.done) {
425 if (
item1.value != state.second.value) {
430 state.second = state.first.next();
449 auto item = state.next();
469using deref_type = std::remove_const_t<std::remove_reference_t<decltype(*std::declval<decltype(std::declval<T>().
begin())>())>>;
506 return generator<deref_type<T>, std::pair<T, iter_type<T>>>({list, list.begin()}, [](std::pair<T, iter_type<T>> &state) {
507 if (state.second != state.first.
end()) {
526 if (state.second < state.first.
length()) {
527 return yield<T>{
false, state.first[state.second++]};
541template <
typename K,
typename V>
561template <
typename K,
typename V>
563 return generator<std::pair<K, V>, std::pair<typename std::map<K, V>::const_iterator, std::map<K, V>>>({map.begin(), map}, [](
auto &state) {
564 if (state.first != state.second.
end()) {
A wrapper for std::vector.
Definition array.hpp:72
void increase(int newSize) noexcept
Increase the space allocated for this array.
Definition array.hpp:189
int push(const T &object) noexcept
Add an object to the array.
Definition array.hpp:239
int length() const noexcept override
Get the length of the array.
Definition array.hpp:1011
T * begin() const noexcept override
Get pointer to the beginning of the array.
Definition array.hpp:613
T * end() const noexcept override
Get pointer to the end of the array.
Definition array.hpp:625
array< U > map(std::function< U(const T &)> lambda) const
Applies a transformation function to each element of the array and returns a new array with the resul...
Definition array.hpp:1206
Custom iterator for generators to allow for range-based for loops.
Definition generator.hpp:29
bool operator!=(const generatorIter &other) const
Check if the generator can get more data.
Definition generator.hpp:68
generatorIter & operator++()
Generate the next value.
Definition generator.hpp:59
const T & operator*() const
Get the current value from the generator.
Definition generator.hpp:51
generatorIter(std::function< const yield< T >(S &)> lambda, const S &state, bool dummy=false)
Constructor.
Definition generator.hpp:41
An arbitrary generator for producing sequential results on-the-fly.
Definition generator.hpp:85
generator< std::pair< T, U >, generator< U, S2 > > zip(generator< U, S2 > &other)
Zip this generator with another generator.
Definition generator.hpp:360
generator< std::pair< long, T >, std::pair< long, generator< T, S > > > enumerate()
Enumerate the items in this generator.
Definition generator.hpp:386
array< T > collect()
Concatenate all generator elements into an array.
Definition generator.hpp:148
generator< T, countedState > limit(long count)
Limits the number of items that the generator will yield.
Definition generator.hpp:331
generator & forEach(std::function< void(const T &)> newLambda)
Binds a function to run each time an item comes out of the generator.
Definition generator.hpp:286
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 it...
Definition generator.hpp:226
long count()
Get the total count of items that will be generated.
Definition generator.hpp:134
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 va...
Definition generator.hpp:255
long consume()
Consume and discard all items from the generator.
Definition generator.hpp:161
generator< T, countedState > skip(long count)
Skips a certain number of items from the generator.
Definition generator.hpp:309
generator< U, S > map(std::function< U(const T &)> mapLambda)
Applies a transformation function to each item that comes out of the generator.
Definition generator.hpp:203
generatorIter< T, S > end() const noexcept override
End iterator (end of the range)
Definition generator.hpp:114
yield< T > next()
Get the next item from the generator.
Definition generator.hpp:125
generator< array< T >, generator > chunk(long chunkSize)
Get chunks of items from the generator.
Definition generator.hpp:445
generatorIter< T, S > begin() const noexcept override
Begin iterator (start of the range)
Definition generator.hpp:106
array< T > take(int count)
Take a certain number of items from the generator.
Definition generator.hpp:177
generator< T, std::pair< generator, yield< T > > > diff(generator &other)
List the items in this generator which differ from another generator.
Definition generator.hpp:413
generator(const S &initial, std::function< const yield< T >(S &)> lambda)
Constructor with an initial state.
Definition generator.hpp:100
A base interface for all objects that can be iterated over.
Definition iterable.hpp:10
generator< deref_type< T >, iter_type< T > > generatorFrom(const T &list)
Create a generator from an arbitrary iterable.
Definition generator.hpp:486
std::remove_const_t< decltype(std::declval< T >().begin())> iter_type
The type of the iterator for an iterable. This is used to determine the type of iterator that the gen...
Definition generator.hpp:477
std::remove_const_t< std::remove_reference_t< decltype(*std::declval< decltype(std::declval< T >().begin())>())> > deref_type
The type of the dereferenced value from an iterable. This is used to determine the type of value that...
Definition generator.hpp:469
The return value for generator functions.
Definition generator.hpp:19
T value
The next value that the generator will return.
Definition generator.hpp:24
bool done
Whether the generator has run out of values to return.
Definition generator.hpp:21