Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Functional Programming

Functional-style utilities (map/filter/fold), zip utilities, ranges adapters, and parallel variants. More...

Files

file  ah-generator.H
 Lazy sequence type (Aleph::Generator<T>) built on C++20 coroutines.
 
file  ah-parallel.H
 Parallel functional programming operations using ThreadPool.
 
file  ah-ranges.H
 C++20 Ranges support and adaptors for Aleph-w containers.
 
file  ah-stl-functional.H
 Functional programming utilities for C++ Standard Library containers.
 
file  ah-uni-functional.H
 Unified functional programming utilities for both STL and Aleph containers.
 
file  ah-zip-utils.H
 Unified zip operations for both STL and Aleph containers.
 
file  ah-zip.H
 Zip iterators and functional operations for multiple containers.
 

Classes

class  Aleph::Generator< T >
 Lazy, single-pass sequence of T values produced by a coroutine. More...
 
struct  Aleph::Generator< T >::promise_type
 The promise type the compiler instantiates for a coroutine returning Generator<T>. More...
 
class  Aleph::Generator< T >::iterator
 Single-pass input iterator over the yielded sequence. More...
 

Detailed Description

Functional-style utilities (map/filter/fold), zip utilities, ranges adapters, and parallel variants.

This module covers two related layers:

  1. Header-level utilities (zip views/iterators, unified functional helpers for STL/Aleph containers, ranges adapters, and parallel variants).
  2. Container methods: many Aleph-w containers (trees, lists, arrays, bit-vectors, etc.) expose functional methods directly (e.g. maps(), filter(), foldl()) via CRTP mixins.

Headers in this module

Container-level functional methods (via <tt>FunctionalMixin</tt>)

Many Aleph-w containers provide functional operations as member functions because they inherit from the CRTP mixin FunctionalMixin (see ah-dry-mixin.H).

In practice, this means that if a container provides a suitable traverse() method (directly or via TraverseMixin), it automatically gets methods such as:

Example: <tt>DynSetTree</tt> supports <tt>maps()</tt> and <tt>foldl()</tt>

See tpl_dynSetTree.H.

#include <tpl_dynSetTree.H>
#include <iostream>
using namespace Aleph;
int main()
{
DynSetTree<int, Avl_Tree> s;
for (int x : {1, 2, 3, 4, 5})
s.insert(x);
auto doubled = s.maps<int>([](int x) { return x * 2; });
int sum = s.foldl(0, [](int acc, int x) { return acc + x; });
(void) doubled;
std::cout << "sum=" << sum << "\n";
}

Building a container from its items: <tt>C::build()</tt>

The containers that inherit the CRTP mixin FunctionalMethods (see ah-dry.H): the lists, arrays, stacks, queues, heaps, sets and maps, provide a static build() that builds a container from its items:

#include <htlist.H>
#include <tpl_arrayStack.H>
#include <tpl_dynMapTree.H>
#include <tpl_dynSetTree.H>
#include <memory>
#include <string>
using namespace Aleph;
int main()
{
auto list = DynList<int>::build(3, 1, 2); // 3 1 2
auto set = DynSetTree<int>::build(3, 1, 2, 1); // 1 2 3: one item per key
auto stack = ArrayStack<int>::build(1, 2, 3); // 3 on top
auto map = DynMapTree<int, std::string>::build(std::pair{1, "one"},
std::pair{2, "two"});
auto ptrs = DynList<std::unique_ptr<int>>::build(std::make_unique<int>(1));
}

C::build(a, b, c) builds the same container as C{a, b, c}: each item goes, in argument order, through the append() of the container, so its rules apply. A stack ends with the last item on top, a queue with the first item at its front, and a set or a map keeps the first of two equal keys. Unlike the braces, the items are forwarded: an rvalue is moved, so move-only types can be stored, and an item may have any type implicitly convertible to the item type. Being a named function rather than a constructor, Array<int>::build(10) is always the one-element array {10}; a capacity is reserved with Array<int>::create_reserved(10).

A class derived from a container (DynSetAvlTree, DynMapRbTree, MapOLhash, ...) builds itself rather than its base. A class of your own that derives from a container declares that build() with the macro Derived_Build(Name). The free function build_container<C>(items...) remains for any type with append().

Example: <tt>BitArray</tt> is also a functional container

BitArray inherits from FunctionalMixin, so it also supports the same family of member functions. See bitArray.H.

Graph functional helpers

Graphs expose functional-style helpers, but they are generally specialized to node/arc traversal rather than being treated as a simple sequence container.

Common patterns include:

See tpl_graph.H and graph-dry.H.

#include <tpl_graph.H>
using namespace Aleph;
int main()
{
using Node = Graph_Node<int>;
using Arc = Graph_Arc<double>;
using G = List_Graph<Node, Arc>;
G g;
auto * a = g.insert_node(1);
auto * b = g.insert_node(2);
g.insert_arc(a, b, 1.5);
g.for_each_node([](auto * p) { (void) p; });
const auto n_gt_1 = g.count_nodes([](auto * p) { return p->get_info() > 1; });
(void) n_gt_1;
}