170#include <tclap/CmdLine.h>
179using namespace Aleph;
185template <
typename Container>
188 cout << label <<
": [";
190 c.for_each([&first](
const auto& x) {
198template <
typename T1,
typename T2>
201 cout << label <<
": [";
203 c.
for_each([&first](
const auto& p) {
205 cout <<
"(" << p.first <<
", " << p.second <<
")";
213 cout <<
"\n" << string(60,
'=') <<
"\n";
215 cout << string(60,
'=') <<
"\n\n";
234 auto r2 =
range(0, 20, 5);
240 cout <<
" Note: range() only works with positive steps\n";
252 cout <<
"nrange(0.0, 1.0, 5): [";
261 cout <<
"nrange(0.0, 10.0, 11): [";
297 cout <<
"Elements: ";
309 cout <<
"Indexed list:\n";
311 cout <<
" [" << i <<
"] " << name <<
endl;
316 cout <<
"Print until finding 3: ";
341 auto is_even = [](
int x) {
return x % 2 == 0; };
353 auto is_five = [](
int x) {
return x == 5; };
363 auto is_odd = [](
int x) {
return x % 2 != 0; };
391 if (x < 2)
return false;
392 for (
int i = 2; i * i <= x; ++i)
393 if (x % i == 0)
return false;
416 .
filter([](
int x) {
return x % 2 == 0; })
417 .
template maps<int>([](
int x) {
return x * x; });
440 cout <<
"Nested: [[1,2], [3,4,5], [6]]\n";
473 return x > acc ? x : acc;
492 cout <<
"foldl with +: " << left <<
endl;
493 cout <<
" Evaluation: ((\"\" + 1) + 2) + 3" <<
endl;
500 cout <<
"Right-to-left fold: " << right <<
endl;
501 cout <<
" Evaluation: 1 + (2 + (3 + \"\"))" <<
endl;
534 cout <<
"zip(nums, values): [";
538 cout <<
"(" << p.first <<
", " << p.second <<
")";
541 cout <<
"] (stops at shorter)\n";
545 cout <<
"Iterating with ZipIterator:\n";
548 auto [n,
l] = it.get_curr();
569 return p.first + p.second;
572 return p.first * p.second;
611 cout <<
"Grouped by x % 3:\n";
624 DynList<string> words = {
"hi",
"hello",
"bye",
"ok",
"world",
"no",
"yes"};
628 cout <<
"Grouped by length:\n";
630 cout <<
" Length " <<
group.first <<
": [";
634 cout <<
"\"" << s <<
"\"";
683 .
filter([](
const string& s) {
return s.length() > 2; });
709 if (idx == n - 2
or idx == n - 1)
740 cout <<
"eq(a, b)? " << (
eq(a, b) ?
"yes" :
"no") <<
endl;
741 cout <<
"eq(a, c)? " << (
eq(a, c) ?
"yes" :
"no") <<
" (different lengths)" <<
endl;
742 cout <<
"eq(a, d)? " << (
eq(a, d) ?
"yes" :
"no") <<
" (different element)" <<
endl;
746 cout <<
"diff(a, b)? " << (
diff(a, b) ?
"yes" :
"no") <<
endl;
747 cout <<
"diff(a, d)? " << (
diff(a, d) ?
"yes" :
"no") <<
endl;
759 cout <<
"lesser(x, y)? " << (
lesser(x,
y) ?
"yes" :
"no") <<
" (3 < 4)" <<
endl;
761 cout <<
"lesser(z, x)? " << (
lesser(
z, x) ?
"yes" :
"no") <<
" (prefix)" <<
endl;
773 "Comprehensive functional programming example for Aleph-w.\n"
774 "Demonstrates range generation, iteration, predicates, transformation,\n"
775 "folding, zipping, grouping, and more.",
781 "Run only specific section: ranges, iteration, predicates, transform, "
782 "fold, zip, group, practical, compare, or 'all'",
783 false,
"all",
"section",
cmd
791 cout <<
"============================================================\n";
792 cout <<
" ALEPH-W FUNCTIONAL PROGRAMMING EXAMPLE\n";
793 cout <<
"============================================================\n";
822 cout <<
"\n" << string(60,
'=') <<
"\n";
823 cout <<
"Functional programming demo completed!\n";
824 cout << string(60,
'=') <<
"\n\n";
828 catch (TCLAP::ArgException& e)
830 cerr <<
"Error: " << e.error() <<
" for argument " << e.argId() <<
endl;
835 cerr <<
"Error: " << e.what() <<
endl;
Functional programming utilities for Aleph-w containers.
High-level sorting functions for Aleph containers.
Dynamic singly linked list with functional programming support.
T & append(const T &item)
Append a new item by copy.
T & get_first() const
Return the first item of the list.
size_t size() const noexcept
Count the number of elements of the list.
Aleph::DynList< T > filter(Operation &operation) const
Filter the elements of a container according to a matching criteria.
bool exists(Operation &op) const
Test for existence in the container of an element satisfying a criteria.
bool all(Operation &operation) const
Check if all the elements of container satisfy a condition.
size_t length() const noexcept
Count the number of elements of a container.
void for_each(Operation &operation)
Traverse all the container and performs an operation on each element.
Iterator that traverses multiple Aleph containers in lockstep.
void print_subsection(const string &title)
void print_section(const string &title)
void print_pairs(const string &label, const DynList< pair< T1, T2 > > &c)
void print_container(const string &label, const Container &c)
void demo_transformation()
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_sqrt_function > > sqrt(const __gmp_expr< T, U > &expr)
Singly linked list implementations with head-tail access.
Main namespace for Aleph-w library functions.
std::string tolower(const char *str)
Convert a C std::string to lower-case.
DynList< T > flatten(const C2< C1< T > > &c)
Flatten a nested container of one level.
Container< T > nrange(const T start, const T end, const size_t n)
Generate exactly n values evenly spaced between [start, end].
auto unzip(const Container &l)
Separate a list of pairs into two lists.
void reverse(Itor beg, Itor end)
Reverse elements in a range.
bool eq(const C1 &c1, const C2 &c2, Eq e=Eq())
Check equality of two containers using a predicate.
DynList< typename Container::Item_Type > drop_while(const Container &c, Pred pred)
Skip elements while predicate is true (drop_while).
auto variance(const Container &data, bool population=false) -> std::decay_t< decltype(*std::begin(data))>
Compute variance using Welford's numerically stable algorithm.
std::pair< TgtContainer< typename SrcContainer::Item_Type >, TgtContainer< typename SrcContainer::Item_Type > > partition(const SrcContainer &c, std::function< bool(const typename SrcContainer::Item_Type &)> operation)
Partition a container into two based on a predicate.
Container2< typename Container1::Item_Type > filter(Container1 &container, Operation &operation)
Filter elements that satisfy operation.
static void suffix(Node *root, DynList< Node * > &acc)
DynList< typename Container::Item_Type > take_while(const Container &c, Pred pred)
Return elements while predicate is true (take_while).
DynList< std::pair< typename Container1::Item_Type, typename Container2::Item_Type > > zip(const Container1 &a, const Container2 &b)
Zip two containers into a list of pairs.
bool contains(const std::string_view &str, const std::string_view &substr)
Check if substr appears inside str.
auto flat_map(const Container &container, Op op) -> DynList< typename std::decay_t< decltype(op(std::declval< typename Container::Item_Type >()))>::Item_Type >
Apply operation and flatten results (flatMap/concatMap).
static void prefix(Node *root, DynList< Node * > &acc)
auto stddev(const Container &data, bool population=false) -> std::decay_t< decltype(*std::begin(data))>
Compute standard deviation.
auto mean(const Container &data) -> std::decay_t< decltype(*std::begin(data))>
Compute the arithmetic mean.
T product(const Container &container, const T &init=T{1})
Compute product of all elements.
bool diff(const C1 &c1, const C2 &c2, Eq e=Eq())
Check if two containers differ.
std::pair< First, Second > pair
Alias to std::pair kept for backwards compatibility.
bool is_even(const long n)
Return true if n is even.
bool lesser(const C1 &c1, const C2 &c2, Cmp cmp=Cmp())
Lexicographical comparison between two containers.
std::string to_string(const time_t t, const std::string &format)
Format a time_t value into a string using format.
std::string concat(const Args &... args)
Concatenate multiple streamable arguments into a single std::string.
void next()
Advance all underlying iterators (bounds-checked).
Container< T > range(const T start, const T end, const T step=1)
Generate a range of values [start, end] with a given step.
void enum_for_each(const Container &container, Operation &operation)
Apply an operation to each element and its index.
DynList< std::pair< std::invoke_result_t< KeyFunc, const T & >, DynList< T > > > group_by(const Container< T > &c, KeyFunc key_func)
Group consecutive elements by a key function.
Container< T > contiguous_range(T start, const size_t n)
Generate n contiguous values starting from start.
bool none(const Container &container, Operation &operation)
Return true if no element satisfies operation.
DynList< T > maps(const C &c, Op op)
Classic map operation.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
DynList< T > rep(size_t n, const T &item)
Create a sequence of repeated items.
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
bool traverse(Operation &operation) noexcept(traverse_is_noexcept< Operation >())
Traverse the container via its iterator and performs a conditioned operation on each item.
Lazy and scalable dynamic array implementation.
Dynamic doubly linked list implementation.