A minimal C++ library providing a few functions to be able to work with
boost::optional
as a monad. The idea is to add std::optional
support in the
very near future.
The fmap
function applies the function object to the wrapped value, from
which it creates and returns a new optional. Very loosely speaking, this makes
the pair (optional<T>, fmap)
a functor.
fmap :: optional T -> (T -> S) -> optional S
auto fmap(const optional<T>& opt, Func&& func) -> optional<decltype(func(*opt))>;
The bind
function applies the function object to the wrapped value, unwraps
one layer of optional<T>
and returns a new optional
. The pair
(optional<T>, bind)
is, again very loosely speaking, a monad.
bind :: optional T -> (T -> optional S) -> optional S
auto bind(const optional<T>& opt, Func&& func) -> decltype(func(*opt));
The with
function is sort of a special case for when you want to mutate the
wrapped value in-place.
The first overload takes the optional<T>
by reference and mutates it.
with :: optional T& -> (T -> void) -> void
void with(optional<T>& opt, const Func& func);
The second overload takes the optional<T>
by rvalue reference and moves it to
the return value.
with ::: optional T&& -> (T -> void) -> optional T
auto with(boost::optional<T>&& opt, Func&& func) -> boost::optional<T>;
- Wrap in a suitable namespace (what name?
optm
,optmonad
,omonad
,omon
,opt_monad
,optional_monad
?) - Convert test to actual unit tests (let's try Catch).
- Hook up unit tests to CI system (Travis-CI?)
- Proper cross-platform build system (probably CMake).
- Currently hardcoded for
boost::optional
. Make it work withstd::optional
out-of-the-box. - Add examples to README.
- SFINAE checks for the function objects. This is needed for
operator<<
implementation.
Jon Haggblad [email protected]
Last update: 06 March 2017