1

I think that applying a function to an optional is a really useful pattern. It is however cumbersome to do with the C++ STL. For example:

std::optional<Vector3D> vec = tryCreateVector();
std::optional<float> length = 
    vec.has_value() ? std::optional<float>(vec->length()) : std::nullopt;

Is there an equivalent of haskell's fmap or rust's Option::map in C++? Something like the following:

std::optional<Vector3D> vec = tryCreateVector();
std::optional<float> length = map(vec, [](auto vec) { return vec.length(); });
decden
  • 679
  • 4
  • 19

2 Answers2

1

As far as I know standard library does not provide such functionality out of the box. It is fairly easy to implement it though.

#include <optional>
#include <iostream>
#include <functional>

std::optional<int> create_an_int()
{
    return 1;
}

std::optional<int> dont_create_an_int()
{
    return {};
}

template<typename T, typename F>
auto handler_wrapper(const std::optional<T>& in, F h)
{
    return in.has_value() ? std::optional{h(*in)} : std::nullopt;
}

int main()
{
    auto handler = [](const int& in){ return 3*in; };
    auto test = create_an_int();
    auto multiplied = handler_wrapper(test, handler);
    std::cout << *multiplied << std::endl;
    test = dont_create_an_int();
    auto nulled = handler_wrapper(test, handler);
    if (!nulled.has_value())
        std::cout << "null optional" << std::endl;
}

Basically all you need to do is to create a template wrapper accepting any callable and an optional and you are done (note: the snippet above is not the prettiest / best implementation, but should give you a good starting point I guess). The code above will obviously produce "3" and "null optional" as output.

paler123
  • 976
  • 6
  • 18
1

You could define the following function:

namespace detail
{
    template<typename Callable, typename T>
    struct apply_helper
    {
        using T_noref = typename std::remove_reference<T>::type;
        using value_type = typename T_noref::value_type;
        using Callable_return = decltype(std::declval<Callable>()(std::declval<value_type>()));
        using return_type = optional<Callable_return>;

        static return_type eval(Callable&& f, T&& val)
        {
            if(val)
            {
                return apply(std::forward<Callable&&>(f), *val);
            }
            else return boost::none;
        }

    private:
        static Callable_return apply(Callable&& f, value_type& v)
        {
            return f(v);
        }
        static Callable_return apply(Callable&& f, value_type const& v)
        {
            return f(v);
        }
        static Callable_return apply(Callable&& f, value_type&& v)
        {
            return f(v);
        }

    };
}
template<typename Callable, typename T> 
optional<decltype(std::declval<Callable>()(std::declval<T>()))> apply(Callable&& f, optional<T> const& a)
{
    return detail::apply_helper<Callable, optional<T> const&>::eval(std::forward<Callable>(f), a);
}

which could then be used like:

optional<int> foo(optional<int> value)
{
    auto f = [](int v){return v + 10;};
    return apply(f, value);
}
Paul Belanger
  • 2,354
  • 14
  • 23