Skip to content

Commit d3231b0

Browse files
authored
Merge pull request functionaljava#286 from jbgi/first-class-cata
First class cata for either and option
2 parents 6798ac6 + 9f1cf57 commit d3231b0

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,17 @@ public static <A, B> Either<A, B> right(final B b) {
746746
return new Right<>(b);
747747
}
748748

749+
/**
750+
* First class catamorphism for either. Folds over this either breaking into left or right.
751+
*
752+
* @param left The function to call if this is left.
753+
* @param right The function to call if this is right.
754+
* @return The reducing function.
755+
*/
756+
public static <A, B, X> F<Either<A,B>, X> either_(final F<A, X> left, final F<B, X> right) {
757+
return e -> e.either(left, right);
758+
}
759+
749760
/**
750761
* @return A function that maps another function across an either's left projection.
751762
*/

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,18 @@ public static <T> F<T, Option<T>> fromNull() {
688688
return Option::fromNull;
689689
}
690690

691+
/**
692+
* First-class catamorphism for Option: return a function that will performs
693+
* a reduction on an optional value using the given arguments.
694+
*
695+
* @param none The value to return if this optional value has no value.
696+
* @param some The function to apply to the value of this optional value.
697+
* @return the reducing function.
698+
*/
699+
public static final <A, B> F<Option<A>, B> option_(final B none, final F<A, B> some) {
700+
return o -> o.option(none, some);
701+
}
702+
691703
/**
692704
* Joins the given optional value of optional value using a bind operation.
693705
*

0 commit comments

Comments
 (0)