36template <
typename T,
typename S>
38 std::function<const yield<T>(
S &)> lambda;
49 explicit generatorIter(std::function<
const yield<T>(
S &)> lambda,
const S &state,
bool dummy =
false) : lambda(lambda), state(state), current_yield({
false, {}}) {
60 return current_yield.value;
68 current_yield = lambda(state);
78 return !current_yield.done;
92template <
typename T,
typename S>
95 std::function<const yield<T>(
S &)> lambda;
134 return lambda(state);
144 ->
map<
long>([](
const T &state) {
148 .
reduce(0, std::plus<long>());
158 for (
auto i : *
this) {
171 for (
auto _ : *
this) {
210 template <
typename U>
212 auto lambda = this->lambda;
215 auto item = lambda(state);
235 auto lambda = this->lambda;
238 auto val = lambda(state);
264 auto result = lambda(state);
270 auto value =
result.value;
295 auto lambda = this->lambda;
296 this->lambda = [lambda,
newLambda](
S &state) {
297 auto item = lambda(state);
318 auto lambda = this->lambda;
321 for (
long i = 0;
i < state.count;
i++) {
322 auto item = lambda(state.state);
329 return lambda(state.state);
340 auto lambda = this->lambda;
343 if (state.count <= 0) {
347 auto item = lambda(state.state);
367 template <
typename U,
typename S2>
396 auto item = state.second.next();
429 if (state.second.done) {
433 if (
item1.value != state.second.value) {
438 state.second = state.first.next();
457 auto item = state.next();
482 auto &
gen = state.second;
502using deref_type = std::remove_const_t<std::remove_reference_t<decltype(*std::declval<decltype(std::declval<T>().
begin())>())>>;
539 return generator<deref_type<T>, std::pair<T, iter_type<T>>>({list, list.begin()}, [](std::pair<T, iter_type<T>> &state) {
540 if (state.second != state.first.
end()) {
559 if (state.second < state.first.
length()) {
560 return yield<T>{
false, state.first[state.second++]};
574template <
typename K,
typename V>
594template <
typename K,
typename V>
596 return generator<std::pair<K, V>, std::pair<typename std::map<K, V>::const_iterator, std::map<K, V>>>({map.begin(), map}, [](
auto &state) {
597 if (state.first != state.second.
end()) {
A wrapper for std::vector.
Definition array.hpp:75
void increase(int newSize) noexcept
Increase the space allocated for this array.
Definition array.hpp:192
int push(const T &object) noexcept
Add an object to the array.
Definition array.hpp:242
int length() const noexcept override
Get the length of the array.
Definition array.hpp:1014
T * begin() const noexcept override
Get pointer to the beginning of the array.
Definition array.hpp:616
T * end() const noexcept override
Get pointer to the end of the array.
Definition array.hpp:628
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:1209
Custom iterator for generators to allow for range-based for loops.
Definition generator.hpp:37
bool operator!=(const generatorIter &other) const
Check if the generator can get more data.
Definition generator.hpp:76
generatorIter & operator++()
Generate the next value.
Definition generator.hpp:67
const T & operator*() const
Get the current value from the generator.
Definition generator.hpp:59
generatorIter(std::function< const yield< T >(S &)> lambda, const S &state, bool dummy=false)
Constructor.
Definition generator.hpp:49
An arbitrary generator for producing sequential results on-the-fly.
Definition generator.hpp:93
generator< std::pair< T, U >, generator< U, S2 > > zip(generator< U, S2 > &other)
Zip this generator with another generator.
Definition generator.hpp:368
generator< std::pair< long, T >, std::pair< long, generator< T, S > > > enumerate()
Enumerate the items in this generator.
Definition generator.hpp:394
array< T > collect()
Concatenate all generator elements into an array.
Definition generator.hpp:156
generator< T, countedState > limit(long count)
Limits the number of items that the generator will yield.
Definition generator.hpp:339
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:294
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:234
long count()
Get the total count of items that will be generated.
Definition generator.hpp:142
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:263
long consume()
Consume and discard all items from the generator.
Definition generator.hpp:169
generator< T, countedState > skip(long count)
Skips a certain number of items from the generator.
Definition generator.hpp:317
generator< std::pair< T, yield< T > >, std::pair< yield< T >, generator > > peek()
Allow peeking at the next item in the generator as items are generated.
Definition generator.hpp:479
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:211
generatorIter< T, S > end() const noexcept override
End iterator (end of the range)
Definition generator.hpp:122
yield< T > next()
Get the next item from the generator.
Definition generator.hpp:133
generator< array< T >, generator > chunk(long chunkSize)
Get chunks of items from the generator.
Definition generator.hpp:453
generatorIter< T, S > begin() const noexcept override
Begin iterator (start of the range)
Definition generator.hpp:114
array< T > take(int count)
Take a certain number of items from the generator.
Definition generator.hpp:185
generator< T, std::pair< generator, yield< T > > > diff(generator &other)
List the items in this generator which differ from another generator.
Definition generator.hpp:421
generator(const S &initial, std::function< const yield< T >(S &)> lambda)
Constructor with an initial state.
Definition generator.hpp:108
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:519
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:510
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:502
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