#include <iostream>
#include <z/system/sleep.hpp>
#include <z/util/progress.hpp>
int main() {
"short range:"_zs.writeln(std::cout);
for (auto i : range(6)) {
(">"_zs + i).writeln(std::cout);
}
auto list = range(15, 100, 5).collect();
("\narray: "_zs + join(list, ',')).writeln(std::cout);
"\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;
}
}
"\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);
}
"\ninfinite range (only first 5):"_zs.writeln(std::cout);
(">"_zs + i).writeln(std::cout);
}
long max = 10'000'000;
auto numbers = range(max).forEach([max, &progress](
auto i) { progress.
set(std::cout, i, max); });
"\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.