|
Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
|
Comprehensive example of functional programming in Aleph-w. More...
#include <iostream>#include <iomanip>#include <string>#include <cmath>#include <tclap/CmdLine.h>#include <tpl_dynArray.H>#include <tpl_dynDlist.H>#include <htlist.H>#include <ahFunctional.H>#include <ahSort.H>Go to the source code of this file.
Functions | |
| template<typename Container > | |
| void | print_container (const string &label, const Container &c) |
| template<typename T1 , typename T2 > | |
| void | print_pairs (const string &label, const DynList< pair< T1, T2 > > &c) |
| void | print_section (const string &title) |
| void | print_subsection (const string &title) |
| void | demo_ranges () |
| void | demo_iteration () |
| void | demo_predicates () |
| void | demo_transformation () |
| void | demo_folding () |
| void | demo_zipping () |
| void | demo_grouping () |
| void | demo_practical () |
| void | demo_comparison () |
| int | main (int argc, char *argv[]) |
Comprehensive example of functional programming in Aleph-w.
This program demonstrates all major functional programming features available in Aleph-w through ahFunctional.H. Functional programming provides a declarative, composable approach to data processing that is often more readable and less error-prone than imperative loops.
Functional programming emphasizes:
Benefits:
Create sequences of values:
range(start, end, step)**: Generate numeric ranges (like Python's range)nrange(start, end, n)**: Generate n evenly spaced valuescontiguous_range(start, n)**: Generate n consecutive valuesrep(n, value)**: Repeat a value n timesExample: range(1, 10, 2) → [1, 3, 5, 7, 9]
Apply operations to containers:
for_each(container, op)**: Apply function to each elementenum_for_each(container, op)**: Apply with index (i, element)traverse(container, op)**: Conditional traversal (can stop early)Test conditions on containers:
all(container, pred)**: All elements satisfy predicate?exists(container, pred)**: At least one satisfies?none(container, pred)**: No element satisfies?contains(container, value)**: Value exists in container?Transform containers into new containers:
maps<T>(container, op)**: Transform each element (like std::transform)filter(container, pred)**: Keep elements satisfying predicateflat_map(container, op)**: Map and flatten nested resultsreverse(container)**: Reverse order of elementsflatten(container)**: Flatten nested containersCombine elements into a single value:
foldl(container, init, op)**: Left fold (reduce from left)foldr(container, init, op)**: Right fold (reduce from right)sum(container)**: Sum all numeric elementsproduct(container)**: Multiply all numeric elementsExample: foldl([1,2,3], 0, +) → 6 (sum)
Combine multiple containers element-wise:
zip(c1, c2)**: Create pairs from two containerszipEq(c1, c2)**: Zip with length equality checkunzip(container)**: Split pairs back into two containerszip_longest(c1, c2, d1, d2)**: Zip with defaults for shorter containerExample: ‘zip([1,2,3], ['a’,'b','c'])‘ → [(1,'a’), (2,'b'), (3,'c')]
Organize elements by criteria:
group_by(container, key_func)**: Group elements by key functionpartition(container, pred)**: Split into two groups (true/false)take_while(container, pred)**: Take prefix satisfying predicatedrop_while(container, pred)**: Drop prefix satisfying predicateImperative style (traditional):
Functional style (Aleph-w):
More concise, readable, and composable!
Functional operations compose naturally:
| Feature | STL | Aleph-w Functional |
|---|---|---|
| Transform | std::transform | maps() |
| Filter | Manual loop | filter() |
| Reduce | std::accumulate | foldl() |
| Composition | Manual chaining | Natural composition |
| Immutability | Modifies input | Returns new container |
Definition in file functional_example.C.
| void demo_comparison | ( | ) |
Definition at line 724 of file functional_example.C.
References Aleph::diff(), Aleph::eq(), Aleph::lesser(), Aleph::maps(), print_container(), print_section(), print_subsection(), and y.
Referenced by main().
| void demo_folding | ( | ) |
Definition at line 449 of file functional_example.C.
References Aleph::concat(), Aleph::DynList< T >::get_first(), Aleph::maps(), print_container(), print_section(), print_subsection(), Aleph::product(), Aleph::reverse(), Aleph::sum(), and Aleph::to_string().
Referenced by main().
| void demo_grouping | ( | ) |
Definition at line 583 of file functional_example.C.
References Aleph::drop_while(), FunctionalMethods< Container, T >::for_each(), Aleph::group_by(), FunctionalMethods< Container, T >::length(), Aleph::maps(), Aleph::partition(), Aleph::prefix(), print_container(), print_section(), print_subsection(), Aleph::suffix(), and Aleph::take_while().
Referenced by main().
| void demo_iteration | ( | ) |
Definition at line 289 of file functional_example.C.
References Aleph::enum_for_each(), FunctionalMethods< Container, T >::for_each(), Aleph::maps(), print_section(), print_subsection(), and GenericTraverse< Container >::traverse().
Referenced by main().
| void demo_practical | ( | ) |
Definition at line 645 of file functional_example.C.
References Aleph::DynList< T >::append(), Aleph::count(), Aleph::filter(), FunctionalMethods< Container, T >::for_each(), Aleph::maps(), Aleph::mean(), Aleph::next(), print_container(), print_section(), print_subsection(), Aleph::range(), Aleph::HTList::size(), sqrt(), Aleph::stddev(), Aleph::sum(), Aleph::tolower(), and Aleph::variance().
Referenced by main().
| void demo_predicates | ( | ) |
Definition at line 328 of file functional_example.C.
References FunctionalMethods< Container, T >::all(), Aleph::contains(), FunctionalMethods< Container, T >::exists(), Aleph::is_even(), is_odd, Aleph::maps(), Aleph::none(), print_container(), print_section(), and print_subsection().
Referenced by main().
| void demo_ranges | ( | ) |
Definition at line 227 of file functional_example.C.
References Aleph::contiguous_range(), FunctionalMethods< Container, T >::for_each(), Aleph::maps(), Aleph::nrange(), print_container(), print_section(), print_subsection(), Aleph::range(), and Aleph::rep().
Referenced by main().
| void demo_transformation | ( | ) |
Definition at line 379 of file functional_example.C.
References Aleph::DynList< T >::append(), FunctionalMethods< Container, T >::filter(), Aleph::flat_map(), Aleph::flatten(), Aleph::maps(), primes, print_container(), print_section(), print_subsection(), Aleph::reverse(), and Aleph::to_string().
Referenced by main().
| void demo_zipping | ( | ) |
Definition at line 516 of file functional_example.C.
References Aleph::DynList< T >::append(), FunctionalMethods< Container, T >::for_each(), l, Aleph::maps(), print_container(), print_pairs(), print_section(), print_subsection(), Aleph::unzip(), and Aleph::zip().
Referenced by main().
| int main | ( | int | argc, |
| char * | argv[] | ||
| ) |
Definition at line 768 of file functional_example.C.
References demo_comparison(), demo_folding(), demo_grouping(), demo_iteration(), demo_practical(), demo_predicates(), demo_ranges(), demo_transformation(), demo_zipping(), and Aleph::maps().
Definition at line 186 of file functional_example.C.
References Aleph::maps().
Referenced by demo_comparison(), demo_folding(), demo_grouping(), demo_practical(), demo_predicates(), demo_ranges(), demo_transformation(), and demo_zipping().
| void print_pairs | ( | const string & | label, |
| const DynList< pair< T1, T2 > > & | c | ||
| ) |
Definition at line 199 of file functional_example.C.
References FunctionalMethods< Container, T >::for_each(), and Aleph::maps().
Referenced by demo_zipping().
| void print_section | ( | const string & | title | ) |
Definition at line 211 of file functional_example.C.
References Aleph::maps().
Referenced by demo_comparison(), demo_folding(), demo_grouping(), demo_iteration(), demo_practical(), demo_predicates(), demo_ranges(), demo_transformation(), and demo_zipping().
| void print_subsection | ( | const string & | title | ) |
Definition at line 218 of file functional_example.C.
References Aleph::maps().
Referenced by demo_comparison(), demo_folding(), demo_grouping(), demo_iteration(), demo_practical(), demo_predicates(), demo_ranges(), demo_transformation(), and demo_zipping().