32 #include <type_traits>
33 #include <boost/variant.hpp>
42 template<
typename L,
typename R>
45 using Either_t = boost::variant<L, R>;
48 enum { LeftVal, RightVal };
50 static_assert (!std::is_same<L, R>::value,
"Types cannot be the same.");
67 template<
typename LPrime,
typename RPrime,
68 typename = std::enable_if_t<std::is_convertible<LPrime, L>::value &&
69 std::is_convertible<RPrime, R>::value>>
82 return This_.which () == LeftVal;
87 return This_.which () == RightVal;
93 throw std::runtime_error {
"Tried accessing Left for a Right Either" };
94 return boost::get<L> (This_);
100 throw std::runtime_error {
"Tried accessing Right for a Left Either" };
101 return boost::get<R> (This_);
131 template<
typename RNew>
149 template<
typename RNew>
157 return [] (
const auto& other)
159 static_assert (std::is_convertible<decltype (other.GetLeft ()), L>::value,
160 "Other's Either's Left type is not convertible to this Left type.");
161 return other.IsLeft () ?
169 return e1.This_ == e2.This_;
178 template<
typename L,
typename R,
typename F,
typename = std::result_of_t<F ()>>
186 template<
typename L,
typename R>
194 template<
template<
typename>
class Cont,
typename L,
typename R>
197 std::pair<Cont<L>, Cont<R>> result;
198 for (
const auto& either : eithers)
199 if (either.IsLeft ())
200 result.first.push_back (either.GetLeft ());
202 result.second.push_back (either.GetRight ());
207 template<
typename L,
typename R>
223 template<
typename L,
typename R>
237 template<
typename RP>
243 template<
typename AV>
249 return R_t::Left (f.
GetLeft ());
252 return R_t::Left (v.GetLeft ());
254 return R_t::Right (f.
GetRight () (v.GetRight ()));
258 template<
typename L,
typename R>
270 return R_t::Left (value.
GetLeft ());
static Either Right(const R &r)
friend bool operator!=(const Either &e1, const Either &e2)
friend bool operator==(const Either &e1, const Either &e2)
static BindResult_t< F > Bind(const Either< L, R > &value, const F &f)
static Either< L, RP > Pure(const RP &v)
static Either Left(const L &l)
static std::enable_if_t<!std::is_convertible< RNew, R >::value, Either< L, RNew > > Right(const RNew &r)
const R & GetRight() const
R RightOr(const Either< L, R > &either, F &&f)
const L & GetLeft() const
boost::optional< L > MaybeLeft() const
static FmapResult_t< F > Apply(const Either< L, R > &either, const F &f)
std::result_of_t< F(R)> BindResult_t
boost::optional< R > MaybeRight() const
Either & operator=(const Either &)=default
boost::variant< L, R > AsVariant() const
std::pair< Cont< L >, Cont< R > > PartitionEithers(const Cont< Either< L, R >> &eithers)
static GSLResult_t< Type_t, AV > GSL(const Type_t &f, const AV &v)
typename InstanceApplicative< AF >::template GSLResult< AV >::Type_t GSLResult_t
static auto EmbeddingLeft()
static Either< L, RNew > FromMaybe(const boost::optional< RNew > &maybeRight, const L &left)
The Functor class is used for types that can be mapped over.
Either(const Either< LPrime, RPrime > &other)