#include <iostream>
int main() {
array<zstring> strings = {"123", "abc", "11", "-400"};
auto numbers = strings.
map<
int>([](
auto item) {
return item.integer(); });
auto total_sum = numbers.
reduce(0, [](
auto a,
auto b) {
return a + b; });
auto nonneg_sum = numbers.
filter([](
auto item) {
return item >= 0; }).reduce(0, [](
auto a,
auto b) {
return a + b; });
("Number list = ["_zs + join(numbers, ", ") + "]").writeln(std::cout);
("Sum of all numbers = "_zs + total_sum).writeln(std::cout);
("Sum of non-negative numbers = "_zs + nonneg_sum).writeln(std::cout);
}
A wrapper for std::vector.
Definition array.hpp:72
array filter(std::function< bool(const T &)> lambda) const
Filters the array based on a predicate and returns a new array containing the elements that satisfy t...
Definition array.hpp:1218
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
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
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