1
1
package fj .data ;
2
2
3
- import fj .Equal ;
4
- import fj .F ;
5
- import fj .F0 ;
6
- import fj .Function ;
7
- import fj .Hash ;
8
- import fj .P1 ;
9
- import fj .Show ;
10
- import fj .Unit ;
3
+ import fj .*;
11
4
import fj .function .Effect1 ;
12
5
13
- import java .util .Collection ;
14
- import java .util .Iterator ;
6
+ import java .util .*;
15
7
16
8
import static fj .Bottom .error ;
17
- import static fj .Function .compose ;
18
- import static fj .Function .identity ;
9
+ import static fj .Function .*;
19
10
import static fj .P .p ;
20
11
import static fj .Unit .unit ;
21
12
import static fj .data .Array .mkArray ;
22
- import static fj .data .List .cons_ ;
23
- import static fj .data .List .list ;
24
- import static fj .data .List .single ;
13
+ import static fj .data .List .*;
25
14
import static fj .data .Option .some ;
26
15
27
16
/**
@@ -321,6 +310,19 @@ public <C> IO<Either<C, B>> traverseIO(final F<A, IO<C>> f) {
321
310
IOFunctions .unit (Either .right (e .right ().value ()));
322
311
}
323
312
313
+ /**
314
+ * Traverse this left with the given function and collect the output as a p1.
315
+ *
316
+ * @param f the given function
317
+ * @param <C> the type of the p1 value
318
+ * @return the p1
319
+ */
320
+ public <C > P1 <Either <C , B >> traverseP1 (final F <A , P1 <C >> f ) {
321
+ return e .isLeft () ?
322
+ f .f (value ()).map (left_ ()) :
323
+ p (right (e .right ().value ()));
324
+ }
325
+
324
326
/**
325
327
* Returns <code>None</code> if this projection has no value or if the given predicate
326
328
* <code>p</code> does not hold for the value, otherwise, returns a right in <code>Some</code>.
@@ -464,6 +466,16 @@ public Either<A, B> either() {
464
466
return e ;
465
467
}
466
468
469
+ /**
470
+ * Returns the value of this projection or fails with the given error message.
471
+ *
472
+ * @param err The error message to fail with.
473
+ * @return The value of this projection
474
+ */
475
+ public B valueE (final String err ) {
476
+ return valueE (p (err ));
477
+ }
478
+
467
479
/**
468
480
* Returns the value of this projection or fails with the given error message.
469
481
*
@@ -487,6 +499,16 @@ public B value() {
487
499
return valueE (p ("right.value on Left" ));
488
500
}
489
501
502
+ /**
503
+ * The value of this projection or the given argument.
504
+ *
505
+ * @param a The value to return if this projection has no value.
506
+ * @return The value of this projection or the given argument.
507
+ */
508
+ public B orValue (final B a ) {
509
+ return e .isRight () ? value () : a ;
510
+ }
511
+
490
512
/**
491
513
* The value of this projection or the given argument.
492
514
*
@@ -586,12 +608,26 @@ public <C> IO<Either<A, C>> traverseIO(final F<B, IO<C>> f) {
586
608
IOFunctions .lazy (() -> left (e .left ().value ()));
587
609
}
588
610
611
+ /**
612
+ * Traverse this right with the given function and collect the output as a p1.
613
+ *
614
+ * @param f the given function
615
+ * @param <C> the type of the p1 value
616
+ * @return the p1
617
+ */
589
618
public <C > P1 <Either <A , C >> traverseP1 (final F <B , P1 <C >> f ) {
590
619
return e .isRight () ?
591
620
f .f (value ()).map (right_ ()) :
592
621
p (left (e .left ().value ()));
593
622
}
594
623
624
+ /**
625
+ * Traverse this right with the given function and collect the output as an option.
626
+ *
627
+ * @param f the given function
628
+ * @param <C> the type of the option value
629
+ * @return the option
630
+ */
595
631
public <C > Option <Either <A , C >> traverseOption (final F <B , Option <C >> f ) {
596
632
return e .isRight () ?
597
633
f .f (value ()).map (right_ ()) :
@@ -758,13 +794,45 @@ public static <A, B, X> F<Either<A,B>, X> either_(final F<A, X> left, final F<B,
758
794
}
759
795
760
796
/**
797
+ * Map the given function across this either's left projection.
798
+ *
799
+ * @param f the given function
800
+ * @param <X> the type of the function output
801
+ * @return the either
802
+ */
803
+ public final <X > Either <X , B > leftMap (final F <A , X > f ) {
804
+ return left ().map (f );
805
+ }
806
+
807
+ /**
808
+ * Return a function that maps a given function across this either's left projection.
809
+ *
810
+ * @param <A> the type of the right value
811
+ * @param <B> the type of the left value
812
+ * @param <X> the type of the function output
761
813
* @return A function that maps another function across an either's left projection.
762
814
*/
763
815
public static <A , B , X > F <F <A , X >, F <Either <A , B >, Either <X , B >>> leftMap_ () {
764
816
return axf -> e -> e .left ().map (axf );
765
817
}
766
818
767
819
/**
820
+ * Map the given function across this either's right.
821
+ *
822
+ * @param f the given function
823
+ * @param <X> the type of the function output
824
+ * @return the either
825
+ */
826
+ public final <X > Either <A , X > rightMap (final F <B , X > f ) {
827
+ return right ().map (f );
828
+ }
829
+
830
+ /**
831
+ * Return a function that maps a given function across this either's right projection.
832
+ *
833
+ * @param <A> the type of the right value
834
+ * @param <B> the type of the left value
835
+ * @param <X> the type of the function output
768
836
* @return A function that maps another function across an either's right projection.
769
837
*/
770
838
public static <A , B , X > F <F <B , X >, F <Either <A , B >, Either <A , X >>> rightMap_ () {
@@ -796,6 +864,7 @@ public static <A, B> Either<A, B> joinRight(final Either<A, Either<A, B>> e) {
796
864
*
797
865
* @param a The list of values to sequence with the either monad.
798
866
* @return A sequenced value.
867
+ * @see fj.data.List#sequenceEitherLeft
799
868
*/
800
869
public static <A , X > Either <List <A >, X > sequenceLeft (final List <Either <A , X >> a ) {
801
870
return a .isEmpty () ?
@@ -808,6 +877,8 @@ public static <A, X> Either<List<A>, X> sequenceLeft(final List<Either<A, X>> a)
808
877
*
809
878
* @param a The list of values to sequence with the either monad.
810
879
* @return A sequenced value.
880
+ * @see fj.data.List#sequenceEither
881
+ * @see fj.data.List#sequenceEitherRight
811
882
*/
812
883
public static <B , X > Either <X , List <B >> sequenceRight (final List <Either <X , B >> a ) {
813
884
return a .isEmpty () ?
0 commit comments