63 cout << label <<
": {";
64 for (
size_t i = 0; i < v.size(); ++i)
67 if (i < v.size() - 1)
cout <<
", ";
73template <
typename T1,
typename T2>
76 cout << label <<
": {";
77 for (
size_t i = 0; i < v.size(); ++i)
79 cout <<
"(" << v[i].first <<
", " << v[i].second <<
")";
80 if (i < v.size() - 1)
cout <<
", ";
90 for (
const auto &
inner : v)
104 cout <<
"========================================" <<
endl;
105 cout <<
" ah-stl-functional.H Usage Examples" <<
endl;
106 cout <<
"========================================" <<
endl <<
endl;
111 cout <<
"--- 1. Range Generation ---" <<
endl;
123 cout <<
"stl_linspace(0.0, 1.0, 5): {";
124 for (
size_t i = 0; i <
lin.
size(); ++i)
142 cout <<
"--- 2. Map and Transform ---" <<
endl;
144 vector<int>
nums = {1, 2, 3, 4, 5};
163 cout <<
"--- 3. Filter and Reject ---" <<
endl;
165 vector<int> data = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
182 cout <<
"--- 4. Fold (Reduce) Operations ---" <<
endl;
184 vector<int> v = {1, 2, 3, 4, 5};
206 cout <<
"--- 5. Predicates ---" <<
endl;
208 vector<int>
all_even = {2, 4, 6, 8};
210 vector<int>
no_even = {1, 3, 5, 7};
212 auto is_even = [](
int x) {
return x % 2 == 0; };
214 cout <<
"all_even = {2, 4, 6, 8}" <<
endl;
219 cout <<
"no_even = {1, 3, 5, 7}" <<
endl;
229 cout <<
"--- 6. Finding Elements ---" <<
endl;
231 vector<int>
find_data = {10, 20, 30, 40, 50};
241 cout <<
"stl_find_index(x == 30): " << (idx ?
to_string(*idx) :
"not found") <<
endl;
251 cout <<
"--- 7. Counting ---" <<
endl;
253 vector<int>
count_data = {1, 2, 2, 3, 3, 3, 4, 4, 4, 4};
264 cout <<
"--- 8. Take and Drop ---" <<
endl;
266 vector<int>
td = {1, 2, 3, 4, 5, 6, 7, 8};
280 cout <<
"--- 9. Accessing Elements ---" <<
endl;
282 vector<int> access = {10, 20, 30, 40, 50};
299 cout <<
"--- 10. Min, Max, Sum, Product ---" <<
endl;
301 vector<int>
mm = {3, 1, 4, 1, 5, 9, 2, 6};
324 cout <<
"--- 11. Partition ---" <<
endl;
326 vector<int>
part = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
338 cout <<
"--- 12. Zip and Enumerate ---" <<
endl;
340 vector<int> keys = {1, 2, 3};
361 cout <<
"--- 13. Reverse and Sort ---" <<
endl;
363 vector<int>
unsorted = {3, 1, 4, 1, 5, 9, 2, 6};
375 cout <<
"--- 14. Unique and Distinct ---" <<
endl;
377 vector<int>
with_dups = {1, 1, 2, 2, 2, 3, 3, 1, 1};
388 cout <<
"--- 15. Concat and Flatten ---" <<
endl;
390 vector<int> a = {1, 2, 3};
391 vector<int> b = {4, 5, 6};
401 return vector<int>{x, x * 10};
410 cout <<
"--- 16. Grouping ---" <<
endl;
412 vector<int>
to_group = {1, 1, 2, 2, 2, 3, 1};
416 cout <<
"stl_group (consecutive):" <<
endl;
427 cout <<
"stl_group_by(first char):" <<
endl;
430 cout <<
" '" << key <<
"': ";
439 cout <<
"--- 17. Tally (Frequency Count) ---" <<
endl;
454 cout <<
"--- 18. Sliding Window and Chunks ---" <<
endl;
456 vector<int>
seq = {1, 2, 3, 4, 5};
470 cout <<
"--- 19. Intersperse, Split, Span ---" <<
endl;
477 vector<int>
to_split = {1, 2, 3, 4, 5};
491 cout <<
"--- 20. Init and Tail ---" <<
endl;
493 vector<int> it = {1, 2, 3, 4, 5};
504 cout <<
"--- 21. Combinatorics ---" <<
endl;
511 for (
const auto & p :
perms)
519 for (
const auto & c :
combos)
527 for (
const auto & arr :
arrs)
538 cout <<
"--- 22. Cartesian Product and Power Set ---" <<
endl;
541 cout <<
"Sets: {{1, 2}, {3, 4}}" <<
endl;
546 vector<int>
ps_set = {1, 2, 3};
551 for (
const auto & s :
power)
554 for (
size_t i = 0; i < s.size(); ++i)
557 if (i < s.size() - 1)
cout <<
", ";
567 cout <<
"--- 23. Works with Different STL Container Types ---" <<
endl;
571 cout <<
"std::list<int>: {1, 2, 3, 4, 5}" <<
endl;
583 cout <<
"std::set<int>: {5, 2, 8, 1, 9, 3} -> ordered: {";
606 cout <<
"--- 23b. Aleph Containers (DynList, DynSetTree) ---" <<
endl;
610 cout <<
"DynList<int>: {10, 20, 30, 40, 50}" <<
endl;
631 cout <<
"DynSetTree<int>: {15, 5, 25, 10, 20} -> in-order: {";
636 cout << it.get_curr();
656 cout <<
"--- 23c. Mixing STL and Aleph Results ---" <<
endl;
659 vector<int>
vec_a = {1, 2, 3, 4, 5};
663 cout <<
"vector: {1, 2, 3, 4, 5}" <<
endl;
664 cout <<
"set: {10, 20, 30, 40, 50}" <<
endl;
665 cout <<
"DynList: {100, 200, 300, 400, 500}" <<
endl;
690 cout <<
"--- 23d. Unified API with uni_*: DynList, set, vector, DynSetTree ---" <<
endl;
693 vector<int>
uni_vec = {10, 5, 8, 3, 15, 7, 12};
696 for (
int x : {10, 5, 8, 3, 15, 7, 12})
700 for (
int x : {10, 5, 8, 3, 15, 7, 12})
703 cout <<
"All containers initialized with: {10, 5, 8, 3, 15, 7, 12}" <<
endl;
704 cout <<
" vector preserves insertion order" <<
endl;
705 cout <<
" set sorts and removes duplicates" <<
endl;
706 cout <<
" DynList preserves insertion order" <<
endl;
707 cout <<
" DynSetTree sorts and stores unique values" <<
endl;
712 auto square = [](
int x) {
return x * x; };
720 cout <<
"After uni_filter(x > 7):" <<
endl;
733 cout <<
"After uni_map(x -> x²) on filtered results:" <<
endl;
747 cout <<
"After uni_foldl(0, +) on squared results:" <<
endl;
756 auto has_large = [](
int x) {
return x > 100; };
758 cout <<
"Predicate tests using uni_all and uni_exists:" <<
endl;
771 cout <<
"Min/Max operations:" <<
endl;
784 cout <<
"Take/Drop operations (first 3 elements):" <<
endl;
797 cout <<
"Cross-container comparisons:" <<
endl;
804 cout <<
"Unified pipeline: filter -> map -> take -> sum" <<
endl;
805 cout <<
" Applied identically to all four container types" <<
endl;
807 auto pipeline = [](
const auto & container) {
820 cout <<
"Key insight: uni_* functions provide a single API that works" <<
endl;
821 cout <<
"seamlessly with both STL containers (vector, set) and Aleph" <<
endl;
822 cout <<
"containers (DynList, DynSetTree) without any code changes!" <<
endl;
829 cout <<
"--- 24. Function Composition Example ---" <<
endl;
831 vector<int>
raw = {1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5};
840 cout <<
"Pipeline: distinct -> filter(even) -> map(square) -> sum" <<
endl;
847 cout <<
"========================================" <<
endl;
848 cout <<
" All examples completed successfully!" <<
endl;
849 cout <<
"========================================" <<
endl;
Functional programming utilities for C++ Standard Library containers.
Unified functional programming utilities for both STL and Aleph containers.
void print_vec(const string &label, const vector< T > &v)
void print_nested(const string &label, const vector< vector< T > > &v)
void print_pairs(const string &label, const vector< pair< T1, T2 > > &v)
Dynamic singly linked list with functional programming support.
T & insert(const T &item)
Insert a new item by copy.
T & append(const T &item)
Append a new item by copy.
Dynamic set backed by balanced binary search trees with automatic memory management.
size_t size() const noexcept
Count the number of elements of the list.
auto get_it() const
Return a properly initialized iterator positioned at the first item on the container.
Singly linked list implementations with head-tail access.
Main namespace for Aleph-w library functions.
auto stl_first(const Container &c)
Get first element.
std::vector< T > stl_scan_left(T init, Op &&op, const Container &c)
Scan left - fold with all intermediate results.
auto uni_filter(Pred &&pred, const Container &c)
Filter elements satisfying predicate.
auto stl_span(Pred &&pred, const Container &c)
Split at predicate boundary (span in Haskell).
auto stl_take_last(size_t n, const Container &c)
Take last n elements.
auto stl_chunks(size_t n, const Container &c)
Split container into chunks of size n (each_slice in Ruby).
auto stl_min_max(const Container &c)
Get both min and max in a single pass.
auto stl_last(const Container &c)
Get last element.
auto stl_distinct(const Container &c)
Remove all duplicates (keeps first occurrence).
auto stl_max(const Container &c)
Get maximum element.
bool uni_equal(const Container1 &c1, const Container2 &c2)
Check equality of two containers.
auto stl_flatten(const Container &c)
Flatten a container of containers.
auto stl_reject(Pred &&pred, const Container &c)
Filter out elements (reject in Ruby, opposite of filter).
auto stl_filter(Pred &&pred, const Container &c)
Filter elements satisfying predicate.
auto stl_arrangements(size_t k, const Container &c)
Generate all k-arrangements (k-permutations) of a container.
auto stl_drop(size_t n, const Container &c)
Drop first n elements, return the rest.
auto stl_enumerate_to_pairs(const Container &c)
Enumerate container (return pairs of index and element).
auto stl_concat(const Container1 &c1, const Container2 &c2)
Concatenate two containers.
auto stl_combinations(size_t k, const Container &c)
Generate all k-combinations of a container.
auto stl_take_while(Pred &&pred, const Container &c)
Take elements while predicate is true.
auto stl_nth(const size_t n, const Container &c)
Get n-th element.
auto stl_map(Op &&op, const Container &c)
Map operation - transform each element.
bool stl_mem(const T &target, const Container &c)
Check if element exists in container (mem in ML).
std::optional< size_t > stl_find_index(Pred &&pred, const Container &c)
Find index of first element satisfying predicate.
T stl_foldl(T init, Op &&op, const Container &c)
Left fold (foldl) - reduce from left to right.
auto stl_min_by(Key &&key, const Container &c)
Get minimum element by key function.
std::vector< T > stl_linspace(T start, T end, size_t n)
Generate n evenly spaced values between start and end.
bool stl_exists(Pred &&pred, const Container &c)
Check if any element satisfies predicate.
auto stl_take(size_t n, const Container &c)
Take first n elements.
auto stl_generate(size_t n, Gen &&gen)
Generate a vector using a generator function.
std::vector< T > stl_range(T start, T end, T step=1)
Generate a range of values [start, end] with given step.
auto stl_init(const Container &c)
Get all elements except the last (init in Haskell).
size_t stl_count_value(const T &target, const Container &c)
Count occurrences of a value.
auto stl_sort(const Container &c)
Return sorted copy of container.
auto stl_permutations(const Container &c)
Generate all permutations of a container.
T product(const Container &container, const T &init=T{1})
Compute product of all elements.
auto stl_tally(const Container &c)
Count occurrences of each element (tally in Ruby, frequencies).
std::pair< First, Second > pair
Alias to std::pair kept for backwards compatibility.
bool stl_all(Pred &&pred, const Container &c)
Check if all elements satisfy predicate.
auto stl_partition(Pred &&pred, const Container &c)
Partition elements by predicate.
auto stl_unique(const Container &c)
Remove consecutive duplicates.
auto uni_map(Op &&op, const Container &c)
Map operation - transform each element.
auto stl_mapi(Op &&op, const Container &c)
Map with index (mapi in ML).
size_t stl_count(Pred &&pred, const Container &c)
Count elements satisfying predicate.
auto stl_sum(const Container &c)
Sum all elements.
bool is_even(const long n)
Return true if n is even.
auto uni_min(const Container &c)
Get minimum element.
auto stl_sliding_window(size_t n, const Container &c)
Sliding window of size n over container (each_cons in Ruby).
auto stl_max_by(Key &&key, const Container &c)
Get maximum element by key function.
std::string to_string(const time_t t, const std::string &format)
Format a time_t value into a string using format.
auto stl_group_by(Key &&key, const Container &c)
Group elements by key function.
auto stl_min(const Container &c)
Get minimum element.
bool stl_none(Pred &&pred, const Container &c)
Check if no element satisfies predicate.
auto stl_reverse(const Container &c)
Return reversed copy of container.
auto stl_flat_map(Op &&op, const Container &c)
Flat map - map then flatten.
std::vector< T > stl_rep(size_t n, const T &value)
Generate a vector of n repeated values.
T uni_foldl(T init, Op &&op, const Container &c)
Left fold (foldl) - reduce from left to right.
T stl_foldr(T init, Op &&op, const Container &c)
Right fold (foldr) - reduce from right to left.
auto stl_unzip_pairs(const Container &c)
Unzip pairs into two vectors.
auto stl_drop_while(Pred &&pred, const Container &c)
Drop elements while predicate is true, return the rest.
auto stl_intersperse(const T &sep, const Container &c)
Insert element between each pair (intersperse in Haskell).
auto stl_cartesian_product(const std::vector< std::vector< T > > &containers)
Generate cartesian product of multiple containers.
auto stl_product(const Container &c)
Product of all elements.
bool uni_all(Pred &&pred, const Container &c)
Check if all elements satisfy predicate.
auto stl_tail(const Container &c)
Get all elements except the first (tail in Haskell).
auto stl_filteri(Pred &&pred, const Container &c)
Filter with index (filteri in ML).
auto stl_find(Pred &&pred, const Container &c)
Find first element satisfying predicate.
auto stl_split_at(size_t n, const Container &c)
Split at position n, returning (take n, drop n) in one pass.
auto stl_sort_by(Cmp &&cmp, const Container &c)
Return sorted copy using custom comparator.
auto stl_power_set(const Container &c)
Generate power set (all subsets) of a container.
auto uni_take(size_t n, const Container &c)
Take first n elements.
auto uni_sum(const Container &c)
Sum all elements.
auto stl_group(const Container &c)
Group consecutive equal elements.
bool uni_exists(Pred &&pred, const Container &c)
Check if any element satisfies predicate.
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.
auto stl_zip_to_pairs(const Container1 &c1, const Container2 &c2)
Zip two containers into pairs.
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.
auto stl_find_last(Pred &&pred, const Container &c)
Find last element satisfying predicate.
Dynamic set implementations based on balanced binary search trees.