Thoth
A functional, expressive, asynchronous C++26 webdev library
Loading...
Searching...
No Matches
Functional.hpp File Reference
#include <functional>
#include <expected>
#include <concepts>
#include <ranges>
#include <algorithm>
Include dependency graph for Functional.hpp:

Go to the source code of this file.

Namespaces

namespace  Thoth
 
namespace  Thoth::Utils
 

Functions

template<auto V>
constexpr auto Thoth::Utils::ToValue ()
 If the template parameter is a value, then returns it. If is a type then construct and returns it.
 
template<class T >
constexpr T Thoth::Utils::ToValue ()
 If the template parameter is a value, then returns it. If is a type then construct and returns it.
 
template<class Ret , class Class , class... Args>
constexpr auto Thoth::Utils::Const (Ret(Class::*ptr)(Args...) const)
 
template<class Ret , class Class , class... Args>
constexpr auto Thoth::Utils::Mut (Ret(Class::*ptr)(Args...))
 
template<class Ret , class Class , class... Args>
constexpr auto Thoth::Utils::ConstFn (Ret(Class::*ptr)(Args...) const)
 
template<class Ret , class Class , class... Args>
constexpr auto Thoth::Utils::MutFn (Ret(Class::*ptr)(Args...))
 
template<class Opt , class Err >
constexpr std::expected< typename std::remove_cvref_t< Opt >::value_type, Err > Thoth::Utils::ValueOr (Opt &&val, Err &&err)
 Monad friendly function to transform std::expected<std::optional<T>, Err> into std::expected<T, Err> with a given Err.
 
template<class Opt , class Err >
constexpr auto Thoth::Utils::ValueOrHof (Err &&err)
 @hof{ValueOr}
 
template<bool Negate, class Pred , class Trans , class Val >
requires std::predicate<Pred&, Val&> && std::invocable<Trans, Val>
constexpr std::expected< Val, std::invoke_result_t< Trans, Val > > Thoth::Utils::S_CallIfErrorImpl (Pred &&pred, Trans &&trans, Val &&value)
 
template<class Pred , class Trans , class Val >
constexpr auto Thoth::Utils::CallIfError (Pred &&pred, Trans &&trans, Val &&value)
 In the std::expected<Val, Err> context, returns the value if the predicate match, or the std::unexpected with transformation applied to the error otherwise.
 
template<auto Pred, class Trans , class Val >
constexpr auto Thoth::Utils::CallIfError (Trans &&trans, Val &&val)
 In the std::expected<Val, Err> context, returns the value if the predicate match, or the std::unexpected with transformation applied to the error otherwise.
 
template<auto Pred, auto Trans, class Val >
constexpr auto Thoth::Utils::CallIfError (Val &&val)
 In the std::expected<Val, Err> context, returns the value if the predicate match, or the std::unexpected with transformation applied to the error otherwise.
 
template<class Pred , class Trans , class Val >
constexpr auto Thoth::Utils::CallIfErrorNot (Pred &&pred, Trans &&trans, Val &&value)
 In the std::expected<Val, Err> context, returns the value if the predicate doesn't match, or the std::unexpected with transformation applied to the error otherwise.
 
template<auto Pred, class Trans , class Val >
constexpr auto Thoth::Utils::CallIfErrorNot (Trans &&trans, Val &&val)
 In the std::expected<Val, Err> context, returns the value if the predicate doesn't match, or the std::unexpected with transformation applied to the error otherwise.
 
template<auto Pred, auto Trans, class Val >
constexpr auto Thoth::Utils::CallIfErrorNot (Val &&val)
 In the std::expected<Val, Err> context, returns the value if the predicate doesn't match, or the std::unexpected with transformation applied to the error otherwise.
 
template<class Pred , class Trans >
constexpr auto Thoth::Utils::CallIfErrorHof (Pred &&pred, Trans &&trans)
 @hof{CallIfError}
 
template<auto Pred, class Trans >
constexpr auto Thoth::Utils::CallIfErrorHof (Trans &&trans)
 @hof{CallIfError}
 
template<auto Pred, auto Trans>
constexpr auto Thoth::Utils::CallIfErrorHof ()
 @hof{CallIfError}
 
template<class Pred , class Trans >
constexpr auto Thoth::Utils::CallIfErrorNotHof (Pred &&pred, Trans &&trans)
 @hof{CallIfErrorNot}
 
template<auto Pred, class Trans >
constexpr auto Thoth::Utils::CallIfErrorNotHof (Trans &&trans)
 @hof{CallIfErrorNot}
 
template<auto Pred, auto Trans>
constexpr auto Thoth::Utils::CallIfErrorNotHof ()
 @hof{CallIfErrorNot}
 
template<bool Negate, class Pred , class Val >
requires std::predicate<Pred&, Val&>
constexpr bool Thoth::Utils::S_TestIfImpl (Pred &&pred, Val &value)
 
template<bool Negate, class Pred , class Val , class Err >
requires std::predicate<Pred&, Val&>
constexpr std::expected< Val, std::decay_t< Err > > Thoth::Utils::S_ErrorIfImpl (Pred &&pred, Val &&value, Err &&error)
 
template<class Pred , class Val , class Err >
constexpr auto Thoth::Utils::ErrorIf (Pred &&pred, Val &&value, Err &&error)
 In a std::expected<Val, Err> context, transform an error into a value only if the predicate match.
 
template<auto Pred, class Val , class Err >
constexpr auto Thoth::Utils::ErrorIf (Val &&value, Err &&error)
 In a std::expected<Val, Err> context, transform an error into a value only if the predicate match.
 
template<class Pred , class Val , class Err >
constexpr auto Thoth::Utils::ErrorIfNot (Pred &&pred, Val &&value, Err &&error)
 In a std::expected<Val, Err> context, transform an error into a value only if the predicate doesn't match.
 
template<auto Pred, class Val , class Err >
constexpr auto Thoth::Utils::ErrorIfNot (Val &&value, Err &&error)
 In a std::expected<Val, Err> context, transform an error into a value only if the predicate doesn't match.
 
template<class Pred , class Err >
constexpr auto Thoth::Utils::ErrorIfHof (Pred &&pred, Err &&error)
 @hof{ErrorIf}
 
template<auto Pred, class Err >
constexpr auto Thoth::Utils::ErrorIfHof (Err &&error)
 @hof{ErrorIf}
 
template<class Pred , class Err >
constexpr auto Thoth::Utils::ErrorIfNotHof (Pred &&pred, Err &&error)
 @hof{ErrorIfNof}
 
template<auto Pred, class Err >
constexpr auto Thoth::Utils::ErrorIfNotHof (Err &&error)
 @hof{ErrorIfNot}
 
template<bool Negate, class Pred , class Trans , class Val >
requires std::predicate<Pred&, Val&> && std::invocable<Trans, Val>
constexpr auto Thoth::Utils::S_TransformOptIfImpl (Pred &&pred, Trans &&trans, Val &&value) -> std::optional< std::invoke_result_t< Trans, Val > >
 
template<bool Negate, class Pred , class Trans , class Val , class Err >
requires std::predicate<Pred&, Val&> && std::invocable<Trans, Val>
constexpr auto Thoth::Utils::S_TransformExpIfImpl (Pred &&pred, Trans &&trans, Val &&value, Err &&error) -> std::expected< std::invoke_result_t< Trans, Val >, std::decay_t< Err > >
 
template<class Pred , class Trans , class Val >
requires std::predicate<Pred&, Val&> && std::invocable<Trans, Val>
constexpr auto Thoth::Utils::TransformOptIf (Pred &&pred, Trans &&trans, Val &&value)
 In the std::optional<Val> context, returns the transformation applied to the value if the predicate match, or std::nullopt otherwise.
 
template<auto Pred, class Trans , class Val >
requires std::predicate<decltype(Pred), Val&> && std::invocable<Trans, Val>
constexpr auto Thoth::Utils::TransformOptIf (Trans &&trans, Val &&value)
 In the std::optional<Val> context, returns the transformation applied to the value if the predicate match, or std::nullopt otherwise.
 
template<auto Pred, auto Trans, class Val >
requires std::invocable<decltype(Trans), Val>
constexpr auto Thoth::Utils::TransformOptIf (Val &&value)
 In the std::optional<Val> context, returns the transformation applied to the value if the predicate match, or std::nullopt otherwise.
 
template<class Pred , class Trans , class Val >
requires std::predicate<Pred&, Val&> && std::invocable<Trans, Val>
constexpr auto Thoth::Utils::TransformOptIfNot (Pred &&pred, Trans &&trans, Val &&value)
 In the std::optional<Val> context, returns the transformation applied to the value if the predicate doesn't match, or std::nullopt otherwise.
 
template<auto Pred, class Trans , class Val >
requires std::predicate<decltype(Pred), Val&> && std::invocable<Trans, Val>
constexpr auto Thoth::Utils::TransformOptIfNot (Trans &&trans, Val &&value)
 In the std::optional<Val> context, returns the transformation applied to the value if the predicate doesn't match, or std::nullopt otherwise.
 
template<auto Pred, auto Trans, class Val >
requires std::invocable<decltype(Trans), Val>
constexpr auto Thoth::Utils::TransformOptIfNot (Val &&value)
 In the std::optional<Val> context, returns the transformation applied to the value if the predicate doesn't match, or std::nullopt otherwise.
 
template<class Pred , class Trans >
constexpr auto Thoth::Utils::TransformOptIfHof (Pred &&pred, Trans &&trans)
 @hof{TransformOptIf}
 
template<auto Pred, class Trans >
constexpr auto Thoth::Utils::TransformOptIfHof (Trans &&trans)
 @hof{TransformOptIf}
 
template<auto Pred, auto Trans>
constexpr auto Thoth::Utils::TransformOptIfHof ()
 @hof{TransformOptIf}
 
template<class Pred , class Trans >
constexpr auto Thoth::Utils::TransformOptIfNotHof (Pred &&pred, Trans &&trans)
 @hof{TransformOptIfNot}
 
template<auto Pred, class Trans >
constexpr auto Thoth::Utils::TransformOptIfNotHof (Trans &&trans)
 @hof{TransformOptIfNot}
 
template<auto Pred, auto Trans>
constexpr auto Thoth::Utils::TransformOptIfNotHof ()
 @hof{TransformOptIfNot}
 
template<class Pred , class Trans , class Val , class Err >
requires std::predicate<Pred&, Val&> && std::invocable<Trans, Val>
constexpr auto Thoth::Utils::TransformExpIf (Pred &&pred, Trans &&trans, Val &&value, Err &&error)
 In the std::expected<Val, Err> context, returns the transformation applied to the value if the predicate match, or std::unexpected{ error } otherwise.
 
template<auto Pred, class Trans , class Val , class Err >
requires std::predicate<decltype(Pred), Val&> && std::invocable<Trans, Val>
constexpr auto Thoth::Utils::TransformExpIf (Trans &&trans, Val &&value, Err &&error)
 In the std::expected<Val, Err> context, returns the transformation applied to the value if the predicate match, or std::unexpected{ error } otherwise.
 
template<auto Pred, auto Trans, class Val , class Err >
requires std::invocable<decltype(Trans), Val>
constexpr auto Thoth::Utils::TransformExpIf (Val &&value, Err &&error)
 In the std::expected<Val, Err> context, returns the transformation applied to the value if the predicate match, or std::unexpected{ error } otherwise.
 
template<class Pred , class Trans , class Val , class Err >
requires std::predicate<Pred&, Val&> && std::invocable<Trans, Val>
constexpr auto Thoth::Utils::TransformExpIfNot (Pred &&pred, Trans &&trans, Val &&value, Err &&error)
 In the std::expected<Val, Err> context, returns the transformation applied to the value if the predicate doesn't match, or std::unexpected{ error } otherwise.
 
template<auto Pred, class Trans , class Val , class Err >
requires std::predicate<decltype(Pred), Val&> && std::invocable<Trans, Val>
constexpr auto Thoth::Utils::TransformExpIfNot (Trans &&trans, Val &&value, Err &&error)
 In the std::expected<Val, Err> context, returns the transformation applied to the value if the predicate doesn't match, or std::unexpected{ error } otherwise.
 
template<auto Pred, auto Trans, class Val , class Err >
requires std::invocable<decltype(Trans), Val>
constexpr auto Thoth::Utils::TransformExpIfNot (Val &&value, Err &&error)
 In the std::expected<Val, Err> context, returns the transformation applied to the value if the predicate doesn't match, or std::unexpected{ error } otherwise.
 
template<class Pred , class Trans , class Err >
constexpr auto Thoth::Utils::TransformExpIfHof (Pred &&pred, Trans &&trans, Err &&error)
 @hof{TransformExpIf}
 
template<auto Pred, class Trans , class Err >
constexpr auto Thoth::Utils::TransformExpIfHof (Trans &&trans, Err &&error)
 @hof{TransformExpIf}
 
template<auto Pred, auto Trans, class Err >
constexpr auto Thoth::Utils::TransformExpIfHof (Err &&error)
 @hof{TransformExpIf}
 
template<class Pred , class Trans , class Err >
constexpr auto Thoth::Utils::TransformExpIfNotHof (Pred &&pred, Trans &&trans, Err &&error)
 @hof{TransformExpIfNot}
 
template<auto Pred, class Trans , class Err >
constexpr auto Thoth::Utils::TransformExpIfNotHof (Trans &&trans, Err &&error)
 @hof{TransformExpIfNot}
 
template<auto Pred, auto Trans, class Err >
constexpr auto Thoth::Utils::TransformExpIfNotHof (Err &&error)
 @hof{TransformExpIfNot}
 
template<bool Negate, class Pred , class Val >
requires std::predicate<Pred&, Val&>
constexpr std::optional< std::remove_cvref_t< Val > > Thoth::Utils::S_NulloptIfImpl (Pred &&pred, Val &&value)
 
template<class Pred , class Val >
constexpr auto Thoth::Utils::NulloptIf (Pred &&pred, Val &&value)
 In a std::optional<T> context, transforms the value to std::nullopt if the predicate match.
 
template<auto Pred, class Val >
constexpr auto Thoth::Utils::NulloptIf (Val &&value)
 In a std::optional<T> context, transforms the value to std::nullopt if the predicate match.
 
template<class Pred , class Val >
constexpr auto Thoth::Utils::NulloptIfNot (Pred &&pred, Val &&value)
 In a std::optional<T> context, transforms the value to std::nullopt if the predicate doesn't match.
 
template<auto Pred, class Val >
constexpr auto Thoth::Utils::NulloptIfNot (Val &&value)
 In a std::optional<T> context, transforms the value to std::nullopt if the predicate doesn't match.
 
template<class Pred >
constexpr auto Thoth::Utils::NulloptIfHof (Pred &&pred)
 @hof{NulloptIf}
 
template<auto Pred>
constexpr auto Thoth::Utils::NulloptIfHof ()
 @hof{NulloptIf}
 
template<class Pred >
constexpr auto Thoth::Utils::NulloptIfNotHof (Pred &&pred)
 @hof{NulloptIfNot}
 
template<auto Pred>
constexpr auto Thoth::Utils::NulloptIfNotHof ()
 @hof{NulloptIfNot}
 
template<std::ranges::input_range R, class F , class Acc , class Exp = std::iter_value_t<R>, class Val = Exp::value_type, class Err = Exp::error_type>
requires std::same_as<Exp, std::expected<Val, Err>> && std::invocable<F, Acc, Val> && std::same_as<Acc, std::invoke_result_t<F, Acc, Val>>
constexpr std::expected< std::remove_cvref_t< Acc >, std::remove_cvref_t< Err > > Thoth::Utils::FoldWhileSuccess (R &&range, F &&callable, Acc &&initial={})
 fast fail foldr
 
template<class F , class Exp >
constexpr auto Thoth::Utils::FoldWhileSuccessHof (F &&callable, Exp &&reducer={})
 @hof{FoldWhileSuccess}
 
template<class T , class F >
requires std::invocable<F, T>
constexpr auto Thoth::Utils::Self (T &&val, F &&callable)
 
template<class F >
constexpr auto Thoth::Utils::SelfHof (F &&callable)