Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ah-stl-functional.H File Reference

Functional programming utilities for C++ Standard Library containers. More...

#include <type_traits>
#include <vector>
#include <optional>
#include <functional>
#include <algorithm>
#include <numeric>
#include <utility>
#include <tuple>
#include <iterator>
#include <stdexcept>
#include <unordered_set>
#include <unordered_map>
#include <ah-ranges.H>
Include dependency graph for ah-stl-functional.H:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  Aleph::stl_detail::has_size< T, typename >
 Detect if a type has a size() method. More...
 
struct  Aleph::stl_detail::has_size< T, std::void_t< decltype(std::declval< T >().size())> >
 
struct  Aleph::stl_detail::is_std_hashable< T, typename >
 Detect if a type is hashable via std::hash. More...
 
struct  Aleph::stl_detail::is_std_hashable< T, std::void_t< decltype(std::hash< T >{}(std::declval< T >()))> >
 
struct  Aleph::stl_detail::has_reverse_iterators< T, typename >
 Detect if a container has reverse iterators. More...
 
struct  Aleph::stl_detail::has_reverse_iterators< T, std::void_t< decltype(std::declval< T >().rbegin()), decltype(std::declval< T >().rend())> >
 

Namespaces

namespace  Aleph
 Main namespace for Aleph-w library functions.
 
namespace  Aleph::stl_detail
 
namespace  Aleph::stl_comb_detail
 

Functions

template<typename Container >
size_t Aleph::stl_detail::safe_size (const Container &c)
 Helper to get container size safely (returns 0 for containers without size())
 
template<typename Result , typename Container >
void Aleph::stl_detail::try_reserve (Result &result, const Container &c)
 Helper to reserve if possible.
 
template<typename Result >
void Aleph::stl_detail::try_reserve_n (Result &result, size_t n)
 Helper to reserve with a specific size if the result supports it.
 
template<typename T = int>
std::vector< TAleph::stl_range (T start, T end, T step=1)
 Generate a range of values [start, end] with given step.
 
template<typename T = int>
std::vector< TAleph::stl_range (T n)
 Generate a range [0, n-1].
 
template<typename T = double>
std::vector< TAleph::stl_linspace (T start, T end, size_t n)
 Generate n evenly spaced values between start and end.
 
template<typename T >
std::vector< TAleph::stl_rep (size_t n, const T &value)
 Generate a vector of n repeated values.
 
template<typename Gen >
auto Aleph::stl_generate (size_t n, Gen &&gen)
 Generate a vector using a generator function.
 
template<typename Op , typename Container >
void Aleph::stl_for_each (Op &&op, const Container &c)
 Apply operation to each element (for_each).
 
template<typename Op , typename Container >
void Aleph::stl_for_each_indexed (Op &&op, const Container &c)
 Apply operation to each element with index.
 
template<typename Op , typename Container >
auto Aleph::stl_map (Op &&op, const Container &c)
 Map operation - transform each element.
 
template<typename Op , typename Container >
auto Aleph::stl_mapi (Op &&op, const Container &c)
 Map with index (mapi in ML).
 
template<typename Pred , typename Container >
auto Aleph::stl_filter (Pred &&pred, const Container &c)
 Filter elements satisfying predicate.
 
template<typename Pred , typename Container >
auto Aleph::stl_filteri (Pred &&pred, const Container &c)
 Filter with index (filteri in ML).
 
template<typename T , typename Op , typename Container >
T Aleph::stl_foldl (T init, Op &&op, const Container &c)
 Left fold (foldl) - reduce from left to right.
 
template<typename T , typename Op , typename Container >
T Aleph::stl_foldr (T init, Op &&op, const Container &c)
 Right fold (foldr) - reduce from right to left.
 
template<typename T , typename Op , typename Container >
std::vector< TAleph::stl_scan_left (T init, Op &&op, const Container &c)
 Scan left - fold with all intermediate results.
 
template<typename T , typename Op , typename Container >
std::vector< TAleph::stl_scan_right (T init, Op &&op, const Container &c)
 Scan right - right fold with all intermediate results.
 
template<typename T , typename Op , typename Container >
T Aleph::stl_reduce (T init, Op &&op, const Container &c)
 Alias for stl_foldl.
 
template<typename Pred , typename Container >
bool Aleph::stl_all (Pred &&pred, const Container &c)
 Check if all elements satisfy predicate.
 
template<typename Pred , typename Container >
bool Aleph::stl_exists (Pred &&pred, const Container &c)
 Check if any element satisfies predicate.
 
template<typename Pred , typename Container >
bool Aleph::stl_any (Pred &&pred, const Container &c)
 Alias for stl_exists.
 
template<typename Pred , typename Container >
bool Aleph::stl_none (Pred &&pred, const Container &c)
 Check if no element satisfies predicate.
 
template<typename Pred , typename Container >
auto Aleph::stl_find (Pred &&pred, const Container &c)
 Find first element satisfying predicate.
 
template<typename Pred , typename Container >
auto Aleph::stl_find_last (Pred &&pred, const Container &c)
 Find last element satisfying predicate.
 
template<typename Pred , typename Container >
std::optional< size_tAleph::stl_find_index (Pred &&pred, const Container &c)
 Find index of first element satisfying predicate.
 
template<typename Op , typename Container >
auto Aleph::stl_find_mapi (Op &&op, const Container &c)
 Find and map with index (find_mapi in ML).
 
template<typename T , typename Container >
bool Aleph::stl_mem (const T &target, const Container &c)
 Check if element exists in container (mem in ML).
 
template<typename Pred , typename Container >
size_t Aleph::stl_count (Pred &&pred, const Container &c)
 Count elements satisfying predicate.
 
template<typename T , typename Container >
size_t Aleph::stl_count_value (const T &target, const Container &c)
 Count occurrences of a value.
 
template<typename Container >
auto Aleph::stl_take (size_t n, const Container &c)
 Take first n elements.
 
template<typename Container >
auto Aleph::stl_drop (size_t n, const Container &c)
 Drop first n elements, return the rest.
 
template<typename Container >
auto Aleph::stl_take_last (size_t n, const Container &c)
 Take last n elements.
 
template<typename Pred , typename Container >
auto Aleph::stl_take_while (Pred &&pred, const Container &c)
 Take elements while predicate is true.
 
template<typename Pred , typename Container >
auto Aleph::stl_drop_while (Pred &&pred, const Container &c)
 Drop elements while predicate is true, return the rest.
 
template<typename Container >
auto Aleph::stl_first (const Container &c)
 Get first element.
 
template<typename Container >
auto Aleph::stl_last (const Container &c)
 Get last element.
 
template<typename Container >
auto Aleph::stl_nth (const size_t n, const Container &c)
 Get n-th element.
 
template<typename Container >
auto Aleph::stl_min (const Container &c)
 Get minimum element.
 
template<typename Container >
auto Aleph::stl_max (const Container &c)
 Get maximum element.
 
template<typename Container >
auto Aleph::stl_min_max (const Container &c)
 Get both min and max in a single pass.
 
template<typename Key , typename Container >
auto Aleph::stl_min_by (Key &&key, const Container &c)
 Get minimum element by key function.
 
template<typename Key , typename Container >
auto Aleph::stl_max_by (Key &&key, const Container &c)
 Get maximum element by key function.
 
template<typename Container >
auto Aleph::stl_sum (const Container &c)
 Sum all elements.
 
template<typename Container >
auto Aleph::stl_product (const Container &c)
 Product of all elements.
 
template<typename Pred , typename Container >
auto Aleph::stl_partition (Pred &&pred, const Container &c)
 Partition elements by predicate.
 
template<typename Container1 , typename Container2 >
auto Aleph::stl_zip_to_pairs (const Container1 &c1, const Container2 &c2)
 Zip two containers into pairs.
 
template<typename Container >
auto Aleph::stl_unzip_pairs (const Container &c)
 Unzip pairs into two vectors.
 
template<typename Container >
auto Aleph::stl_enumerate_to_pairs (const Container &c)
 Enumerate container (return pairs of index and element).
 
template<typename Container1 , typename Container2 >
bool Aleph::stl_equal (const Container1 &c1, const Container2 &c2)
 Check equality of two containers.
 
template<typename Container1 , typename Container2 >
int Aleph::stl_compare (const Container1 &c1, const Container2 &c2)
 Compare two containers lexicographically.
 
template<typename Container >
auto Aleph::stl_reverse (const Container &c)
 Return reversed copy of container.
 
template<typename Container >
auto Aleph::stl_sort (const Container &c)
 Return sorted copy of container.
 
template<typename Cmp , typename Container >
auto Aleph::stl_sort_by (Cmp &&cmp, const Container &c)
 Return sorted copy using custom comparator.
 
template<typename Container >
auto Aleph::stl_unique (const Container &c)
 Remove consecutive duplicates.
 
template<typename Container >
auto Aleph::stl_distinct (const Container &c)
 Remove all duplicates (keeps first occurrence).
 
template<typename Container1 , typename Container2 >
auto Aleph::stl_concat (const Container1 &c1, const Container2 &c2)
 Concatenate two containers.
 
template<typename Container >
auto Aleph::stl_flatten (const Container &c)
 Flatten a container of containers.
 
template<typename Op , typename Container >
auto Aleph::stl_flat_map (Op &&op, const Container &c)
 Flat map - map then flatten.
 
template<typename Container >
auto Aleph::stl_group (const Container &c)
 Group consecutive equal elements.
 
template<typename Key , typename Container >
auto Aleph::stl_group_by (Key &&key, const Container &c)
 Group elements by key function.
 
template<typename T , typename Op >
bool Aleph::stl_comb_detail::permutations_impl (std::vector< T > &arr, size_t start, Op &op_ref)
 
template<typename T , typename Op >
bool Aleph::stl_comb_detail::combinations_impl (const std::vector< T > &arr, size_t k, size_t start, std::vector< T > &current, Op &op_ref)
 
template<typename T , typename Op >
bool Aleph::stl_comb_detail::arrangements_impl (const std::vector< T > &arr, size_t k, std::vector< T > &current, std::vector< bool > &used, Op &op_ref)
 
template<typename Op , typename Container >
bool Aleph::stl_traverse_permutations (Op &&op, const Container &c)
 Traverse all permutations of a container.
 
template<typename Container >
auto Aleph::stl_permutations (const Container &c)
 Generate all permutations of a container.
 
template<typename Op , typename Container >
bool Aleph::stl_traverse_combinations (size_t k, Op &&op, const Container &c)
 Traverse all k-combinations of a container.
 
template<typename Container >
auto Aleph::stl_combinations (size_t k, const Container &c)
 Generate all k-combinations of a container.
 
template<typename Op , typename Container >
bool Aleph::stl_traverse_arrangements (size_t k, Op &&op, const Container &c)
 Traverse all k-arrangements (k-permutations) of a container.
 
template<typename Container >
auto Aleph::stl_arrangements (size_t k, const Container &c)
 Generate all k-arrangements (k-permutations) of a container.
 
template<typename T >
auto Aleph::stl_cartesian_product (const std::vector< std::vector< T > > &containers)
 Generate cartesian product of multiple containers.
 
template<typename Container >
auto Aleph::stl_power_set (const Container &c)
 Generate power set (all subsets) of a container.
 
template<typename Container >
auto Aleph::stl_sliding_window (size_t n, const Container &c)
 Sliding window of size n over container (each_cons in Ruby).
 
template<typename Container >
auto Aleph::stl_chunks (size_t n, const Container &c)
 Split container into chunks of size n (each_slice in Ruby).
 
template<typename T , typename Container >
auto Aleph::stl_intersperse (const T &sep, const Container &c)
 Insert element between each pair (intersperse in Haskell).
 
template<typename Container >
auto Aleph::stl_split_at (size_t n, const Container &c)
 Split at position n, returning (take n, drop n) in one pass.
 
template<typename Pred , typename Container >
auto Aleph::stl_span (Pred &&pred, const Container &c)
 Split at predicate boundary (span in Haskell).
 
template<typename Container >
auto Aleph::stl_init (const Container &c)
 Get all elements except the last (init in Haskell).
 
template<typename Container >
auto Aleph::stl_tail (const Container &c)
 Get all elements except the first (tail in Haskell).
 
template<typename Container >
auto Aleph::stl_tally (const Container &c)
 Count occurrences of each element (tally in Ruby, frequencies).
 
template<typename Pred , typename Container >
auto Aleph::stl_reject (Pred &&pred, const Container &c)
 Filter out elements (reject in Ruby, opposite of filter).
 

Variables

constexpr size_t Aleph::stl_detail::ADAPTIVE_THRESHOLD = 64
 Threshold for switching from linear to hash-based algorithms.
 
template<typename T >
constexpr bool Aleph::stl_detail::has_size_v = has_size<T>::value
 
template<typename T >
constexpr bool Aleph::stl_detail::is_std_hashable_v = is_std_hashable<T>::value
 
template<typename T >
constexpr bool Aleph::stl_detail::has_reverse_iterators_v = has_reverse_iterators<T>::value
 

Detailed Description

Functional programming utilities for C++ Standard Library containers.

This header provides functional programming operations (map, filter, fold, etc.) that work with any STL-compatible container (std::vector, std::list, std::deque, std::set, etc.).

Key Features:

  • Works with any container that has begin()/end() iterators
  • Includes foldr (right fold) using reverse iterators
  • ML-style operations: mapi, filteri, scan_left, find_mapi
  • Zero dependencies on Aleph containers

Usage Example:

#include <vector>
std::vector<int> nums = {1, 2, 3, 4, 5};
// Map: square each element
auto squares = stl_map([](int x) { return x * x; }, nums);
// Filter: keep only even numbers
auto evens = stl_filter([](int x) { return x % 2 == 0; }, nums);
// Fold left: sum
int sum = stl_foldl(0, [](int acc, int x) { return acc + x; }, nums);
// Fold right (possible with STL!)
int result = stl_foldr(0, [](int x, int acc) { return x - acc; }, nums);
Functional programming utilities for C++ Standard Library containers.
auto stl_filter(Pred &&pred, const Container &c)
Filter elements satisfying predicate.
T stl_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.
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
Author
Leandro Rabindranath Leon

Definition in file ah-stl-functional.H.