61void print_vec(
const string & label,
const vector<T> & v)
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 <<
", ";
87void print_nested(
const string & label,
const vector<vector<T>> & v)
89 cout << label <<
":" << endl;
90 for (
const auto & inner : v)
93 for (
size_t i = 0; i < inner.size(); ++i)
96 if (i < inner.size() - 1) cout <<
", ";
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)
126 cout << fixed << setprecision(2) << lin[i];
127 if (i < lin.size() - 1) cout <<
", ";
134 auto gen =
stl_generate(5, [](
size_t i) {
return i * i; });
135 print_vec(
"stl_generate(5, i -> i²)", gen);
142 cout <<
"--- 2. Map and Transform ---" << endl;
144 vector<int> nums = {1, 2, 3, 4, 5};
147 auto squares =
stl_map([](
int x) {
return x * x; }, nums);
151 print_vec(
"stl_map(x -> \"num_\" + x)", strings);
153 auto indexed =
stl_mapi([](
size_t i,
int x) {
156 print_vec(
"stl_mapi((i, x) -> \"[i]=x\")", indexed);
163 cout <<
"--- 3. Filter and Reject ---" << endl;
165 vector<int> data = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
168 auto evens =
stl_filter([](
int x) {
return x % 2 == 0; }, data);
169 print_vec(
"stl_filter(x -> x % 2 == 0)", evens);
171 auto odds =
stl_reject([](
int x) {
return x % 2 == 0; }, data);
172 print_vec(
"stl_reject(x -> x % 2 == 0)", odds);
174 auto even_indices =
stl_filteri([](
size_t i,
int) {
return i % 2 == 0; }, data);
175 print_vec(
"stl_filteri((i, x) -> i % 2 == 0)", even_indices);
182 cout <<
"--- 4. Fold (Reduce) Operations ---" << endl;
184 vector<int> v = {1, 2, 3, 4, 5};
187 int sum =
stl_foldl(0, [](
int acc,
int x) {
return acc + x; }, v);
188 cout <<
"stl_foldl(0, +): " <<
sum << endl;
191 cout <<
"stl_foldl(1, *): " <<
product << endl;
194 int foldr_result =
stl_foldr(0, [](
int x,
int acc) {
return x - acc; }, v);
195 cout <<
"stl_foldr(0, -): " << foldr_result <<
" (1-(2-(3-(4-(5-0)))))" << endl;
198 auto scan =
stl_scan_left(0, [](
int acc,
int x) {
return acc + x; }, v);
206 cout <<
"--- 5. Predicates ---" << endl;
208 vector<int> all_even = {2, 4, 6, 8};
209 vector<int> some_even = {1, 2, 3, 4};
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;
215 cout <<
" stl_all(is_even): " << boolalpha <<
stl_all(
is_even, all_even) << endl;
219 cout <<
"no_even = {1, 3, 5, 7}" << endl;
220 cout <<
" stl_all(is_even): " <<
stl_all(
is_even, no_even) << endl;
229 cout <<
"--- 6. Finding Elements ---" << endl;
231 vector<int> find_data = {10, 20, 30, 40, 50};
234 auto found =
stl_find([](
int x) {
return x > 25; }, find_data);
235 cout <<
"stl_find(x > 25): " << (found ?
to_string(*found) :
"not found") << endl;
237 auto last_found =
stl_find_last([](
int x) {
return x < 45; }, find_data);
238 cout <<
"stl_find_last(x < 45): " << (last_found ?
to_string(*last_found) :
"not found") << endl;
240 auto idx =
stl_find_index([](
int x) {
return x == 30; }, find_data);
241 cout <<
"stl_find_index(x == 30): " << (idx ?
to_string(*idx) :
"not found") << endl;
243 cout <<
"stl_mem(30, data): " <<
stl_mem(30, find_data) << endl;
244 cout <<
"stl_mem(99, data): " <<
stl_mem(99, find_data) << endl;
251 cout <<
"--- 7. Counting ---" << endl;
253 vector<int> count_data = {1, 2, 2, 3, 3, 3, 4, 4, 4, 4};
256 cout <<
"stl_count(x -> x % 2 == 0): " <<
stl_count(
is_even, count_data) << endl;
257 cout <<
"stl_count_value(3): " <<
stl_count_value(3, count_data) << endl;
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};
286 cout <<
"stl_first: " << (first ?
to_string(*first) :
"empty") << endl;
289 cout <<
"stl_last: " << (last ?
to_string(*last) :
"empty") << endl;
292 cout <<
"stl_nth(2): " << (nth ?
to_string(*nth) :
"out of bounds") << endl;
299 cout <<
"--- 10. Min, Max, Sum, Product ---" << endl;
301 vector<int> mm = {3, 1, 4, 1, 5, 9, 2, 6};
304 cout <<
"stl_min: " << *
stl_min(mm) << endl;
305 cout <<
"stl_max: " << *
stl_max(mm) << endl;
308 cout <<
"stl_min_max: (" << min_val <<
", " << max_val <<
")" << endl;
310 cout <<
"stl_sum: " <<
stl_sum(mm) << endl;
311 cout <<
"stl_product: " <<
stl_product(mm) << endl;
313 vector<string> words = {
"hello",
"a",
"wonderful",
"world"};
314 auto shortest =
stl_min_by([](
const string & s) {
return s.length(); }, words);
315 auto longest =
stl_max_by([](
const string & s) {
return s.length(); }, words);
316 cout <<
"Shortest word: " << *shortest << endl;
317 cout <<
"Longest word: " << *longest << endl;
324 cout <<
"--- 11. Partition ---" << endl;
326 vector<int> part = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
330 print_vec(
"Evens (matching)", evens_part);
331 print_vec(
"Odds (non-matching)", odds_part);
338 cout <<
"--- 12. Zip and Enumerate ---" << endl;
340 vector<int>
keys = {1, 2, 3};
341 vector<string> values = {
"one",
"two",
"three"};
350 print_vec(
"Unzipped keys", unzipped_keys);
351 print_vec(
"Unzipped values", unzipped_values);
361 cout <<
"--- 13. Reverse and Sort ---" << endl;
363 vector<int> unsorted = {3, 1, 4, 1, 5, 9, 2, 6};
368 print_vec(
"stl_sort_by(descending)",
stl_sort_by([](
int a,
int b) {
return a > b; }, unsorted));
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};
396 vector<vector<int>> nested = {{1, 2}, {3, 4}, {5}};
401 return vector<int>{x, x * 10};
403 print_vec(
"stl_flat_map(x -> {x, x*10})", flat_mapped);
410 cout <<
"--- 16. Grouping ---" << endl;
412 vector<int> to_group = {1, 1, 2, 2, 2, 3, 1};
416 cout <<
"stl_group (consecutive):" << endl;
417 for (
const auto & g : grouped)
423 vector<string> words_to_group = {
"apple",
"ant",
"banana",
"bear",
"apricot"};
426 auto by_first_char =
stl_group_by([](
const string & s) {
return s[0]; }, words_to_group);
427 cout <<
"stl_group_by(first char):" << endl;
428 for (
const auto & [key, vals] : by_first_char)
430 cout <<
" '" << key <<
"': ";
439 cout <<
"--- 17. Tally (Frequency Count) ---" << endl;
441 vector<string> fruits = {
"apple",
"banana",
"apple",
"cherry",
"banana",
"apple"};
445 cout <<
"stl_tally:" << endl;
446 for (
const auto & [item,
count] : tally)
447 cout <<
" \"" << item <<
"\": " <<
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;
472 vector<int> to_inter = {1, 2, 3};
477 vector<int> to_split = {1, 2, 3, 4, 5};
478 auto [first_part, second_part] =
stl_split_at(2, to_split);
479 print_vec(
"stl_split_at(2) first", first_part);
480 print_vec(
"stl_split_at(2) second", second_part);
482 auto [span_match, span_rest] =
stl_span([](
int x) {
return x < 4; }, to_split);
483 print_vec(
"stl_span(x < 4) matching", span_match);
484 print_vec(
"stl_span(x < 4) rest", span_rest);
491 cout <<
"--- 20. Init and Tail ---" << endl;
493 vector<int> it = {1, 2, 3, 4, 5};
504 cout <<
"--- 21. Combinatorics ---" << endl;
506 vector<int> comb_set = {1, 2, 3};
510 cout <<
"stl_permutations (" << perms.size() <<
" total):" << endl;
511 for (
const auto & p : perms)
518 cout <<
"stl_combinations(2) (" << combos.size() <<
" total):" << endl;
519 for (
const auto & c : combos)
526 cout <<
"stl_arrangements(2) (" << arrs.size() <<
" total):" << endl;
527 for (
const auto & arr : arrs)
538 cout <<
"--- 22. Cartesian Product and Power Set ---" << endl;
540 vector<vector<int>> sets = {{1, 2}, {3, 4}};
541 cout <<
"Sets: {{1, 2}, {3, 4}}" << endl;
546 vector<int> ps_set = {1, 2, 3};
550 cout <<
"stl_power_set (" << power.size() <<
" subsets):" << endl;
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;
570 list<int> my_list = {1, 2, 3, 4, 5};
571 cout <<
"std::list<int>: {1, 2, 3, 4, 5}" << endl;
573 auto list_squares =
stl_map([](
int x) {
return x * x; }, my_list);
574 print_vec(
"stl_map(x -> x²) on list", list_squares);
576 int list_sum =
stl_foldl(0, std::plus<int>{}, my_list);
577 cout <<
"stl_foldl(0, +) on list: " << list_sum << endl;
582 set<int> my_set = {5, 2, 8, 1, 9, 3};
583 cout <<
"std::set<int>: {5, 2, 8, 1, 9, 3} -> ordered: {";
584 bool first_set =
true;
587 if (!first_set) cout <<
", ";
593 auto set_doubled =
stl_map([](
int x) {
return x * 2; }, my_set);
594 print_vec(
"stl_map(x -> x*2) on set", set_doubled);
596 auto set_filtered_gt3 =
stl_filter([](
int x) {
return x > 3; }, my_set);
597 print_vec(
"stl_filter(x > 3) on set", set_filtered_gt3);
599 cout <<
"stl_sum on set: " <<
stl_sum(my_set) << endl;
606 cout <<
"--- 23b. Aleph Containers (DynList, DynSetTree) ---" << endl;
610 cout <<
"DynList<int>: {10, 20, 30, 40, 50}" << endl;
613 auto dyn_list_squares =
uni_map([](
int x) {
return x * x; }, dyn_list);
614 print_vec(
"uni_map(x -> x²) on DynList", dyn_list_squares);
616 auto dyn_list_filtered =
uni_filter([](
int x) {
return x >= 30; }, dyn_list);
617 print_vec(
"uni_filter(x >= 30) on DynList", dyn_list_filtered);
619 int dyn_list_sum =
uni_foldl(0, [](
int a,
int b) {
return a + b; }, dyn_list);
620 cout <<
"uni_foldl(0, +) on DynList: " << dyn_list_sum << endl;
631 cout <<
"DynSetTree<int>: {15, 5, 25, 10, 20} -> in-order: {";
632 bool first_tree =
true;
633 for (
auto it = dyn_tree.
get_it(); it.has_curr(); it.next_ne())
635 if (!first_tree) cout <<
", ";
636 cout << it.get_curr();
641 auto tree_mapped =
uni_map([](
int x) {
return x + 100; }, dyn_tree);
642 print_vec(
"uni_map(x -> x+100) on DynSetTree", tree_mapped);
644 auto dyn_tree_even_filtered =
645 uni_filter([](
int x) {
return x % 2 == 0; }, dyn_tree);
646 print_vec(
"uni_filter(even) on DynSetTree", dyn_tree_even_filtered);
648 bool tree_all_positive =
uni_all([](
int x) {
return x > 0; }, dyn_tree);
649 cout <<
"uni_all(x > 0) on DynSetTree: " << boolalpha << tree_all_positive << endl;
656 cout <<
"--- 23c. Mixing STL and Aleph Results ---" << endl;
659 vector<int> vec_a = {1, 2, 3, 4, 5};
660 set<int> set_b = {10, 20, 30, 40, 50};
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;
669 auto vec_sq =
stl_map([](
int x) {
return x * x; }, vec_a);
670 auto set_sq =
stl_map([](
int x) {
return x * x; }, set_b);
673 auto dlist_sq =
uni_map([](
int x) {
return x * x; }, dlist_c);
675 print_vec(
"squares from vector", vec_sq);
677 print_vec(
"squares from DynList", dlist_sq);
681 print_vec(
"All squares combined", combined);
683 cout <<
"Sum of all squares: " <<
stl_sum(combined) << 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};
694 set<int> uni_set = {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;
711 auto is_greater_than_7 = [](
int x) {
return x > 7; };
712 auto square = [](
int x) {
return x * x; };
715 auto vec_filtered =
uni_filter(is_greater_than_7, uni_vec);
716 auto set_filtered =
uni_filter(is_greater_than_7, uni_set);
717 auto dlist_filtered =
uni_filter(is_greater_than_7, uni_dlist);
718 auto tree_filtered =
uni_filter(is_greater_than_7, uni_tree);
720 cout <<
"After uni_filter(x > 7):" << endl;
728 auto vec_squared =
uni_map(square, vec_filtered);
729 auto set_squared =
uni_map(square, set_filtered);
730 auto dlist_squared =
uni_map(square, dlist_filtered);
731 auto tree_squared =
uni_map(square, tree_filtered);
733 cout <<
"After uni_map(x -> x²) on filtered results:" << endl;
741 auto sum_op = [](
int acc,
int x) {
return acc + x; };
742 int vec_sum =
uni_foldl(0, sum_op, vec_squared);
743 int set_sum =
uni_foldl(0, sum_op, set_squared);
744 int dlist_sum =
uni_foldl(0, sum_op, dlist_squared);
745 int tree_sum =
uni_foldl(0, sum_op, tree_squared);
747 cout <<
"After uni_foldl(0, +) on squared results:" << endl;
748 cout <<
" vector sum: " << vec_sum << endl;
749 cout <<
" set sum: " << set_sum << endl;
750 cout <<
" DynList sum: " << dlist_sum << endl;
751 cout <<
" DynSetTree sum: " << tree_sum << endl;
755 auto all_positive = [](
int x) {
return x > 0; };
756 auto has_large = [](
int x) {
return x > 100; };
758 cout <<
"Predicate tests using uni_all and uni_exists:" << endl;
759 cout <<
" vector - uni_all(x > 0): " << boolalpha <<
uni_all(all_positive, uni_vec) << endl;
760 cout <<
" set - uni_all(x > 0): " <<
uni_all(all_positive, uni_set) << endl;
761 cout <<
" DynList - uni_all(x > 0): " <<
uni_all(all_positive, uni_dlist) << endl;
762 cout <<
" DynSetTree - uni_all(x > 0): " <<
uni_all(all_positive, uni_tree) << endl;
764 cout <<
" vector - uni_exists(x > 100): " <<
uni_exists(has_large, vec_squared) << endl;
765 cout <<
" set - uni_exists(x > 100): " <<
uni_exists(has_large, set_squared) << endl;
766 cout <<
" DynList - uni_exists(x > 100): " <<
uni_exists(has_large, dlist_squared) << endl;
767 cout <<
" DynSetTree - uni_exists(x > 100): " <<
uni_exists(has_large, tree_squared) << endl;
771 cout <<
"Min/Max operations:" << endl;
772 auto vec_min =
uni_min(uni_vec);
773 auto set_min =
uni_min(uni_set);
774 auto dlist_min =
uni_min(uni_dlist);
775 auto tree_min =
uni_min(uni_tree);
777 cout <<
" vector min: " << (vec_min ?
to_string(*vec_min) :
"empty") << endl;
778 cout <<
" set min: " << (set_min ?
to_string(*set_min) :
"empty") << endl;
779 cout <<
" DynList min: " << (dlist_min ?
to_string(*dlist_min) :
"empty") << endl;
780 cout <<
" DynSetTree min: " << (tree_min ?
to_string(*tree_min) :
"empty") << endl;
784 cout <<
"Take/Drop operations (first 3 elements):" << endl;
785 auto vec_take =
uni_take(3, uni_vec);
786 auto set_take =
uni_take(3, uni_set);
787 auto dlist_take =
uni_take(3, uni_dlist);
788 auto tree_take =
uni_take(3, uni_tree);
790 print_vec(
" uni_take(3) from vector", vec_take);
791 print_vec(
" uni_take(3) from set", set_take);
792 print_vec(
" uni_take(3) from DynList", dlist_take);
793 print_vec(
" uni_take(3) from DynSetTree", tree_take);
797 cout <<
"Cross-container comparisons:" << endl;
798 cout <<
" vector == DynList: " << boolalpha <<
uni_equal(uni_vec, uni_dlist) << endl;
799 cout <<
" set == DynSetTree: " <<
uni_equal(uni_set, uni_tree) << endl;
800 cout <<
" vector == set: " <<
uni_equal(uni_vec, uni_set) <<
" (order differs)" << 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) {
808 auto step1 =
uni_filter([](
int x) {
return x % 2 != 0; }, container);
809 auto step2 =
uni_map([](
int x) {
return x * 3; }, step1);
814 cout <<
" vector result: " << pipeline(uni_vec) << endl;
815 cout <<
" set result: " << pipeline(uni_set) << endl;
816 cout <<
" DynList result: " << pipeline(uni_dlist) << endl;
817 cout <<
" DynSetTree result: " << pipeline(uni_tree) << endl;
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};
837 auto squared_vals =
stl_map([](
int x) {
return x * x; }, even_vals);
838 int final_sum =
stl_foldl(0, std::plus<int>{}, squared_vals);
840 cout <<
"Pipeline: distinct -> filter(even) -> map(square) -> sum" << endl;
841 print_vec(
" After distinct", distinct_vals);
842 print_vec(
" After filter(even)", even_vals);
843 print_vec(
" After map(square)", squared_vals);
844 cout <<
" Final sum: " << final_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)
Doubly-linked list (defined in tpl_dynList.H).
T & append(const T &item)
Dynamic set backed by balanced binary search trees with automatic memory management.
Key * insert(const Key &key)
Inserts a key into the dynamic set.
auto get_it()
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).
DynList< T > rep(const size_t n, const T &item)
Create a sequence of repeated items.
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.
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.
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.
Node * access(Node *list, size_t n)
Dynamic set implementations based on balanced binary search trees.