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

Functional programming utilities for Aleph-w containers. More...

#include <stdexcept>
#include <utility>
#include <tuple>
#include <functional>
#include <algorithm>
#include <optional>
#include <ah-errors.H>
#include <ah-ranges.H>
Include dependency graph for ahFunctional.H:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  Aleph::Found_Item< T >
 Abstract base class for optional-like results. More...
 
struct  Aleph::None< T >
 Represents a missing value. More...
 
struct  Aleph::Some< T >
 Represents a found value (stored by reference). More...
 
struct  Aleph::Dft_Map_Op< tgtT, srcT >
 Default mapping operation (identity). More...
 
struct  Aleph::Dft_Fold_Op< TR, TD >
 Default folding operation (returns default-constructed accumulator). More...
 
struct  Aleph::Dft_Filter_Op< T >
 Default filter operation (always true). More...
 
class  Aleph::Pair_Iterator< Itor1, Itor2 >
 Iterator that zips two other iterators. More...
 

Namespaces

namespace  Aleph
 Main namespace for Aleph-w library functions.
 

Functions

template<typename T = int, template< typename > class Container = DynList>
Container< TAleph::range (const T start, const T end, const T step=1)
 Generate a range of values [start, end] with a given step.
 
template<typename T = int, template< typename > class Container = DynList>
Container< TAleph::nrange (const T start, const T end, const size_t n)
 Generate exactly n values evenly spaced between [start, end].
 
template<typename T = int, template< typename > class Container = DynList, class Op >
auto Aleph::set_range (const T start, const T end, const T step, Op &op) -> Container< std::decay_t< decltype(op(start))> >
 Generate a range [start, end] and apply an operation to each value.
 
template<typename T = int, template< typename > class Container = DynList, class Op >
auto Aleph::set_range (const T start, const T end, const T step=1, Op &&op=Op()) -> Container< std::decay_t< decltype(op(start))> >
 
template<typename T = int, template< typename > class Container = DynList>
Container< TAleph::contiguous_range (T start, const size_t n)
 Generate n contiguous values starting from start.
 
template<typename T = int, template< typename > class Container = DynList>
Container< TAleph::range (const T n)
 Return a range 0, 1, ... n - 1.
 
template<typename T = int>
DynList< TAleph::rep (size_t n, const T &item)
 Create a sequence of repeated items.
 
template<typename T = int>
DynList< TAleph::rep (size_t n, T &&item=T())
 
template<class Container >
DynList< typename Container::Item_Type * > Aleph::pointers_list (Container &c)
 Create a list of pointers to items in a container.
 
template<class Container >
DynList< const typename Container::Item_Type * > Aleph::pointers_list (const Container &c)
 
template<class Op >
void Aleph::each (size_t start, size_t end, Op &op)
 Execute an operation repeatedly over a range of indices.
 
template<class Op >
void Aleph::each (size_t start, size_t end, Op &&op)
 
template<class Op >
void Aleph::each (size_t n, Op &op)
 
template<class Op >
void Aleph::each (size_t n, Op &&op)
 
template<class Container >
DynList< typename Container::Item_Type > Aleph::sublist (const Container &c, size_t pos, size_t stride)
 Extract a sublist using a stride.
 
template<class Container >
DynList< typename Container::Item_Type > Aleph::sublist (const Container &c, size_t stride)
 
template<class Container , class Operation >
ContainerAleph::for_each (Container &container, Operation &operation)
 Apply an operation to each element in a container.
 
template<class Container , class Operation >
const ContainerAleph::for_each (const Container &container, Operation &operation)
 
template<class Container , class Operation >
ContainerAleph::for_each (Container &container, Operation &&operation=Operation())
 
template<class Container , class Operation >
const ContainerAleph::for_each (const Container &container, Operation &&operation=Operation())
 
template<class Container , class Operation >
void Aleph::enum_for_each (const Container &container, Operation &operation)
 Apply an operation to each element and its index.
 
template<class Container , class Operation >
void Aleph::enum_for_each (const Container &container, Operation &&operation)
 
template<class Container , class Operation >
bool Aleph::all (Container &container, Operation &operation)
 Return true if all elements satisfy a predicate.
 
template<class Container , class Operation >
bool Aleph::all (const Container &container, Operation &operation)
 
template<class Container , class Operation >
bool Aleph::all (Container &container, Operation &&operation=Operation())
 
template<class Container , class Operation >
bool Aleph::all (const Container &container, Operation &&operation=Operation())
 
template<class Container , class Operation >
bool Aleph::exists (Container &container, Operation &operation)
 Return true if at least one element satisfies a predicate.
 
template<class Container , class Operation >
bool Aleph::exists (const Container &container, Operation &operation)
 
template<class Container , class Operation >
bool Aleph::exists (Container &container, Operation &&operation=Operation())
 
template<class Container , class Operation >
bool Aleph::exists (const Container &container, Operation &&operation=Operation())
 
template<class Container1 , template< typename > class Container2 = Aleph::DynList, class Operation = Dft_Filter_Op<typename Container1::Item_Type>>
Container2< typename Container1::Item_Type > Aleph::filter (Container1 &container, Operation &operation)
 Filter elements that satisfy operation.
 
template<class Container1 , template< typename > class Container2 = Aleph::DynList, class Operation = Dft_Filter_Op<typename Container1::Item_Type>>
Container2< typename Container1::Item_Type > Aleph::filter (const Container1 &container, Operation &operation)
 
template<class Container1 , template< typename > class Container2 = Aleph::DynList, class Operation = Dft_Filter_Op<typename Container1::Item_Type>>
Container2< typename Container1::Item_Type > Aleph::filter (Container1 &container, Operation &&operation)
 
template<class Container1 , template< typename > class Container2 = Aleph::DynList, class Operation = Dft_Filter_Op<typename Container1::Item_Type>>
Container2< typename Container1::Item_Type > Aleph::filter (const Container1 &container, Operation &&operation)
 
template<typename T , class C , class Op >
DynList< TAleph::maps (const C &c, Op op)
 Classic map operation.
 
template<typename T , class Container , class Operation >
T Aleph::foldl (const Container &container, const T &init, Operation operation)
 Classic left fold (reduce).
 
template<class Container1 , class Container2 >
DynList< std::pair< typename Container1::Item_Type, typename Container2::Item_Type > > Aleph::zip (const Container1 &a, const Container2 &b)
 Zip two containers into a list of pairs.
 
template<class Container1 , class Container2 >
DynList< std::tuple< typename Container1::Item_Type, typename Container2::Item_Type > > Aleph::tzip (const Container1 &a, const Container2 &b)
 Zip two containers into a list of tuples.
 
template<class Container1 , class Container2 >
DynList< std::pair< typename Container1::Item_Type, typename Container2::Item_Type > > Aleph::zipEq (const Container1 &a, const Container2 &b)
 Zip two containers; throw if lengths differ.
 
template<class Container1 , class Container2 >
DynList< std::tuple< typename Container1::Item_Type, typename Container2::Item_Type > > Aleph::tzipEq (const Container1 &a, const Container2 &b)
 Zip two containers into tuples; throw if lengths differ.
 
template<class Container >
auto Aleph::enumerate (const Container &c)
 Return pairs of (element, index).
 
template<class C1 , class C2 , class Eq = std::equal_to<typename C1::Item_Type>>
bool Aleph::eq (const C1 &c1, const C2 &c2, Eq e=Eq())
 Check equality of two containers using a predicate.
 
template<typename T >
bool Aleph::operator== (const DynList< T > &l1, const DynList< T > &l2)
 Equality operator for DynList.
 
template<class C1 , class C2 , class Eq >
bool Aleph::containers_eq (const C1 &c1, const C2 &c2, Eq e)
 
template<class C1 , class C2 , class Eq = std::equal_to<typename C1::Item_Type>>
std::tuple< bool, size_t, typename C1::Item_Type, typename C2::Item_Type > Aleph::are_eq (const C1 &c1, const C2 &c2, Eq e=Eq())
 Detailed equality check returning mismatch position and values.
 
template<class C1 , class C2 , class Cmp = std::less<typename C1::Item_Type>>
bool Aleph::lesser (const C1 &c1, const C2 &c2, Cmp cmp=Cmp())
 Lexicographical comparison between two containers.
 
template<class C1 , class C2 , class Eq = std::equal_to<typename C1::Item_Type>>
bool Aleph::diff (const C1 &c1, const C2 &c2, Eq e=Eq())
 Check if two containers differ.
 
template<class Container >
auto Aleph::unzip (const Container &l)
 Separate a list of pairs into two lists.
 
template<template< typename > class Container, typename T1 , typename T2 >
std::tuple< Container< T1 >, Container< T2 > > Aleph::tunzip (const Container< std::tuple< T1, T2 > > &l)
 Separate a list of tuples into two containers.
 
template<class SrcContainer , template< typename > class TgtContainer = Aleph::DynList>
std::pair< TgtContainer< typename SrcContainer::Item_Type >, TgtContainer< typename SrcContainer::Item_Type > > Aleph::partition (const SrcContainer &c, std::function< bool(const typename SrcContainer::Item_Type &)> operation)
 Partition a container into two based on a predicate.
 
template<class Container >
DynList< std::pair< size_t, typename Container::Key_Type > > Aleph::indexes (const Container &c)
 Return pairs of (index, key).
 
template<class Container >
DynList< std::tuple< size_t, typename Container::Key_Type > > Aleph::tindexes (const Container &c)
 Return tuples of (index, key).
 
template<typename T , template< typename > class Container>
Container< TAleph::reverse (const Container< T > &l)
 Return a reversed copy of the container.
 
template<class Container >
auto Aleph::gen_seq_list_tuples (const Container &c, size_t n)
 Generate all sequential tuples (sliding windows) of size n.
 
template<typename T , template< typename > class Container, class Equal >
std::pair< DynList< DynList< T > >, size_tAleph::sequential_groups (const Container< T > &c, Equal &eq)
 Group consecutive equal elements together.
 
template<typename T , template< typename > class Container, class Equal = std::equal_to<T>>
std::pair< DynList< DynList< T > >, size_tAleph::sequential_groups (const Container< T > &c, Equal &&eq=Equal())
 
template<typename T , template< typename > class Container, class Equal >
std::pair< DynList< T >, size_tAleph::unique_sequential (const Container< T > &c, Equal &eq)
 Extract unique consecutive items.
 
template<typename T , template< typename > class Container, class Equal = std::equal_to<T>>
std::pair< DynList< T >, size_tAleph::unique_sequential (const Container< T > &c, Equal &&eq=Equal())
 
template<class C1 , class C2 >
Pair_Iterator< typename C1::Iterator, typename C2::Iterator > Aleph::get_pair_it (const C1 &c1, const C2 &c2)
 Create a Pair_Iterator for two containers.
 
template<class C1 , class C2 >
Pair_Iterator< typename C1::Iterator, typename C2::Iterator > Aleph::get_pair_it (const C1 &c1, const C2 &c2, size_t pos)
 
template<class C , typename ... Args>
size_t Aleph::insert_in_container (C &c, Args ... args)
 Insert multiple items into a container.
 
template<class C , typename ... Args>
size_t Aleph::append_in_container (C &c, Args ... args)
 Append multiple items into a container.
 
template<class C , typename ... Args>
Aleph::build_container (Args ... args)
 Build a container with the given items.
 
template<class SrcC , class TgtC >
TgtC Aleph::assign_container (const SrcC &srcc)
 Convert one container type to another.
 
template<typename T , typename ... Args>
DynList< TAleph::build_dynlist (Args ... args)
 Build a DynList with the given items.
 
template<class C , typename ... Args>
size_t Aleph::remove_from_container (C &c, Args ... args)
 Remove multiple items from a container.
 
template<typename T , template< typename > class Container>
DynList< TAleph::join (const Container< T > &c1, const Container< T > &c2)
 
template<typename T , template< typename > class Container>
DynList< TAleph::intercept (const Container< T > &c1, const Container< T > &c2)
 
template<typename T , template< typename > class Container>
DynList< TAleph::unique (const Container< T > &c)
 
template<typename T , template< typename > class Container>
DynList< TAleph::repeated (const Container< T > &c)
 
template<typename T , template< typename > class Container>
DynList< std::pair< T, size_t > > Aleph::repeated_with_index (const Container< T > &c)
 
template<typename T , template< typename > class C1, template< typename > class C2>
DynList< TAleph::flatten (const C2< C1< T > > &c)
 Flatten a nested container of one level.
 
template<typename T , template< typename > class C1, template< typename > class C2, template< typename > class C3>
DynList< TAleph::flatten (const C3< C2< C1< T > > > &c)
 Flatten a nested container of two levels.
 
template<typename T , template< typename > class C1, template< typename > class C2, template< typename > class C3, template< typename > class C4>
DynList< TAleph::flatten (const C4< C3< C2< C1< T > > > > &c)
 Flatten a nested container of three levels.
 
template<typename T , template< typename > class C1, template< typename > class C2, template< typename > class C3, template< typename > class C4, template< typename > class C5>
DynList< TAleph::flatten (const C5< C4< C3< C2< C1< T > > > > > &c)
 Flatten a nested container of four levels.
 
template<typename T >
bool Aleph::is_inside (const T &val, const DynList< T > &values)
 Check if a value is present in a list.
 
template<typename T >
bool Aleph::is_equal (const T &val)
 Variadic check for equality against multiple values.
 
template<typename T , typename U , typename ... Args>
bool Aleph::is_equal (const T &val, const U &rhs, const Args &... args)
 
template<class Container , class Operation >
bool Aleph::none (const Container &container, Operation &operation)
 Return true if no element satisfies operation.
 
template<class Container , class Operation >
bool Aleph::none (const Container &container, Operation &&operation=Operation())
 
template<class Container , class Pred >
Container::Item_Type * Aleph::find_ptr (Container &container, Pred &pred)
 Find the first element satisfying pred.
 
template<class Container , class Pred >
const Container::Item_Type * Aleph::find_ptr (const Container &container, Pred &pred)
 
template<class Container , class Pred >
Container::Item_Type * Aleph::find_ptr (Container &container, Pred &&pred)
 
template<class Container , class Pred >
const Container::Item_Type * Aleph::find_ptr (const Container &container, Pred &&pred)
 
template<class Container , class Pred >
std::optional< typename Container::Item_Type > Aleph::find_opt (const Container &container, Pred &pred)
 Find the first element satisfying pred (safe version).
 
template<class Container , class Pred >
std::optional< typename Container::Item_Type > Aleph::find_opt (const Container &container, Pred &&pred)
 
template<typename T , class Container , class Operation >
T Aleph::foldr (const Container &container, const T &init, Operation operation)
 Right fold (reduce).
 
template<class Container , typename T = typename Container::Item_Type>
T Aleph::sum (const Container &container, const T &init=T{})
 Compute sum of all elements.
 
template<class Container , typename T = typename Container::Item_Type>
T Aleph::product (const Container &container, const T &init=T{1})
 Compute product of all elements.
 
template<class C1 , class C2 >
DynList< typename C1::Item_Type > Aleph::concat (const C1 &c1, const C2 &c2)
 Concatenate two containers into a new DynList.
 
template<class Container , class Pred >
DynList< typename Container::Item_Type > Aleph::take_while (const Container &c, Pred pred)
 Return elements while predicate is true (take_while).
 
template<class Container , class Pred >
DynList< typename Container::Item_Type > Aleph::drop_while (const Container &c, Pred pred)
 Skip elements while predicate is true (drop_while).
 
template<class Container , class Op >
auto Aleph::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).
 
template<typename T , class Container >
DynList< TAleph::scanl_sum (const Container &container, const T &init)
 Compute running sums (scanl with addition).
 
template<typename T , class Container , class Op >
DynList< TAleph::scanl (const Container &container, const T &init, Op op)
 Prefix scan with custom operation.
 
template<class Container , class Cmp = std::less<typename Container::Item_Type>>
const Container::Item_Type * Aleph::min_ptr (const Container &container, Cmp cmp=Cmp())
 Find the minimum element in a container.
 
template<class Container , class Cmp = std::less<typename Container::Item_Type>>
const Container::Item_Type * Aleph::max_ptr (const Container &container, Cmp cmp=Cmp())
 Find the maximum element in a container.
 
template<class Container , class Cmp = std::less<typename Container::Item_Type>>
std::pair< const typename Container::Item_Type *, const typename Container::Item_Type * > Aleph::minmax_ptr (const Container &container, Cmp cmp=Cmp())
 Find both min and max elements in a single pass.
 
template<class Container , class Pred >
size_t Aleph::count_if (const Container &container, Pred pred)
 Count elements satisfying a predicate.
 
template<class Container >
bool Aleph::contains (const Container &container, const typename Container::Item_Type &value)
 Check if container contains a specific value.
 
template<class Container >
DynList< std::tuple< size_t, typename Container::Item_Type > > Aleph::enumerate_tuple (const Container &container)
 Zip containers with an index (enumerate with zip).
 
template<class Container1 , class Container2 >
DynList< std::pair< typename Container1::Item_Type, typename Container2::Item_Type > > Aleph::zip_longest (const Container1 &a, const Container2 &b, const typename Container1::Item_Type &fill_a=typename Container1::Item_Type(), const typename Container2::Item_Type &fill_b=typename Container2::Item_Type())
 Zip two containers, padding the shorter one with a fill value.
 
template<class Container1 , class Container2 >
DynList< std::tuple< typename Container1::Item_Type, typename Container2::Item_Type > > Aleph::tzip_longest (const Container1 &a, const Container2 &b, const typename Container1::Item_Type &fill_a=typename Container1::Item_Type(), const typename Container2::Item_Type &fill_b=typename Container2::Item_Type())
 Zip two containers into tuples, padding the shorter one.
 
template<class Container1 , class Container2 >
DynList< std::pair< std::optional< typename Container1::Item_Type >, std::optional< typename Container2::Item_Type > > > Aleph::zip_longest_opt (const Container1 &a, const Container2 &b)
 Zip two containers using optionals for missing values.
 
template<typename T , template< typename > class Container, class KeyFunc >
DynList< std::pair< std::invoke_result_t< KeyFunc, const T & >, DynList< T > > > Aleph::group_by (const Container< T > &c, KeyFunc key_func)
 Group consecutive elements by a key function.
 
template<typename T , template< typename > class Container, class KeyFunc , class KeyEqual >
DynList< std::pair< std::invoke_result_t< KeyFunc, const T & >, DynList< T > > > Aleph::group_by_eq (const Container< T > &c, KeyFunc key_func, KeyEqual key_equal)
 Group consecutive elements by a key function with custom equality.
 
template<typename T , template< typename > class Container, class KeyFunc , class Reducer >
auto Aleph::group_by_reduce (const Container< T > &c, KeyFunc key_func, Reducer reducer) -> DynList< std::pair< std::invoke_result_t< KeyFunc, const T & >, std::invoke_result_t< Reducer, const DynList< T > & > > >
 Group consecutive elements and apply a reducer to each group.
 

Detailed Description

Functional programming utilities for Aleph-w containers.

This file contains various functional patterns like map, filter, fold, zip, range generation, and container manipulation helpers.

Main functions include:

  • Range generation: range(), nrange(), contiguous_range(), rep()
  • Iteration: for_each(), enum_for_each(), each()
  • Predicates: all(), exists(), none(), contains(), count_if()
  • Transformation: maps(), filter(), reverse(), flatten(), flat_map()
  • Folding: foldl(), foldr(), sum(), product(), scanl()
  • Zipping: zip(), zipEq(), tzip(), zip_longest(), tzip_longest(), zip_longest_opt()
  • Grouping: group_by(), group_by_eq(), group_by_reduce(), sequential_groups(), unique_sequential()
  • Finding: find_ptr(), find_opt(), min_ptr(), max_ptr(), minmax_ptr()
  • Comparison: eq(), lesser(), diff()
  • Partitioning: partition(), take_while(), drop_while()
Author
Leandro Rabindranath León

Definition in file ahFunctional.H.