Maybe Monad, supporting constants and functions.
More...
|
template<typename A > |
Maybe< std::decay_t< A > > | ma::Just (A &&a) |
| Convenience constructor, copies/moves a into new Maybe instance.
|
|
Maybe Monad, supporting constants and functions.
Convertible to any kind of (empty) Maybe object.
template <class T> class Maybe;
Extension to std::optional
/boost::optional
, endowing it with flatMap
.
Example
class Widget;
class BetterWidget;
BetterWidget morph(Widget& w);
Maybe<Widget> requestWidget(double length, int numberOfBells) {
if (failure) {
}
return widget;
}
The return value of requestWidget
can be used regardless of whether the operation succeeded by using map
:
auto mw = requestWidget(14.2, 3);
Maybe<BetterWidget> morphed = mw.map([](Widget& w) { return morph(w); });
- See also
- Maybe0
◆ ma::Maybe
template<typename A>
class ma::Maybe< A >
Maybe monad.
May contain a value or Nothing.
Throughout the documentation, if an object of type Maybe<A>
contains a value, this value is denoted by a
.
Type requirements: A must be a value type
Public Types |
using | value_type = A |
|
Public Member Functions |
| Maybe (Nothing_t) |
| New empty object (containing Nothing ).
|
|
| Maybe () |
| New empty object (containing Nothing ).
|
|
| Maybe (const A &a) |
| Copy a into new Maybe instance.
|
|
| Maybe (A &&a) |
| Move a into new Maybe instance.
|
|
| Maybe (const Maybe< A > &Ma)=default |
| Copy Maybe instance.
|
|
| Maybe (Maybe< A > &&Ma)=default |
| Move Maybe instance.
|
|
Maybe< A > & | operator= (const Maybe< A > &Ma)=default |
|
Maybe< A > & | operator= (Maybe< A > &&Ma)=default |
|
template<typename F > |
auto | flatMap (F f) const -> std::result_of_t< F(A)> |
| Returns result of f(a) wrapped in a Maybe if this holds a value, otherwise returns Nothing. More...
|
|
template<typename F > |
auto | flatMap (F f) -> std::result_of_t< F(A)> |
| Returns result of f(a) if this holds a value, otherwise returns Nothing. More...
|
|
template<typename F > |
auto | map (F f) const -> Maybe< std::result_of_t< F(A)>> |
| Returns maybe containing result of f(a) if this holds a value, otherwise returns Nothing. More...
|
|
template<typename F > |
auto | map (F f) -> Maybe< std::result_of_t< F(A)>> |
| Returns maybe containing result of f(a) if this holds a value, otherwise returns Nothing. More...
|
|
template<typename B > |
bool | contains (const B &b) const |
|
template<class Left > |
Either< Left, A > | toRight (const Left &left) const |
|
template<class Right > |
Either< A, Right > | toLeft (const Right &right) const |
|
bool | isJust () const |
| Checks whether this instance contains a value. More...
|
|
bool | isNothing () const |
| Checks whether this instance does not contain a value. More...
|
|
const A & | get () const |
| Obtains contained value. More...
|
|
A & | get () |
| Obtains contained value. More...
|
|
const A & | getOrElse (const A &dflt) const |
| If this object contains a value, returns it. More...
|
|
template<class Predicate > |
bool | exists (Predicate pred) const |
| Return result of applying predicate to stored value if there is one, false otherwise. More...
|
|
MaybeIterator< A > | begin () |
|
ConstMaybeIterator< A > | begin () const |
|
ConstMaybeIterator< A > | cbegin () const |
|
MaybeIterator< A > | end () |
|
ConstMaybeIterator< A > | end () const |
|
ConstMaybeIterator< A > | cend () const |
|
◆ contains()
template<typename A>
template<typename B >
bool ma::Maybe< A >::contains |
( |
const B & |
b | ) |
const |
|
inline |
- Returns
- true iff this maybe instance contains a value that compares true to
b
.
◆ exists()
template<typename A>
template<class Predicate >
bool ma::Maybe< A >::exists |
( |
Predicate |
pred | ) |
const |
|
inline |
Return result of applying predicate to stored value if there is one, false otherwise.
- Parameters
-
pred | Callable with const A& , returns bool convertible. |
◆ flatMap() [1/2]
template<typename A>
template<typename F >
auto ma::Maybe< A >::flatMap |
( |
F |
f | ) |
const -> std::result_of_t<F(A)> |
|
inline |
Returns result of f(a)
wrapped in a Maybe if this holds a value, otherwise returns Nothing.
- Parameters
-
Type requirement:
F::operator()
when called with const A&
argument has return type Maybe<B>
, where B
is non void.
- Returns
Maybe<B>
containing the result of f(a)
or Nothing
.
◆ flatMap() [2/2]
template<typename A>
template<typename F >
auto ma::Maybe< A >::flatMap |
( |
F |
f | ) |
-> std::result_of_t<F(A)> |
|
inline |
Returns result of f(a)
if this holds a value, otherwise returns Nothing.
- Parameters
-
Type requirement:
F::operator()
when called with A&&
argument has return type Maybe<B>
, where B
is non void
- Returns
Maybe<B>
containing the result of f(a)
or Nothing
.
◆ get() [1/2]
Obtains contained value.
Undefined behavior if this Maybe does not contain a value.
- Returns
- const reference to contained value.
◆ get() [2/2]
Obtains contained value.
Undefined behavior if this Maybe does not contain a value.
- Returns
- reference to contained value.
◆ getOrElse()
template<typename A>
const A& ma::Maybe< A >::getOrElse |
( |
const A & |
dflt | ) |
const |
|
inline |
If this object contains a value, returns it.
Otherwise returns dflt
.
- Returns
- reference to contained value.
◆ isJust()
Checks whether this instance contains a value.
- Returns
- true if this instance contains a value.
◆ isNothing()
Checks whether this instance does not contain a value.
- Returns
- true if this instance does not contain a value.
◆ map() [1/2]
template<typename A>
template<typename F >
auto ma::Maybe< A >::map |
( |
F |
f | ) |
const -> Maybe<std::result_of_t<F(A)>> |
|
inline |
Returns maybe containing result of f(a)
if this holds a value, otherwise returns Nothing.
- Parameters
-
Type requirement:
F::operator()
when called with const A&
argument has non-void return type B
.
- Returns
Maybe<B>
containing the result of f(a)
or Nothing
.
◆ map() [2/2]
template<typename A>
template<typename F >
Returns maybe containing result of f(a)
if this holds a value, otherwise returns Nothing.
- Parameters
-
Type requirement:
F::operator()
when called with argument of type A&&
has non-void return type B
.
- Returns
Maybe<B>
containing the result of f(a)
or Nothing
.
◆ toLeft()
template<typename A>
template<class Right >
- Returns
- ma::Either containing either the stored value or the argument.
◆ toRight()
template<typename A>
template<class Left >
- Returns
- ma::Either containing either the stored value or the argument.
◆ ma::MaybeIterator
template<typename A>
class ma::MaybeIterator< A >
Iterator over Maybe.
Allows mutable access to value contained. If the Maybe instance from which the iterator was created contains a value, the loop body is executed.
Example:
Maybe<A> Ma;
[...]
for (A& a: Ma) {
}
- See also
- ConstMaybeIterator
Public Types |
using | iterator_category = std::input_iterator_tag |
|
using | difference_type = size_t |
|
using | value_type = A |
|
using | reference = A & |
|
using | pointer = A * |
|
Public Member Functions |
| MaybeIterator (Maybe< A > &Ma, bool start) |
|
bool | operator!= (const MaybeIterator< A > &other) |
|
reference | operator* () |
|
pointer | operator-> () |
|
MaybeIterator & | operator++ () |
|
MaybeIterator & | operator++ (int) |
|
◆ ma::ConstMaybeIterator
class ma::ConstMaybeIterator |
template<typename A>
class ma::ConstMaybeIterator< A >
Immutable Iterator over Maybe.
- See also
- MaybeIterator
Public Types |
using | iterator_category = std::input_iterator_tag |
|
using | difference_type = size_t |
|
using | value_type = const A |
|
using | reference = const A & |
|
using | pointer = const A * |
|
Public Member Functions |
| ConstMaybeIterator (const Maybe< A > &Ma, bool start) |
|
bool | operator!= (const ConstMaybeIterator< A > &other) |
|
reference | operator* () |
|
pointer | operator-> () |
|
ConstMaybeIterator & | operator++ () |
|
ConstMaybeIterator & | operator++ (int) |
|
◆ ma::Nothing_t