Skip to content

Commit 64487ec

Browse files
Implemented 'compose' and 'andThen' for FragileFunction
1 parent cf4f4da commit 64487ec

2 files changed

Lines changed: 8 additions & 16 deletions

File tree

src/main/java/de/variantsync/evolution/util/functional/Functional.java

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -123,20 +123,4 @@ public static <Nullable, B, E extends Exception> Optional<B> mapFragile(final Nu
123123
public static <A, B, E extends Exception> Lazy<Optional<B>> mapFragileLazily(final A a, final FragileFunction<A, B, E> f, final Supplier<String> errorMessage) {
124124
return Lazy.of(() -> mapFragile(a, f, errorMessage));
125125
}
126-
127-
/**
128-
* Chains two fragile functions. The chained function first applies f1 to a given input, and then applies f2 to the
129-
* output of f1 (i.e., given an input x the chained function corresponds to f2(f1(x)))
130-
* @param f1 The inner function
131-
* @param f2 The outer function
132-
* @param <Input> The input type of the inner function
133-
* @param <Intermediate> The input type of the outer function and the return type of the inner function
134-
* @param <Output> The return type of the outer function
135-
* @return A new FragileFunction f2(f1(x))
136-
*/
137-
public static <Input, Intermediate, Output> FragileFunction<Input, Output, Exception> chainFragile(
138-
final FragileFunction<Input, Intermediate, Exception> f1,
139-
final FragileFunction<Intermediate, Output, Exception> f2) {
140-
return (Input input) -> f2.run(f1.run(input));
141-
}
142126
}

src/main/java/de/variantsync/evolution/util/functional/interfaces/FragileFunction.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,12 @@
33
@FunctionalInterface
44
public interface FragileFunction<A, B, E extends Exception> {
55
B run(A a) throws E;
6+
7+
default <Input, ExceptionBefore extends Exception> FragileFunction<Input, B, Exception> compose(final FragileFunction<Input, A, ExceptionBefore> before) {
8+
return (Input input) -> this.run(before.run(input));
9+
}
10+
11+
default <Output, ExceptionAfter extends Exception> FragileFunction<A, Output, Exception> andThen(final FragileFunction<B, Output, ExceptionAfter> after) {
12+
return (A input) -> after.run(this.run(input));
13+
}
614
}

0 commit comments

Comments
 (0)