Add Result.reduce(Result,BinaryOperator)

This commit is contained in:
Paul Campbell 2018-07-20 18:40:33 +01:00
parent a1a7ad65c2
commit b80650b6ae
4 changed files with 123 additions and 39 deletions

View file

@ -26,6 +26,7 @@ import net.kemitix.mon.maybe.Maybe;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
import java.util.function.BinaryOperator;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.Predicate; import java.util.function.Predicate;
@ -101,6 +102,11 @@ class Err<T> implements Result<T> {
return this; return this;
} }
@Override
public Result<T> reduce(final Result<T> identify, final BinaryOperator<T> operator) {
return Result.error(error);
}
@Override @Override
public boolean equals(final Object other) { public boolean equals(final Object other) {
return other instanceof Err && Objects.equals(error, ((Err) other).error); return other instanceof Err && Objects.equals(error, ((Err) other).error);

View file

@ -25,10 +25,7 @@ import net.kemitix.mon.Functor;
import net.kemitix.mon.maybe.Maybe; import net.kemitix.mon.maybe.Maybe;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
import java.util.function.Consumer; import java.util.function.*;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
/** /**
* An Either type for holding a result or an error (Throwable). * An Either type for holding a result or an error (Throwable).
@ -63,17 +60,6 @@ public interface Result<T> extends Functor<T, Result<?>> {
return new Err<>(error); return new Err<>(error);
} }
/**
* Create a Result for a success.
*
* @param value the value
* @param <T> the type of the value
* @return a successful Result
*/
static <T> Result<T> ok(final T value) {
return new Success<>(value);
}
/** /**
* Create a Result for a output of the Callable. * Create a Result for a output of the Callable.
* *
@ -90,6 +76,17 @@ public interface Result<T> extends Functor<T, Result<?>> {
} }
} }
/**
* Create a Result for a success.
*
* @param value the value
* @param <T> the type of the value
* @return a successful Result
*/
static <T> Result<T> ok(final T value) {
return new Success<>(value);
}
/** /**
* Creates a {@link Maybe} from the Result, where the Result is a success, then the Maybe will contain the value. * Creates a {@link Maybe} from the Result, where the Result is a success, then the Maybe will contain the value.
* *
@ -108,6 +105,15 @@ public interface Result<T> extends Functor<T, Result<?>> {
} }
} }
/**
* Extracts the successful value from the result, or throws the error Throwable.
*
* @return the value if a success
* @throws Throwable the result is an error
*/
@SuppressWarnings("illegalthrows")
T orElseThrow() throws Throwable;
/** /**
* Swaps the inner Result of a Maybe, so that a Result is on the outside. * Swaps the inner Result of a Maybe, so that a Result is on the outside.
* *
@ -122,6 +128,15 @@ public interface Result<T> extends Functor<T, Result<?>> {
.flatMap(value -> Result.ok(Maybe.maybe(value))); .flatMap(value -> Result.ok(Maybe.maybe(value)));
} }
/**
* Returns a new Result consisting of the result of applying the function to the contents of the Result.
*
* @param f the mapping function the produces a Result
* @param <R> the type of the value withing the Result of the mapping function
* @return a Result
*/
<R> Result<R> flatMap(Function<T, Result<R>> f);
/** /**
* Applies the function to the contents of a Maybe within the Result. * Applies the function to the contents of a Maybe within the Result.
* *
@ -131,7 +146,10 @@ public interface Result<T> extends Functor<T, Result<?>> {
* @param <R> the type of the updated Result * @param <R> the type of the updated Result
* @return a new Maybe within a Result * @return a new Maybe within a Result
*/ */
static <T, R> Result<Maybe<R>> flatMapMaybe(Result<Maybe<T>> maybeResult, Function<Maybe<T>, Result<Maybe<R>>> f) { static <T, R> Result<Maybe<R>> flatMapMaybe(
final Result<Maybe<T>> maybeResult,
final Function<Maybe<T>, Result<Maybe<R>>> f
) {
return maybeResult.flatMap(f); return maybeResult.flatMap(f);
} }
@ -149,15 +167,6 @@ public interface Result<T> extends Functor<T, Result<?>> {
*/ */
boolean isOkay(); boolean isOkay();
/**
* Returns a new Result consisting of the result of applying the function to the contents of the Result.
*
* @param f the mapping function the produces a Result
* @param <R> the type of the value withing the Result of the mapping function
* @return a Result
*/
<R> Result<R> flatMap(Function<T, Result<R>> f);
@Override @Override
<R> Result<R> map(Function<T, R> f); <R> Result<R> map(Function<T, R> f);
@ -177,15 +186,6 @@ public interface Result<T> extends Functor<T, Result<?>> {
*/ */
Result<Maybe<T>> maybe(Predicate<T> predicate); Result<Maybe<T>> maybe(Predicate<T> predicate);
/**
* Extracts the successful value from the result, or throws the error Throwable.
*
* @return the value if a success
* @throws Throwable the result is an error
*/
@SuppressWarnings("illegalthrows")
T orElseThrow() throws Throwable;
/** /**
* Provide the value within the Result, if it is a success, to the Consumer, and returns this Result. * Provide the value within the Result, if it is a success, to the Consumer, and returns this Result.
* *
@ -251,4 +251,16 @@ public interface Result<T> extends Functor<T, Result<?>> {
* @return the Result or a new error Result * @return the Result or a new error Result
*/ */
Result<T> thenWith(Function<T, WithResultContinuation<T>> f); Result<T> thenWith(Function<T, WithResultContinuation<T>> f);
/**
* Reduce two Results of the same type into one using the reducing function provided.
*
* <p>If either Result is an error, then the reduce will return the error. If both are errors, then the error of
* {@link this} Result will be returned.</p>
*
* @param identify the identify Result
* @param operator the function to combine the values the Results
* @return a Result containing the combination of the two Results
*/
Result<T> reduce(Result<T> identify, BinaryOperator<T> operator);
} }

View file

@ -26,6 +26,7 @@ import net.kemitix.mon.maybe.Maybe;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
import java.util.function.BinaryOperator;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.Predicate; import java.util.function.Predicate;
@ -51,11 +52,6 @@ class Success<T> implements Result<T> {
return true; return true;
} }
@Override
public <R> Result<R> flatMap(final Function<T, Result<R>> f) {
return f.apply(value);
}
@Override @Override
public <R> Result<R> map(final Function<T, R> f) { public <R> Result<R> map(final Function<T, R> f) {
return Result.ok(f.apply(value)); return Result.ok(f.apply(value));
@ -105,6 +101,16 @@ class Success<T> implements Result<T> {
return f.apply(value).call(this); return f.apply(value).call(this);
} }
@Override
public Result<T> reduce(final Result<T> identity, final BinaryOperator<T> operator) {
return flatMap(a -> identity.flatMap(b -> Result.of(() -> operator.apply(a, b))));
}
@Override
public <R> Result<R> flatMap(final Function<T, Result<R>> f) {
return f.apply(value);
}
@Override @Override
public boolean equals(final Object other) { public boolean equals(final Object other) {
return other instanceof Success && Objects.equals(value, ((Success) other).value); return other instanceof Success && Objects.equals(value, ((Success) other).value);

View file

@ -671,6 +671,66 @@ public class ResultTest implements WithAssertions {
); );
} }
@Test
public void okayOkay_whenReduce_thenCombine() {
//given
final Result<Integer> result1 = Result.ok(1);
final Result<Integer> result10 = Result.ok(10);
//when
final Result<Integer> result11 = result1.reduce(result10, (a, b) -> a + b);
//then
result11.match(
success -> assertThat(success).isEqualTo(11),
error -> fail("Not an error")
);
}
@Test
public void okayError_whenReduce_thenError() {
//given
final Result<Integer> result1 = Result.ok(1);
final RuntimeException exception = new RuntimeException();
final Result<Integer> result10 = Result.error(exception);
//when
final Result<Integer> result11 = result1.reduce(result10, (a, b) -> a + b);
//then
result11.match(
success->fail("Not a success"),
error -> assertThat(error).isSameAs(exception)
);
}
@Test
public void errorOkay_whenReduce_thenError() {
//given
final RuntimeException exception = new RuntimeException();
final Result<Integer> result1 = Result.error(exception);
final Result<Integer> result10 = Result.ok(10);
//when
final Result<Integer> result11 = result1.reduce(result10, (a, b) -> a + b);
//then
result11.match(
success->fail("Not a success"),
error -> assertThat(error).isSameAs(exception)
);
}
@Test
public void errorError_whenReduce_thenError() {
//given
final RuntimeException exception1 = new RuntimeException();
final Result<Integer> result1 = Result.error(exception1);
final RuntimeException exception10 = new RuntimeException();
final Result<Integer> result10 = Result.error(exception10);
//when
final Result<Integer> result11 = result1.reduce(result10, (a, b) -> a + b);
//then
result11.match(
success->fail("Not a success"),
error -> assertThat(error).isSameAs(exception1)
);
}
@RequiredArgsConstructor @RequiredArgsConstructor
private static class UseCase { private static class UseCase {