libzed 1.10.2
A general-purpose library for quick and simple data manipulation.
 
Loading...
Searching...
No Matches
generatorChunking.cpp
// This example shows off generators' ability to un-chunk chunked data.
#include <iostream>
#include <z/all.hpp>
int main() {
std::cout << "Chunking a generator:" << std::endl;
for (auto chunk : range(10).chunk(5)) {
std::cout << '[' << join(chunk, ",") << ']' << std::endl;
}
std::cout << "Unchunking from a chunked generator:" << std::endl;
auto gen = range(10).chunk(5);
for (auto item : gen.flatten()) {
std::cout << item << ", ";
}
std::cout << std::endl;
// Technically, the generator doesn't need to have been chunked
// in order to be flattened; it just needs to yield objects with an iterator.
// In this example, the yield from notChunked would be pairs of numbers,
// but we can call `flatten()` to just yield individual numbers from the list.
std::vector<std::vector<int>> listOfLists{{0, 1}, {2, 3}, {4, 5}, {6, 7}, {8, 9}};
auto notChunked = generatorFrom(listOfLists);
std::cout << "Unchunking from an arbitrary iterator:" << std::endl;
for (auto item : notChunked.flatten()) {
std::cout << item << ", ";
}
std::cout << std::endl;
}
generator< dereference< T >, const_iterator_value< T > > generatorFrom(const T &list)
Create a generator from an arbitrary iterable.
Definition generator.hpp:587
generator< long, long > range(long begin, long end, long step=1) noexcept
Generate a sequence of integers in a specified range.
string< E > join(const iterable< T > &list, const string< E > &delim) noexcept
Concatenate elements in an array into a string, separated by a delimiter.
Definition join.hpp:20