Marjoram
0.01
Library for Functional Programming in C++
|
Either Monad, supporting constants and functions. More...
Classes | |
class | ma::EitherIterator< A, B > |
Right-biased iterator. More... | |
class | ma::ConstEitherIterator< A, B > |
Right-biased const iterator. More... | |
class | ma::Maybe< A > |
Maybe monad. More... | |
class | ma::Either< A, B > |
Either monad. More... | |
Either Monad, supporting constants and functions.
An instance of Either<A,B>
contains either an A
or a B
. Unlike std::variant
both A
and B
may be the same type.
Given a function that returns either a fully fledged Widget or a descriptive Error message.
Then client code can treat the above return value uniformly:
class ma::EitherIterator |
Right-biased iterator.
Allows mutable access to the right value of an Either.
Example:
Public Types | |
using | iterator_category = std::input_iterator_tag |
using | difference_type = size_t |
using | value_type = B |
using | reference = B & |
using | pointer = B * |
Public Member Functions | |
EitherIterator (Either< A, B > &Mb, bool start) | |
bool | operator!= (const EitherIterator< A, B > &other) |
reference | operator* () |
pointer | operator-> () |
EitherIterator & | operator++ () |
EitherIterator & | operator++ (int) |
class ma::ConstEitherIterator |
Right-biased const iterator.
Public Types | |
using | iterator_category = std::input_iterator_tag |
using | difference_type = size_t |
using | value_type = const B |
using | reference = const B & |
using | pointer = const B * |
Public Member Functions | |
ConstEitherIterator (const Either< A, B > &Mb, bool start) | |
bool | operator!= (const ConstEitherIterator< A, B > &other) |
reference | operator* () |
pointer | operator-> () |
ConstEitherIterator & | operator++ () |
ConstEitherIterator & | operator++ (int) |
class ma::Maybe |
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 |
|
inline |
b
.
|
inline |
Return result of applying predicate to stored value if there is one, false otherwise.
pred | Callable with const A& , returns bool convertible. |
|
inline |
|
inline |
|
inline |
Obtains contained value.
Undefined behavior if this Maybe does not contain a value.
|
inline |
Obtains contained value.
Undefined behavior if this Maybe does not contain a value.
|
inline |
If this object contains a value, returns it.
Otherwise returns dflt
.
|
inline |
Checks whether this instance contains a value.
|
inline |
Checks whether this instance does not contain a value.
|
inline |
Returns maybe containing result of f(a)
if this holds a value, otherwise returns Nothing.
f | Function object. |
Type requirement:
F::operator()
when called with const A&
argument has non-void return type B
.Maybe<B>
containing the result of f(a)
or Nothing
.
|
inline |
Returns maybe containing result of f(a)
if this holds a value, otherwise returns Nothing.
f | Function object. |
Type requirement:
F::operator()
when called with argument of type A&&
has non-void return type B
.Maybe<B>
containing the result of f(a)
or Nothing
.
|
inline |
|
inline |
class ma::Either |
Either monad.
Disjoint union of two types.
Note that a Either is a monad if one of the type arguments are fixed, we chose the left one to remain fixed under flatMap, i.e.,
Where C is the result of F(A).
Terminology: If an Either<A, B> contains an A value, we say it has a left value, similarly for B and right.
Public Types | |
using | value_type = B |
using | left_type = A |
using | right_type = B |
Public Member Functions | |
template<typename... Args, typename = typename std::enable_if< std::is_constructible<A, Args...>::value ^ std::is_constructible<B, Args...>::value>::type> | |
Either (Args &&... args) | |
Convenience constructor that infers the Either's side (right or left). More... | |
template<typename... Args> | |
Either (LeftSide, Args &&... args) | |
Construct left Either<A, B> containing an A. More... | |
template<typename... Args> | |
Either (RightSide, Args &&... args) | |
Construct right Either<A, B> containing an B. More... | |
bool | isLeft () const |
Checks whether an A is stored. More... | |
bool | isRight () const |
Checks whether a B is stored. More... | |
template<typename Fa , typename Fb > | |
auto | fold (Fa fa, Fb fb) const -> std::result_of_t< Fa(A)> |
Reduces Either<A, B> to single value via two function objects. More... | |
template<typename Fa , typename Fb > | |
auto | fold (Fa fa, Fb fb) -> std::result_of_t< Fa(A)> |
Reduces Either<A, B> to single value via two function objects. More... | |
template<typename Fb > | |
auto | flatMap (Fb fb) const -> std::result_of_t< Fb(B)> |
Applies supplied function to stored B value if one is available. More... | |
template<typename Fb > | |
auto | flatMap (Fb fb) -> std::result_of_t< Fb(B)> |
Applies supplied function to stored B value if one is available. More... | |
template<typename Fb > | |
auto | map (Fb fb) -> Either< A, std::result_of_t< Fb(B)>> |
Applies supplied function to stored B value if one is available and wraps the result in Either. More... | |
template<typename Fb > | |
auto | map (Fb fb) const -> Either< A, std::result_of_t< Fb(B)>> |
Applies supplied function to stored B value if one is available and wraps result in Either. More... | |
template<class Predicate > | |
bool | exists (Predicate pred) const |
If right sided either, return result of applying predicate on right. More... | |
const B & | getOrElse (const B &dflt) |
Returns contained right value or given default value. | |
Either< B, A > | mirror () const |
Swaps left and right, preserving the stored value. More... | |
Maybe< B > | toMaybe () const |
If a B is contained, returns Just(B) . More... | |
template<typename C > | |
bool | contains (const C &c) |
template<typename Fa > | |
B | recover (Fa fa) const |
template<class Dummy = A> | |
auto | merge () const -> typename std::enable_if< std::is_same< A, B >::value, const Dummy &>::type |
If both left and right have the same type, allows merging to common type. More... | |
template<class BB = B> | |
auto | rightJoin () const -> typename std::enable_if< std::is_same< Either< typename BB::left_type, typename BB::right_type >, right_type >::value &&std::is_same< left_type, typename BB::left_type >::value, Either< left_type, typename BB::right_type >>::type |
Joins instance of Either through right. More... | |
template<class AA = A> | |
auto | leftJoin () const -> typename std::enable_if< std::is_same< Either< typename AA::left_type, typename AA::right_type >, left_type >::value &&std::is_same< right_type, typename AA::right_type >::value, Either< typename AA::left_type, right_type >>::type |
Joins instance of Either through left. More... | |
EitherIterator< A, B > | begin () |
ConstEitherIterator< A, B > | begin () const |
ConstEitherIterator< A, B > | cbegin () const |
EitherIterator< A, B > | end () |
ConstEitherIterator< A, B > | end () const |
ConstEitherIterator< A, B > | cend () const |
Private Member Functions | |
B & | asRight () |
Returns stored B value. More... | |
const B & | asRight () const |
Returns stored B value. More... | |
A & | asLeft () |
Returns stored A value. More... | |
const A & | asLeft () const |
Returns stored A value. More... | |
Private Attributes | |
EitherSide | side |
Indicates whether this contains an A or a B. | |
|
inline |
Convenience constructor that infers the Either's side (right or left).
Disabled if both A and B can be constructed from the input arguments.
|
inline |
|
inline |
Construct right Either<A, B> containing an B.
Note that RightSide is just a tag, a const static instance is declared as ma::Right for convenience.
|
inline |
B
value that compares equal to argument b
.
|
inline |
If right sided either, return result of applying predicate on right.
Otherwise return false.
pred | Function object, when called with argument of type A must return bool convertible. |
|
inline |
Applies supplied function to stored B
value if one is available.
fb | Function object. F::operator() when called with const B& has return type Either<A, C> . |
B
value, the result of fb(b)
is stored in the returned either. Otherwise, the pre-existing A
value is copied into the return value.
|
inline |
Applies supplied function to stored B
value if one is available.
fb | Function object. F::operator() when called with B&& has return type Either<A, C> . |
B
value, the result of fb(b)
is stored in the returned either. Otherwise, the pre-existing A
value is copied into the return value.
|
inline |
Reduces Either<A, B> to single value via two function objects.
fa | Function object that can be applied to an A . |
fb | Function object that can be applied to a B . |
fa(a)
or fb(b)
depending on which kind of value is contained.Note that both fa
and fb
must have the same return type when applied to their respective arguments.
|
inline |
Reduces Either<A, B> to single value via two function objects.
fa | Function object that can be applied to an A . |
fb | Function object that can be applied to a B . |
fa(a)
or fb(b)
depending on which kind of value is contained.Note that both fa
and fb
must have the same return type when applied to their respective arguments.
|
inline |
Checks whether an A
is stored.
Either<A, B>
contains an A value.
|
inline |
Checks whether a B
is stored.
Either<A, B>
contains a B
value.
|
inline |
|
inline |
Applies supplied function to stored B
value if one is available and wraps the result in Either.
fb | Function object. F::operator() when called with B&& has return type Either<A, C> . |
B
value, the result of fb(b)
is stored in the returned either. Otherwise, the pre-existing A
value is copied into the return value.
|
inline |
Applies supplied function to stored B
value if one is available and wraps result in Either.
fb | Function object. F::operator() callable with const B& and non-void return type. |
B
value, the result of fb(b)
is stored in the returned either. Otherwise, the pre-existing A
value is copied into the return value.
|
inline |
If both left and right have the same type, allows merging to common type.
Dummy
is to allow SFINAE.
|
inline |
Swaps left and right, preserving the stored value.
|
inline |
Fa | Function object. F::operator() when called with A value has return type B . |
Fa
to a
.
|
inline |
|
inline |
If a B
is contained, returns Just(B)
.
Otherwise returns Nothing
.
Maybe<B>
containing either the B or nothing.