Skip to content

Commit

Permalink
Refresh neither to its latest commit
Browse files Browse the repository at this point in the history
  • Loading branch information
WojciechMigda committed Sep 16, 2023
1 parent fb6ff8f commit cd722eb
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 6 deletions.
7 changes: 1 addition & 6 deletions lib/include/neither/COMMIT
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
https://github.com/LoopPerfect/neither

91d4878b411e5903253f2a9bf37b0edee95a77f0


Changes:
- silenced warning in maybe.hpp
- rightMap && fix
d2db83d37e4ac84e50c51928bac4504b982983f0
63 changes: 63 additions & 0 deletions lib/include/neither/either.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<!(std::is_copy_constructible<L2>::value && std::is_copy_constructible<R2>::value), int>::type = 0
>
auto join()&
-> std::common_type_t<L2, R2> {
return isLeft ? std::move(leftValue) : std::move(rightValue);
}

template<class LeftF, class RightF>
constexpr auto join(LeftF const& leftCase, RightF const& rightCase) const
-> decltype( isLeft? leftCase( leftValue ) : rightCase( rightValue ) ) {
return isLeft ? leftCase( leftValue ) : rightCase( rightValue );
}

template<class LeftF, class RightF,
typename std::enable_if<!(sizeof(LeftF), std::is_copy_constructible<L>::value && std::is_copy_constructible<R>::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<class F, class L2=L, class R2=R>
constexpr auto leftMap(F const& leftCase) const&
-> Either<decltype(leftCase( isCopyable((L2)leftValue, (R2)rightValue) )), R2> {
Expand All @@ -173,6 +191,16 @@ struct Either {
NextEither::rightOf( std::move(rightValue) );
}


template<class F, class L2=L, class R2=R,
typename std::enable_if<!(std::is_copy_constructible<L2>::value && std::is_copy_constructible<R2>::value), int>::type = 0>
auto leftMap(F const& leftCase)& -> Either<decltype(leftCase(std::move(leftValue))), R2> {
using NextEither = Either<decltype(leftCase(std::move(leftValue))), R2>;
return isLeft ?
NextEither::leftOf(leftCase(std::move(leftValue))) :
NextEither::rightOf( std::move(rightValue) );
}

template<class F, class L2=L, class R2=R>
constexpr auto rightMap(F const& rightCase) const& -> Either<L, decltype(rightCase(isCopyable((R2)rightValue, (L2)leftValue)))> {
using NextEither = Either<L, decltype(rightCase(rightValue))>;
Expand All @@ -189,6 +217,15 @@ struct Either {
NextEither::rightOf( rightCase( std::move(rightValue) ) );
}

template<class F, class L2=L, class R2=R,
typename std::enable_if<!(std::is_copy_constructible<L2>::value && std::is_copy_constructible<R2>::value), int>::type = 0>
auto rightMap(F const& rightCase)& -> Either<L2, decltype(rightCase(std::move(rightValue)))> {
using NextEither = Either<L, decltype(rightCase(std::move(rightValue)))>;
return isLeft ?
NextEither::leftOf( std::move(leftValue) ) :
NextEither::rightOf( rightCase( std::move(rightValue) ) );
}

template<class LeftCase, class L2=L, class R2=R>
constexpr auto leftFlatMap(LeftCase const& leftCase) const&
-> decltype( ensureEitherRight(leftCase(isCopyable((L2)leftValue)), isCopyable((R2)rightValue))) {
Expand Down Expand Up @@ -225,6 +262,19 @@ struct Either {
return NextEither::rightOf(std::move(rightValue));
}

template<class LeftCase, class L2 = L, class R2 = R,
typename std::enable_if<!(std::is_copy_constructible<L2>::value && std::is_copy_constructible<R2>::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<class RightCase, class L2=L, class R2=R>
auto rightFlatMap(RightCase const& rightCase)&&
-> decltype( ensureEitherLeft(rightCase(std::move(rightValue)), std::move(leftValue))) {
Expand All @@ -237,6 +287,19 @@ struct Either {
return NextEither::leftOf(std::move(leftValue));
}

template<class RightCase, class L2=L, class R2=R,
typename std::enable_if<!(std::is_copy_constructible<L2>::value && std::is_copy_constructible<R2>::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; }
};

Expand Down

0 comments on commit cd722eb

Please sign in to comment.