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.");
57 explicit Either (
const L& l)
62 explicit Either (
const R& r)
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>>
71 : This_ { other.AsVariant () }
75 Either (
const Either&) =
default;
76 Either (Either&&) =
default;
77 Either& operator= (
const Either&) =
default;
78 Either& operator= (Either&&) =
default;
82 return This_.which () == LeftVal;
87 return This_.which () == RightVal;
90 const L& GetLeft ()
const
93 throw std::runtime_error {
"Tried accessing Left for a Right Either" };
94 return boost::get<L> (This_);
97 const R& GetRight ()
const
100 throw std::runtime_error {
"Tried accessing Right for a Left Either" };
101 return boost::get<R> (This_);
104 boost::optional<L> MaybeLeft ()
const
111 boost::optional<R> MaybeRight ()
const
118 boost::variant<L, R> AsVariant ()
const
124 R ToRight (F&& f)
const
131 template<
typename RNew>
132 static Either<L, RNew> FromMaybe (
const boost::optional<RNew>& maybeRight,
const L& left)
139 static Either Left (
const L& l)
144 static Either Right (
const R& r)
149 template<
typename RNew>
150 static std::enable_if_t<!std::is_convertible<RNew, R>::value,
Either<L, RNew>> Right (
const RNew& r)
155 static auto EmbeddingLeft ()
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 () ?
167 friend bool operator== (
const Either& e1,
const Either& e2)
169 return e1.This_ == e2.This_;
172 friend bool operator!= (
const Either& e1,
const Either& e2)
178 template<
typename L,
typename R,
typename F,
typename = std::result_of_t<F ()>>
181 return either.IsRight () ?
186 template<
typename L,
typename R>
189 return either.IsRight () ?
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>
216 if (either.IsLeft ())
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 ());
272 return f (value.GetRight ());
bool operator!=(const ModelIterator &left, const ModelIterator &right)
static BindResult_t< F > Bind(const Either< L, R > &value, const F &f)
static Either< L, RP > Pure(const RP &v)
bool operator==(const ModelIterator &left, const ModelIterator &right)
R RightOr(const Either< L, R > &either, F &&f)
static FmapResult_t< F > Apply(const Either< L, R > &either, const F &f)
std::result_of_t< F(R)> BindResult_t
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
The Functor class is used for types that can be mapped over.