예제 #1
0
 /**
  * Sequences through the right side of the either monad with a list of values.
  *
  * @param a The list of values to sequence with the either monad.
  * @return A sequenced value.
  */
 public static <B, X> Either<X, List<B>> sequenceRight(final List<Either<X, B>> a) {
   return a.isEmpty()
       ? Either.<X, List<B>>right(List.<B>nil())
       : a.head()
           .right()
           .bind(
               new Func<B, Either<X, List<B>>>() {
                 public Either<X, List<B>> f(final B bb) {
                   return sequenceRight(a.tail()).right().map(cons_(bb));
                 }
               });
 }
예제 #2
0
 /**
  * Sequences through the left side of the either monad with a list of values.
  *
  * @param a The list of values to sequence with the either monad.
  * @return A sequenced value.
  */
 public static <A, X> Either<List<A>, X> sequenceLeft(final List<Either<A, X>> a) {
   return a.isEmpty()
       ? Either.<List<A>, X>left(List.<A>nil())
       : a.head()
           .left()
           .bind(
               new Func<A, Either<List<A>, X>>() {
                 public Either<List<A>, X> f(final A aa) {
                   return sequenceLeft(a.tail()).left().map(cons_(aa));
                 }
               });
 }
예제 #3
0
 /**
  * Takes an <code>Either</code> to its contained value within left or right.
  *
  * @param e The either to reduce.
  * @return An <code>Either</code> to its contained value within left or right.
  */
 public static <A> A reduce(final Either<A, A> e) {
   return e.isLeft() ? e.left().value() : e.right().value();
 }
예제 #4
0
 /**
  * Binds the given function across this projection's value if it has one.
  *
  * @param f The function to bind across this projection.
  * @return A new either value after binding.
  */
 public <X> Either<A, X> bind(final Func<B, Either<A, X>> f) {
   return isRight() ? f.f(value()) : new Left<A, X>(e.left().value());
 }
예제 #5
0
 /**
  * The value of this projection or the result of the given function on the opposing projection's
  * value.
  *
  * @param f The function to execute if this projection has no value.
  * @return The value of this projection or the result of the given function on the opposing
  *     projection's value.
  */
 public B on(final Func<A, B> f) {
   return isRight() ? value() : f.f(e.left().value());
 }
예제 #6
0
 /**
  * Map the given function across this projection's value if it has one.
  *
  * @param f The function to map across this projection.
  * @return A new either value after mapping.
  */
 public <X> Either<A, X> map(final Func<B, X> f) {
   return isRight() ? new Right<A, X>(f.f(value())) : new Left<A, X>(e.left().value());
 }
예제 #7
0
 /**
  * Returns the value of this projection or fails with the given error message.
  *
  * @param err The error message to fail with.
  * @return The value of this projection
  */
 public B valueE(final P1<String> err) {
   if (e.isRight())
     //noinspection CastToConcreteClass
     return ((Right<A, B>) e).b;
   else throw error(err._1());
 }
예제 #8
0
 /**
  * Binds the given function across this projection's value if it has one.
  *
  * @param f The function to bind across this projection.
  * @return A new either value after binding.
  */
 public <X> Either<X, B> bind(final Func<A, Either<X, B>> f) {
   return isLeft() ? f.f(value()) : new Right<X, B>(e.right().value());
 }
예제 #9
0
 /**
  * Map the given function across this projection's value if it has one.
  *
  * @param f The function to map across this projection.
  * @return A new either value after mapping.
  */
 public <X> Either<X, B> map(final Func<A, X> f) {
   return isLeft() ? new Left<X, B>(f.f(value())) : new Right<X, B>(e.right().value());
 }
예제 #10
0
 /**
  * The value of this projection or the result of the given function on the opposing projection's
  * value.
  *
  * @param f The function to execute if this projection has no value.
  * @return The value of this projection or the result of the given function on the opposing
  *     projection's value.
  */
 public A on(final Func<B, A> f) {
   return isLeft() ? value() : f.f(e.right().value());
 }
예제 #11
0
 /**
  * Returns the value of this projection or fails with the given error message.
  *
  * @param err The error message to fail with.
  * @return The value of this projection
  */
 public A valueE(final P1<String> err) {
   if (e.isLeft())
     //noinspection CastToConcreteClass
     return ((Left<A, B>) e).a;
   else throw error(err._1());
 }