From cd722eb8495ebe9285242d37e1817e252d8f5659 Mon Sep 17 00:00:00 2001 From: Wojciech Migda Date: Sat, 16 Sep 2023 14:57:39 +0200 Subject: [PATCH] Refresh `neither` to its latest commit (#262) --- lib/include/neither/COMMIT | 7 +--- lib/include/neither/either.hpp | 63 ++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 6 deletions(-) diff --git a/lib/include/neither/COMMIT b/lib/include/neither/COMMIT index bf875704..b9ae89f2 100644 --- a/lib/include/neither/COMMIT +++ b/lib/include/neither/COMMIT @@ -1,8 +1,3 @@ https://github.com/LoopPerfect/neither -91d4878b411e5903253f2a9bf37b0edee95a77f0 - - -Changes: -- silenced warning in maybe.hpp -- rightMap && fix +d2db83d37e4ac84e50c51928bac4504b982983f0 diff --git a/lib/include/neither/either.hpp b/lib/include/neither/either.hpp index af91e5d8..a6d28f64 100644 --- a/lib/include/neither/either.hpp +++ b/lib/include/neither/either.hpp @@ -150,12 +150,30 @@ struct Either { return isLeft ? std::move(leftValue) : std::move(rightValue); } + template< + class L2 = L, + class R2 = R, + typename std::enable_if::value && std::is_copy_constructible::value), int>::type = 0 + > + auto join()& + -> std::common_type_t { + return isLeft ? std::move(leftValue) : std::move(rightValue); + } + template constexpr auto join(LeftF const& leftCase, RightF const& rightCase) const -> decltype( isLeft? leftCase( leftValue ) : rightCase( rightValue ) ) { return isLeft ? leftCase( leftValue ) : rightCase( rightValue ); } + template::value && std::is_copy_constructible::value), int>::type = 0 + > + constexpr auto join(LeftF const& leftCase, RightF const& rightCase)& + -> decltype( isLeft? leftCase(std::move(leftValue)) : rightCase(std::move(rightValue)) ){ + return isLeft ? leftCase(std::move(leftValue)) : rightCase(std::move(rightValue)); + } + template constexpr auto leftMap(F const& leftCase) const& -> Either { @@ -173,6 +191,16 @@ struct Either { NextEither::rightOf( std::move(rightValue) ); } + + template::value && std::is_copy_constructible::value), int>::type = 0> + auto leftMap(F const& leftCase)& -> Either { + using NextEither = Either; + return isLeft ? + NextEither::leftOf(leftCase(std::move(leftValue))) : + NextEither::rightOf( std::move(rightValue) ); + } + template constexpr auto rightMap(F const& rightCase) const& -> Either { using NextEither = Either; @@ -189,6 +217,15 @@ struct Either { NextEither::rightOf( rightCase( std::move(rightValue) ) ); } + template::value && std::is_copy_constructible::value), int>::type = 0> + auto rightMap(F const& rightCase)& -> Either { + using NextEither = Either; + return isLeft ? + NextEither::leftOf( std::move(leftValue) ) : + NextEither::rightOf( rightCase( std::move(rightValue) ) ); + } + template constexpr auto leftFlatMap(LeftCase const& leftCase) const& -> decltype( ensureEitherRight(leftCase(isCopyable((L2)leftValue)), isCopyable((R2)rightValue))) { @@ -225,6 +262,19 @@ struct Either { return NextEither::rightOf(std::move(rightValue)); } + template::value && std::is_copy_constructible::value), int>::type = 0> + auto leftFlatMap(LeftCase const& leftCase)& + -> decltype( ensureEitherRight(leftCase(std::move(leftValue)), std::move(rightValue))) { + using NextEither = decltype(leftCase(std::move(leftValue))); + + if (!*this) { + return leftCase( std::move(leftValue) ); + } + + return NextEither::rightOf(std::move(rightValue)); + } + template auto rightFlatMap(RightCase const& rightCase)&& -> decltype( ensureEitherLeft(rightCase(std::move(rightValue)), std::move(leftValue))) { @@ -237,6 +287,19 @@ struct Either { return NextEither::leftOf(std::move(leftValue)); } + template::value && std::is_copy_constructible::value), int>::type = 0> + auto rightFlatMap(RightCase const& rightCase)& + -> decltype( ensureEitherLeft(rightCase(std::move(rightValue)), std::move(leftValue))) { + using NextEither = decltype(rightCase(std::move(rightValue))); + + if (*this) { + return rightCase(std::move(rightValue)); + } + + return NextEither::leftOf(std::move(leftValue)); + } + constexpr operator bool()const { return !isLeft; } };