From c6a4aa13c256a65123355636d30f4a7b38c4fccf Mon Sep 17 00:00:00 2001 From: Steven Le Rouzic Date: Sat, 2 Nov 2024 20:18:32 +0100 Subject: Add option::transform and option::or_else --- asl/option.hpp | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) (limited to 'asl/option.hpp') diff --git a/asl/option.hpp b/asl/option.hpp index ebb9eef..0c816f4 100644 --- a/asl/option.hpp +++ b/asl/option.hpp @@ -429,6 +429,53 @@ public: } return un_cvref_t>{}; } + + template + constexpr auto transform(F&& f) & + { + using U = un_cvref_t>; + if (has_value()) + { + return option{ invoke(ASL_FWD(f), value()) }; + } + return option{}; + } + + template + constexpr auto transform(F&& f) const& + { + using U = un_cvref_t>; + if (has_value()) + { + return option{ invoke(ASL_FWD(f), value()) }; + } + return option{}; + } + + template + constexpr auto transform(F&& f) && + { + using U = un_cvref_t>; + if (has_value()) + { + return option{ invoke(ASL_FWD(f), ASL_MOVE(value())) }; + } + return option{}; + } + + template + constexpr option or_else(F&& f) const& + requires is_same>, option> + { + return has_value() ? *this : invoke(ASL_FWD(f)); + } + + template + constexpr option or_else(F&& f) && + requires is_same>, option> + { + return has_value() ? ASL_MOVE(*this) : invoke(ASL_FWD(f)); + } }; template -- cgit