libzed 1.9.9
A general-purpose library for quick and simple data manipulation.
 
Loading...
Searching...
No Matches
range.cpp
// This is an example of using the z::core::range() generator to generate a list of numbers.
// Ranges are a memory-efficient way of creating and looping over a large range.
// z::core::range(begin = 0, end, step = 1)
#include <iostream>
#include <z/core.hpp>
#include <z/system/sleep.hpp>
#include <z/util/progress.hpp>
int main() {
// You can iterate over ranges just like arrays.
"short range:"_zs.writeln(std::cout);
for (auto i : range(6)) {
(">"_zs + i).writeln(std::cout);
}
// To convert to an array, use the collect() method.
auto list = range(15, 100, 5).collect();
("\narray: "_zs + join(list, ',')).writeln(std::cout);
// Unlike arrays, you can create VERY long ranges with no memory impact.
// An array of 1 trillion elements would take a long time to allocate, but the range is instant.
"\nlong range:"_zs.writeln(std::cout);
for (auto i : range(1'000'000'000)) {
(">"_zs + i).writeln(std::cout);
if (i > 3) {
"Exiting early!"_zs.writeln(std::cout);
break;
}
}
// Skipping elements
// Notice how the range is still instant, even though we're skipping 500,000 elements.
"\nskipping some elements and limiting to 5 total:"_zs.writeln(std::cout);
for (auto i : (range(1'000'000).forEach([](long) { sleep(100); })).skip(500'000).limit(5)) {
(">"_zs + i).writeln(std::cout);
}
// Ranges can even be infinite!
// This will loop forever, so best not to use it with functions that consume all values (e.g. `count()`).
"\ninfinite range (only first 5):"_zs.writeln(std::cout);
for (auto i : range(0, z::core::infinity).limit(5)) {
(">"_zs + i).writeln(std::cout);
}
// A fun feature of generators is that they can have extra functionality bound to them.
// Here, we are taking a long range, and binding a progress bar be updated while we iterate through it.
long max = 10'000'000;
auto numbers = range(max).forEach([max, &progress](auto i) { progress.set(std::cout, i, max); });
// See how here, we're not directly calling progress.set, but the progress bar displays!
"\nSum a long range and display the progress:"_zs.writeln(std::cout);
auto sum = numbers.reduce(0, std::plus<long>());
("Total: "_zs + sum).writeln(std::cout);
return 0;
}
T reduce(const T &defaultValue, std::function< T(const T &, const T &)> lambda) const
Reduces the array to a single value by applying a binary operation cumulatively to the elements.
Definition array.hpp:1232
A class for outputting a progress bar to the terminal.
Definition progress.hpp:15
void set(std::ostream &stream, long item, long max, const zstring &message="", bool force=false) noexcept
Update the progress bar.
generator< long, long > range(long begin, long end, long step=1) noexcept
Generate a sequence of integers in a specified range.
const sentinel infinity
A sentinel that tells numeric generators to run forever.
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
void sleep(double ms) noexcept
Delays program execution temporarily.