libzed 1.9.9
A general-purpose library for quick and simple data manipulation.
 
Loading...
Searching...
No Matches
array.cpp
#include <iostream>
#include <z/core.hpp>
int main() {
array<zstring> strings = {"123", "abc", "11", "-400"};
// Convert all the string values to integers.
// Note that non-numeric strings all go to zero.
auto numbers = strings.map<int>([](auto item) { return item.integer(); });
// Sum all the numbers
auto total_sum = numbers.reduce(0, [](auto a, auto b) { return a + b; });
// Sum only the non-negative numbers
auto nonneg_sum = numbers.filter([](auto item) { return item >= 0; }).reduce(0, [](auto a, auto b) { return a + b; });
// Print output
("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