Files
Library/include/Void/Functional/Pipeline.hpp
T
faumarayandClaude Fable 5 28b94883fd Restructure into umbrella modules (~55 -> 16 targets)
Headers move, CMake targets merge, C++ namespaces stay unchanged:
- Core: Scope, Lazy, Counted, Flags, Hash, Bits, Uuid, TypeList, TypeMap, Variant
- Functional: Pipeline, Either, Result, Retry
- Containers: Ring, Pool, PriorityQueue, LRU, Trie, Graph, DisjointSet, Bitset, Interval
- Math: +Matrix, Numeric, Random, Histogram
- Text: Str, Format, Tokenizer, Diff, Encoding, Semver
- Data: Json, Csv, Ini
- Async: Coro, Concurrency, EventBus, Observable
- Time: +Bench
- Log: Log facade + Logging framework (facade now header-only, breaking
  the Stacktrace<->Log link cycle the merge would have created)
- System: FileSystem + Process
- Configuration: +Argparse
- Unchanged: Stacktrace, Plugin, StateMachine, Testing, TestingMock, Detail

Module system fixes:
- OPTIONAL_DEPS resolution deferred to void_finalize_modules() so it no
  longer depends on the alphabetical discovery order of src/ subdirs
  (Testing -> Text would have been silently dropped).
- Every header-only module now has a tracked module.cmake; previously the
  empty src/<Name> marker dirs were untracked, so a fresh clone would have
  silently skipped those modules.
- VOID_HAS_LOGGING/CONCURRENCY/STR consumers renamed to LOG/ASYNC/TEXT.

Tests move to tests/standalone/<Umbrella>/<Feature>.cpp (Result suite files
prefixed: ResultBasic etc.). Test count verified identical (831) before and
after. README/CLAUDE.md module tables updated.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-12 18:50:02 +03:00

137 lines
4.4 KiB
C++

#pragma once
#include <functional>
#include <type_traits>
#include <utility>
namespace Void::Pipeline
{
// ── Pipe<T> ───────────────────────────────────────────────────────────────────
// Wraps a value and lets you chain single-argument callables with operator|.
//
// int result = pipe(5)
// | [](int x) { return x * 2; }
// | [](int x) { return x + 1; }; // → 11
template <typename T>
class Pipe
{
public:
explicit constexpr Pipe(T&& v) : value_{std::forward<T>(v)} {}
[[nodiscard]] constexpr T& get() noexcept { return value_; }
[[nodiscard]] constexpr const T& get() const noexcept { return value_; }
constexpr operator T() const noexcept(std::is_nothrow_copy_constructible_v<T>)
{
return value_;
}
template <typename F>
[[nodiscard]] constexpr auto operator|(F&& fn) &&
-> Pipe<std::invoke_result_t<F, T>>
{
return Pipe<std::invoke_result_t<F, T>>{
std::invoke(std::forward<F>(fn), std::move(value_))};
}
template <typename F>
[[nodiscard]] constexpr auto operator|(F&& fn) const &
-> Pipe<std::invoke_result_t<F, const T&>>
{
return Pipe<std::invoke_result_t<F, const T&>>{
std::invoke(std::forward<F>(fn), value_)};
}
private:
T value_;
};
template <typename T>
[[nodiscard]] constexpr Pipe<std::decay_t<T>> pipe(T&& v)
{
return Pipe<std::decay_t<T>>{std::forward<T>(v)};
}
// ── compose ───────────────────────────────────────────────────────────────────
// Compose callables left-to-right: compose(f, g)(x) = g(f(x)).
namespace Detail
{
template <typename F, typename G>
struct Composed
{
F f;
G g;
template <typename T>
constexpr auto operator()(T&& x) const
{
return std::invoke(g, std::invoke(f, std::forward<T>(x)));
}
};
} // namespace Detail
template <typename F, typename G>
[[nodiscard]] constexpr auto compose(F&& f, G&& g)
{
return Detail::Composed<std::decay_t<F>, std::decay_t<G>>{
std::forward<F>(f), std::forward<G>(g)};
}
template <typename F, typename G, typename... Rest>
[[nodiscard]] constexpr auto compose(F&& f, G&& g, Rest&&... rest)
{
return compose(compose(std::forward<F>(f), std::forward<G>(g)),
std::forward<Rest>(rest)...);
}
// ── partial ───────────────────────────────────────────────────────────────────
// Bind leading arguments: partial(f, a, b)(c) = f(a, b, c).
template <typename F, typename... Bound>
[[nodiscard]] constexpr auto partial(F&& f, Bound&&... bound)
{
return [f = std::forward<F>(f), ...bound = std::forward<Bound>(bound)](auto&&... rest) mutable {
return std::invoke(f, bound..., std::forward<decltype(rest)>(rest)...);
};
}
// ── flip ──────────────────────────────────────────────────────────────────────
template <typename F>
[[nodiscard]] constexpr auto flip(F&& f)
{
return [f = std::forward<F>(f)](auto&& a, auto&& b, auto&&... rest) mutable {
return std::invoke(f,
std::forward<decltype(b)>(b),
std::forward<decltype(a)>(a),
std::forward<decltype(rest)>(rest)...);
};
}
// ── identity / constant / negate ─────────────────────────────────────────────
struct Identity {
template <typename T>
constexpr T&& operator()(T&& x) const noexcept { return std::forward<T>(x); }
};
inline constexpr Identity identity{};
template <typename T>
[[nodiscard]] constexpr auto constant(T&& v)
{
return [v = std::forward<T>(v)](auto&&...) { return v; };
}
template <typename Pred>
[[nodiscard]] constexpr auto negate(Pred&& pred)
{
return [pred = std::forward<Pred>(pred)](auto&&... args) mutable {
return !std::invoke(pred, std::forward<decltype(args)>(args)...);
};
}
} // namespace Void::Pipeline