header_utils
Loading...
Searching...
No Matches
Functional

Useful functions and types for functional-style programming. More...

Namespaces

namespace  ghassanpl::pred
 Contains functions that return predicates; appropriate for functions like std::all_of.
namespace  ghassanpl::op
 Contains functions that return functors that perform an operation without output; appropriate for functions like std::for_each.
namespace  ghassanpl::xf
 Contains functions that return functors which transform and return their inputs somehow; appropriate for not-in-place transformation functions (e.g. std::transform).

Classes

struct  ghassanpl::overloaded< Ts >
 Can be used to create an object with an overload set of operator() if constructed from multiple lambdas. More...
class  ghassanpl::mutlticast_function< R(ARGS...)>
 Like std::function<R(ARGS...)> but can reference multiple invocables, and call them all at once. More...

Macros

#define GHPL_OVERLOAD(...)
#define std_call(func)
 Macro that allows us to pass std:: functions to algorithms as if they were function pointers, because you can't take the address of an std:: function for some stupid reason.

Functions

template<typename... ARGS>
constexpr auto ghassanpl::make_single_time_function (std::function< void(ARGS...)> func)
 Returns a function that calls func when invoked, but only the first time.
template<typename FUNC>
constexpr auto ghassanpl::make_single_time_function (FUNC &&func)
 Returns a function that calls func when invoked, but only the first time.
template<typename T, typename FUNC>
constexpr auto ghassanpl::transformed (std::optional< T > const &value, FUNC &&func) -> decltype(std::optional{ func(value.value()) })
 Pre C++23 transform/fmap.
template<typename T, typename FUNC>
constexpr auto ghassanpl::transformed (std::optional< T > &&value, FUNC &&func) -> decltype(std::optional{ func(value.value()) })
 Pre C++23 transform/fmap.
template<typename T>
std::optional< T > ghassanpl::to_optional (T *value) noexcept
template<typename T>
std::optional< T > ghassanpl::move_to_optional (T *value) noexcept
template<typename T>
constexpr auto ghassanpl::flattened (std::optional< std::optional< T > > &&value)
template<typename T>
constexpr auto ghassanpl::flattened (std::optional< T > &&value)
template<typename T>
constexpr auto ghassanpl::flattened (std::optional< std::optional< T > > const &value)
template<typename T>
constexpr auto ghassanpl::flattened (std::optional< T > const &value)
template<typename T, typename FUNC>
constexpr T ghassanpl::resulting (FUNC &&func)
 Returns a new T transformed by func.
template<typename FUNC>
 ghassanpl::GHPL_REQUIRES ((!std::is_void_v< typename decltype(detail::detect_first_arg(std::function{ std::declval< FUNC >() }))::type > &&requires { std::function{ std::declval< FUNC >() };})) const expr auto resulting(FUNC &&func)
 Returns a new object transformed by func. The first argument to func determines what the object's type is.
template<typename T, typename... ARGS>
constexpr std::optional< T > ghassanpl::optional_get (std::variant< ARGS... > const &var) noexcept
 Variant operations.
template<typename FUNC, typename... Ts>
constexpr auto ghassanpl::transformed (std::variant< Ts... > const &var, FUNC &&func)
 Returns a new variant transformed by func.
template<typename... TYPES>
 ghassanpl::convertible_to_variant (std::variant< TYPES... >) -> convertible_to_variant< TYPES... >
template<typename T>
 ghassanpl::convertible_to_variant (T) -> convertible_to_variant< remove_cvref_t< T > >
template<typename FUNC, typename... Ts>
constexpr auto ghassanpl::transformed_flattened (std::variant< Ts... > const &var, FUNC &&func)
 Returns a new variant transformed by func, flattened.

Detailed Description

Useful functions and types for functional-style programming.

Additional useful delegates (like std::function<>)

Macro Definition Documentation

◆ GHPL_OVERLOAD

#define GHPL_OVERLOAD ( ...)

Definition at line 76 of file functional.h.

◆ std_call

#define std_call ( func)

Macro that allows us to pass std:: functions to algorithms as if they were function pointers, because you can't take the address of an std:: function for some stupid reason.

Definition at line 302 of file functional.h.

Function Documentation

◆ flattened() [1/4]

template<typename T>
auto ghassanpl::flattened ( std::optional< std::optional< T > > && value)
nodiscardconstexpr

Definition at line 59 of file functional.h.

◆ flattened() [2/4]

template<typename T>
auto ghassanpl::flattened ( std::optional< std::optional< T > > const & value)
nodiscardconstexpr

Definition at line 61 of file functional.h.

◆ flattened() [3/4]

template<typename T>
auto ghassanpl::flattened ( std::optional< T > && value)
nodiscardconstexpr

Definition at line 60 of file functional.h.

◆ flattened() [4/4]

template<typename T>
auto ghassanpl::flattened ( std::optional< T > const & value)
nodiscardconstexpr

Definition at line 62 of file functional.h.

◆ GHPL_REQUIRES()

template<typename FUNC>
ghassanpl::GHPL_REQUIRES ( (!std::is_void_v< typename decltype(detail::detect_first_arg(std::function{ std::declval< FUNC >() }))::type > &&requires { std::function{ std::declval< FUNC >() };}) ) const &&

Returns a new object transformed by func. The first argument to func determines what the object's type is.

Definition at line 330 of file functional.h.

◆ make_single_time_function() [1/2]

template<typename FUNC>
auto ghassanpl::make_single_time_function ( FUNC && func)
nodiscardconstexpr

Returns a function that calls func when invoked, but only the first time.

Definition at line 33 of file functional.h.

◆ make_single_time_function() [2/2]

template<typename... ARGS>
auto ghassanpl::make_single_time_function ( std::function< void(ARGS...)> func)
nodiscardconstexpr

Returns a function that calls func when invoked, but only the first time.

Definition at line 24 of file functional.h.

◆ move_to_optional()

template<typename T>
std::optional< T > ghassanpl::move_to_optional ( T * value)
noexcept

Definition at line 56 of file functional.h.

◆ optional_get()

template<typename T, typename... ARGS>
std::optional< T > ghassanpl::optional_get ( std::variant< ARGS... > const & var)
nodiscardconstexprnoexcept

Variant operations.

Returns an std::optional<T> with a copy of the value in the variant, if T is currently in the variant, or nullopt otherwise

Definition at line 346 of file functional.h.

◆ resulting()

template<typename T, typename FUNC>
T ghassanpl::resulting ( FUNC && func)
constexpr

Returns a new T transformed by func.

Definition at line 306 of file functional.h.

◆ to_optional()

template<typename T>
std::optional< T > ghassanpl::to_optional ( T * value)
noexcept

Definition at line 53 of file functional.h.

◆ transformed() [1/3]

template<typename T, typename FUNC>
auto ghassanpl::transformed ( std::optional< T > && value,
FUNC && func ) -> decltype(std::optional{ func(value.value()) })
nodiscardconstexpr

Pre C++23 transform/fmap.

Definition at line 47 of file functional.h.

◆ transformed() [2/3]

template<typename T, typename FUNC>
auto ghassanpl::transformed ( std::optional< T > const & value,
FUNC && func ) -> decltype(std::optional{ func(value.value()) })
nodiscardconstexpr

Pre C++23 transform/fmap.

Definition at line 40 of file functional.h.

◆ transformed() [3/3]

template<typename FUNC, typename... Ts>
auto ghassanpl::transformed ( std::variant< Ts... > const & var,
FUNC && func )
constexpr

Returns a new variant transformed by func.

Definition at line 399 of file functional.h.

◆ transformed_flattened()

template<typename FUNC, typename... Ts>
auto ghassanpl::transformed_flattened ( std::variant< Ts... > const & var,
FUNC && func )
constexpr

Returns a new variant transformed by func, flattened.

Definition at line 477 of file functional.h.