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

Unified functional programming for mixed STL/Aleph containers. More...

#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <list>
#include <deque>
#include <tclap/CmdLine.h>
#include <htlist.H>
#include <tpl_dynArray.H>
#include <ah-uni-functional.H>
Include dependency graph for uni_functional_example.C:

Go to the source code of this file.

Functions

void print_section (const string &title)
 
void print_subsection (const string &title)
 
template<typename Container >
void print_stl (const string &label, const Container &c)
 
template<typename Container >
void print_aleph (const string &label, const Container &c)
 
void demo_unified_api ()
 
void demo_map_filter ()
 
void demo_fold_reduce ()
 
void demo_predicates ()
 
void demo_access ()
 
void demo_slicing ()
 
void demo_statistics ()
 
void demo_conversion ()
 
void demo_practical ()
 
int main (int argc, char *argv[])
 

Detailed Description

Unified functional programming for mixed STL/Aleph containers.

This program demonstrates ah-uni-functional.H which provides functional programming operations that work with any container type - STL or Aleph. This unified interface eliminates the need to remember different APIs for different container types.

Key Feature: Universal Container Support

The Problem

Different container libraries have different APIs:

  • STL: std::transform, std::find_if, etc.
  • Aleph-w: maps(), filter(), etc.
  • Different syntax: Hard to remember multiple APIs

The Solution

ah-uni-functional.H provides unified functions that:

  • Work with any container type (STL or Aleph)
  • Automatically detect container type
  • Use same API regardless of container
  • Mix containers in same operation

Example

// Works with STL containers
std::vector<int> vec = {1, 2, 3};
auto squared = uni_map(vec, [](int x) { return x * x; });
// Works with Aleph containers
DynList<int> list;
list.append(1); list.append(2); list.append(3);
auto squared2 = uni_map(list, [](int x) { return x * x; });
// Same function, different containers!
auto uni_map(Op &&op, const Container &c)
Map operation - transform each element.

Functions Demonstrated

Transformation

Reduction

Predicates

Access

Slicing

  • **uni_take(n)**: Take first n elements
  • **uni_drop(n)**: Skip first n elements
  • **uni_take_while()**: Take while predicate true
  • **uni_drop_while()**: Drop while predicate true

Statistics

Comparison with Alternatives

Feature STL Aleph-w Unified
Container support STL only Aleph only Both
API consistency Different Different Same
Mix containers No No Yes
Type detection Manual Manual Automatic

Use Cases

Mixed Codebases

  • Legacy code: Existing STL containers
  • New code: Using Aleph-w containers
  • Unified API: Same functions for both

Library Integration

  • Third-party APIs: May return STL containers
  • Your code: Uses Aleph-w containers
  • Seamless: Convert and process easily

Migration

  • Gradual migration: Move from STL to Aleph-w
  • No API changes: Same functions work with both
  • Low risk: Can mix containers during migration

Performance Considerations

  • Type detection: Minimal overhead (compile-time)
  • Container operations: Same as underlying container
  • No extra copies: Operations are efficient

Usage

# Run all demonstrations
./uni_functional_example
# Run specific section
./uni_functional_example -s unified # Unified API overview
./uni_functional_example -s map # Map/filter demo
./uni_functional_example -s fold # Fold operations
./uni_functional_example -s predicates # all/exists/none/mem, etc.
./uni_functional_example -s access # first/last/nth/find
./uni_functional_example -s slicing # take/drop/take_while/drop_while
./uni_functional_example -s statistics # min/max/min_max
./uni_functional_example -s conversion # STL <-> Aleph conversions
./uni_functional_example -s practical # Practical examples
See also
ah-uni-functional.H Unified functional utilities
functional_example.C Aleph-w only functional (faster for Aleph containers)
stl_utils_example.C Container conversions (related)
Author
Leandro Rabindranath León
Date
2024

Definition in file uni_functional_example.C.

Function Documentation

◆ demo_access()

◆ demo_conversion()

◆ demo_fold_reduce()

◆ demo_map_filter()

◆ demo_practical()

◆ demo_predicates()

◆ demo_slicing()

◆ demo_statistics()

◆ demo_unified_api()

void demo_unified_api ( )

◆ main()

◆ print_aleph()

template<typename Container >
void print_aleph ( const string &  label,
const Container c 
)

◆ print_section()

void print_section ( const string &  title)

◆ print_stl()

template<typename Container >
void print_stl ( const string &  label,
const Container c 
)

◆ print_subsection()

void print_subsection ( const string &  title)