Skip to content

Commit fc85ee1

Browse files
Fixes #403. Add fj.data.Option.sequence*, fj.data.Option.traverse.*; add sequenceEither, traverseEither as aliases for sequenceEitherRight, sequenceEitherLeft.
1 parent f9ec6ee commit fc85ee1

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

core/src/main/java/fj/data/Option.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,18 @@ public final <B> Option<B> sequence(final Option<B> o) {
392392
return bind(c);
393393
}
394394

395+
/**
396+
* Sequence the given option and collect the output on the right side of an either.
397+
*
398+
* @param option the given option
399+
* @param <B> the type of the right value
400+
* @param <L> the type of the left value
401+
* @return the either
402+
*/
403+
public static final <L, B> Either<L, Option<B>> sequenceEither(final Option<Either<L, B>> option) {
404+
return option.traverseEitherRight(identity());
405+
}
406+
395407
/**
396408
* Sequence the given option and collect the output on the left side of an either.
397409
*
@@ -529,6 +541,18 @@ public static final <E, B> Validation<E, Option<B>> sequenceValidation(final Opt
529541
return option.traverseValidation(identity());
530542
}
531543

544+
/**
545+
* Traverse this option with the given function and collect the output on the right side of an either.
546+
*
547+
* @param f the given function
548+
* @param <L> the type of the left value
549+
* @param <B> the type of the right value
550+
* @return the either
551+
*/
552+
public final <L, B> Either<L, Option<B>> traverseEither(final F<A, Either<L, B>> f) {
553+
return traverseEitherRight(f);
554+
}
555+
532556
/**
533557
* Traverse this option with the given function and collect the output on the left side of an either.
534558
*

core/src/test/java/fj/data/OptionTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,19 @@
1313
import static fj.data.List.*;
1414
import static fj.data.Option.iif;
1515
import static fj.data.Option.sequence;
16+
import static fj.data.Option.sequenceEither;
17+
import static fj.data.Option.sequenceEitherLeft;
18+
import static fj.data.Option.sequenceEitherRight;
19+
import static fj.data.Option.sequenceF;
20+
import static fj.data.Option.sequenceIO;
21+
import static fj.data.Option.sequenceList;
22+
import static fj.data.Option.sequenceOption;
23+
import static fj.data.Option.sequenceP1;
24+
import static fj.data.Option.sequenceSeq;
25+
import static fj.data.Option.sequenceSet;
26+
import static fj.data.Option.sequenceStream;
27+
import static fj.data.Option.sequenceTrampoline;
28+
import static fj.data.Option.sequenceValidation;
1629
import static fj.data.Option.*;
1730
import static fj.data.Validation.fail;
1831
import static fj.data.Validation.*;
@@ -226,6 +239,12 @@ public void testBindProduct8() {
226239
});
227240
}
228241

242+
@Test
243+
public void testSequenceEither() {
244+
assertEquals(right(none()), sequenceEither(none()));
245+
assertEquals(right(some("zero")), sequenceEither(some(right("zero"))));
246+
assertEquals(left("zero"), sequenceEither(some(left("zero"))));
247+
}
229248

230249
@Test
231250
public void testSequenceEitherLeft() {
@@ -311,6 +330,14 @@ public void testSequenceValidation() {
311330
assertEquals(Validation.success(some(0)), sequenceValidation(some(Validation.success(0))));
312331
}
313332

333+
@Test
334+
public void testTraverseEither() {
335+
assertEquals(right(none()), none().traverseEither(constant(right(0))));
336+
assertEquals(right(some(0)), some("zero").traverseEither(constant(right(0))));
337+
assertEquals(right(none()), none().traverseEither(constant(left(0))));
338+
assertEquals(left(0), some("zero").traverseEither(constant(left(0))));
339+
}
340+
314341
@Test
315342
public void testTraverseEitherLeft() {
316343
assertEquals(left(none()), none().traverseEitherLeft(constant(left(0))));

0 commit comments

Comments
 (0)