gimo v0.2.0
Loading...
Searching...
No Matches
algorithm

Monadic algorithms. More...

Concepts

concept  gimo::applicable_to
 Evaluates whether a Nullable type is compatible with the specific Algorithm.

Classes

class  gimo::BasicAlgorithm< Traits, Action >
 The basic building block for every monadic operation. More...

Functions

template<typename Action>
constexpr auto gimo::and_then (Action &&action)
 Creates a pipeline step that applies a function returning a nullable type.
template<typename Action>
constexpr auto gimo::or_else (Action &&action)
 Creates a pipeline step that handles the null/error case.
template<typename Action>
constexpr auto gimo::transform (Action &&action)
 Creates a pipeline step that transforms the underlying value.
template<typename Action>
constexpr auto gimo::transform_error (Action &&action)
 Creates a pipeline step that transforms the error of an expected-like type.
template<std::invocable Action>
constexpr auto gimo::value_or_else (Action &&action)
 Creates a terminating pipeline step that returns the contained value or invokes a fallback action if the nullable is null.
template<typename Alternative>
constexpr auto gimo::value_or (Alternative &&alternative)
 Creates a terminating pipeline step that returns the contained value or a specified alternative if the nullable is null.

Detailed Description

Monadic algorithms.

Function Documentation

◆ and_then()

template<typename Action>
auto gimo::and_then ( Action && action)
nodiscardconstexpr

Creates a pipeline step that applies a function returning a nullable type.

Template Parameters
ActionThe action type.
Parameters
actionA unary operation.
Returns
A Pipeline step containing the and_then algorithm.
  • On Value: Invokes the action with the underlying value of the input. The action must return a nullable type.
  • On Null: Propagates the null (or error) state immediately (i.e., action is not executed).

Let T be the (possibly cv-qualified) reference to the value extracted from the input nullable. Action must be invocable with an argument of type T (or a type to which T is implicitly convertible), while the decayed return-type will become the resulting nullable-type.

See also
https://en.wikipedia.org/wiki/Monad_(functional_programming)

◆ or_else()

template<typename Action>
auto gimo::or_else ( Action && action)
nodiscardconstexpr

Creates a pipeline step that handles the null/error case.

Template Parameters
ActionThe action type.
Parameters
actionA nullary operation.
Returns
A Pipeline step containing the or_else algorithm.

◆ transform()

template<typename Action>
auto gimo::transform ( Action && action)
nodiscardconstexpr

Creates a pipeline step that transforms the underlying value.

Template Parameters
ActionThe action type.
Parameters
actionA unary operation.
Returns
A Pipeline step containing the transform algorithm.
  • On Value: Invokes the action with the underlying value of the input. The result of this invocation is wrapped into a new instance of the nullable container.
  • On Null: Propagates the null (or error) state immediately (i.e., action is not executed).

Let T be the (possibly cv-qualified) reference to the value extracted from the input nullable. Action must be invocable with an argument of type T (or a type to which T is implicitly convertible), while the decayed return-type will become the value-type of the resulting nullable.

See also
https://en.wikipedia.org/wiki/Map_(higher-order_function)
Note
The nullable type must support value-type rebinding.
See also
gimo::traits::rebind_value

◆ transform_error()

template<typename Action>
auto gimo::transform_error ( Action && action)
nodiscardconstexpr

Creates a pipeline step that transforms the error of an expected-like type.

Template Parameters
ActionThe action type.
Parameters
actionA nullary operation.
Returns
A Pipeline step containing the or_else algorithm.
  • On Value: Propagates the value state immediately (i.e., action is not executed).
  • On Null: Invokes the action with the underlying error of the input. The result of this invocation is wrapped as an error into a new instance of the expected_like container.

Let T be the (possibly cv-qualified) reference to the error extracted from the input expected_like. Action must be invocable with an argument of type T (or a type to which T is implicitly convertible), while the decayed return-type will become the error-type of the resulting nullable.

See also
https://en.wikipedia.org/wiki/Monad_(functional_programming)
Note
The expected_like type must support error-type rebinding.
See also
gimo::traits::rebind_error

◆ value_or()

template<typename Alternative>
auto gimo::value_or ( Alternative && alternative)
nodiscardconstexpr

Creates a terminating pipeline step that returns the contained value or a specified alternative if the nullable is null.

Template Parameters
AlternativeThe alternative value type.
Parameters
alternativeThe alternative value to use if the nullable is null.
Returns
A Pipeline step containing the value_or algorithm.
  • On Value: Forwards the underlying value of the input.
  • On Null: Returns the specified alternative converted to the nullable's value type.

Alternative must be convertible to the nullable's value type.

◆ value_or_else()

template<std::invocable Action>
auto gimo::value_or_else ( Action && action)
nodiscardconstexpr

Creates a terminating pipeline step that returns the contained value or invokes a fallback action if the nullable is null.

Template Parameters
ActionThe fallback action type.
Parameters
actionA nullary operation that produces an alternative value.
Returns
A Pipeline step containing the value_or_else algorithm.
  • On Value: Forwards the underlying value of the input.
  • On Null: Invokes action and returns its result converted to the nullable's value type.

The action is invoked only if the input nullable does not contain a value. The return type of action must be convertible to the nullable's value type.