6 #ifndef KAGOME_MONADIC_UTILS_H 7 #define KAGOME_MONADIC_UTILS_H 10 #include <type_traits> 23 typename R = std::invoke_result_t<F, const T &>>
24 std::optional<R>
map_optional(std::optional<T>
const &opt, F
const &f) {
25 if (opt.has_value()) {
26 return std::optional<R>{f(opt.value())};
36 template <
typename T,
typename F,
typename R = std::invoke_result_t<F, T &&>>
37 std::optional<R>
map_optional(std::optional<T> &&opt, F
const &f) {
38 if (opt.has_value()) {
39 return std::optional<R>{f(std::move(opt.value()))};
52 typename R = std::invoke_result_t<F, const T &>>
53 outcome::result<R>
map_result(outcome::result<T>
const &res, F
const &f) {
54 if (res.has_value()) {
55 return outcome::result<R>{f(res.value())};
57 return res.as_failure();
66 template <
typename T,
typename F,
typename R = std::invoke_result_t<F, T &&>>
67 outcome::result<R>
map_result(outcome::result<T> &&res, F
const &f) {
68 if (res.has_value()) {
69 return outcome::result<R>{f(std::move(res.value()))};
71 return res.as_failure();
83 typename R = std::invoke_result_t<F, T const &>>
85 outcome::result<std::optional<T>>
const &res_opt, F
const &f) {
87 return map_optional(opt, [&f](
auto &v) {
return f(v); });
98 template <
typename T,
typename F,
typename R = std::invoke_result_t<F, T &&>>
100 outcome::result<std::optional<T>> &&res_opt, F
const &f) {
102 return map_optional(opt, [&f](
auto &v) {
return f(std::move(v)); });
108 #endif // KAGOME_MONADIC_UTILS_H
outcome::result< R > map_result(outcome::result< T > const &res, F const &f)
std::optional< R > map_optional(std::optional< T > const &opt, F const &f)
outcome::result< std::optional< R > > map_result_optional(outcome::result< std::optional< T >> const &res_opt, F const &f)