public final class Steps extends Object
Option, Either, Try and Java 8
Optional.
The intention of this comprehension is to provide access to flatMap, map and filter of these monad types in a way that is easier to read and reason about.
A short explanation is:
Option then each step must return an Option
with the final yield producing an Option result.
Simple example usage of Steps to produce an Option by chaining
functions that all have access to the previous values.
Option<Integer> result = Steps.begin(some(2))
.then(number -> some(number + 3))
.then((number1, number2) -> some(number1 * number2))
.yield((number1, number2, number3) -> number3 * 4);
This yield will produce a Option.some(A) containing
the final value of 40.
This is calculated by:
some of 40An example of a Steps causing it to break out early, and do no further evaluation is:
Option<Integer> result = Steps.begin(some(2))
.then(number -> none(Integer.class))
.then((number1, number2) -> some(number1 * number2))
.yield((number1, number2, number3) -> number3 * 4);
In this example, the result will be none and the functions after
the empty state is returned will not be evaluated.
While each step provides access to the previous values, if these are not required, then it is possible to evaluate a new value using a Supplier at each step. For example, the following is possible:
Option<Integer> result = Steps.begin(some(2))
.then(() -> some(5))
.then((number1, number2) -> some(number1 * number2))
.yield((number1, number2, number3) -> number3 * 4);
In this case we have decided that the first step will evaluate its result
without requiring the previous state, and just return 5. This final result
will still be a some of 40.
Filter is another method to break out of further evaulations and return the empty result. The following example, illustrates this:
Option<Integer> result = Steps.begin(some(2))
.then(number -> some(number + 3))
.filter((number1, number2) -> number2 == 0)
.then((number1, number2) -> some(number1 * number2))
.yield((number1, number2, number3) -> number3 * 4);
In this contrived example, the filter predicate will return false as number2 will always be 5, and never 0. This will result in that steps result being changed to the empty value, and prevent any further step evaluation.
NB: It is possible to chain together up to 6 steps
| Modifier and Type | Method and Description |
|---|---|
static <A,LEFT> EitherStep1<A,LEFT> |
begin(Either<LEFT,A> either)
Begin a new Steps expresison, using the
Either type. |
static <A> OptionStep1<A> |
begin(Option<A> option)
Begin a new Steps expresison, using the
Option type. |
static <A> OptionalStep1<A> |
begin(Optional<A> optional)
Begin a new Steps expresison, using the
Optional type. |
static <A> TryStep1<A> |
begin(Try<A> aTry)
Begin a new Steps expresison, using the
Try type. |
public static <A,LEFT> EitherStep1<A,LEFT> begin(Either<LEFT,A> either)
Either type.
This assumes a Right-Biased Either, and will therefore continue evaluating
steps on right results, and break out early on the first
left result.
A - The right hand side type for the initial EitherLEFT - The left hand side type for the initial Eithereither - The start value for this steps.Either,
for description of Steps usage with examplespublic static <A> OptionStep1<A> begin(Option<A> option)
Option type.
This will continue evaluating steps on some results, and break
out early on the first none result.
A - The type for the initial Optionoption - The start value for this steps.Option,
for description of Steps usage with examplespublic static <A> OptionalStep1<A> begin(Optional<A> optional)
Optional type.
This will continue evaluating steps on of results, and break
out early on the first empty result.
A - The type for the initial Optionaloptional - The start value for this steps.Optional,
for description of Steps usage with examplespublic static <A> TryStep1<A> begin(Try<A> aTry)
Try type.
This will continue evaluating steps on success results, and
break out early on the first failure result.
A - The type for the initial TryaTry - The start value for this steps.Try,
for description of Steps usage with examplesCopyright © 2018 Atlassian. All rights reserved.