Для решения этой проблемы я использовал инструменты из другого ответа:
- Currying the functions
- Частичное приложение
- Generics
- Концепции аппликативных функторов
- Концепции функторов
Сначала я создаю интерфейс с необходимыми аргументами:
import io.reactivex.annotations.NonNull;
public interface Function11<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, R> {
@NonNull
R apply(@NonNull T1 var1, @NonNull T2 var2, @NonNull T3 var3, @NonNull T4 var4, @NonNull T5 var5, @NonNull T6 var6, @NonNull T7 var7, @NonNull T8 var8, @NonNull T9 var9, @NonNull T10 var10, @NonNull T11 var11) throws Exception;
}
Затем я создаю класс расширения:
public class SingleExtension {
public static <A, B> Single<B> zipOver(Single<Function<A, B>> applicativeFunctor, Single<A> applicativeValue) {
return Single.zip(
applicativeFunctor,
applicativeValue,
(Function<A, B> f, A a) -> f.apply(a));
}
public static <A, B> Single<B> zipOverOptional(Single<Function<Optional<A>, B>> applicativeFunctor, Optional<Single<A>> applicativeValue) {
if (applicativeValue.isPresent()) {
return Single.zip(
applicativeFunctor,
applicativeValue.get(),
(Function<Optional<A>, B> f, A a) -> f.apply(Optional.of(a))
);
} else {
return applicativeFunctor.map((f) -> f.apply(Optional.empty()));
}
}
}
Наконец, я использую его.
public static void main(String[] args) {
Function11<String, Integer, Optional<Integer>, String, String, String, String, String, String, String, String, String> function11 = (String s1, Integer i2, Optional<Integer> i3, String s4, String s5, String s6, String s7, String s8, String s9, String s10, String s11) -> "".join("-", s1, "i" + i2.toString(), "i" + i3.orElse(5).toString(), s4, s5, s6, s7, s8, s9, s10, s11);
Function<String, Function<Integer, Function<Optional<Integer>, Function<String, Function<String, Function<String, Function<String, Function<String, Function<String, Function<String, Function<String, String>>>>>>>>>>> curryF = Curry.curry(function11);
Single<Function<String, Function<Integer, Function<Optional<Integer>, Function<String, Function<String, Function<String, Function<String, Function<String, Function<String, Function<String, Function<String, String>>>>>>>>>>>> applicativeCurryF = Single.just(curryF);
Single<String> singleString1 = Single.just("s1");
Single<Integer> singleOne = Single.just(1);
Optional<Single<Integer>> singleOptionalTwo = Optional.of(Single.just(2));
Single<String> singleString2 = Single.just("s2");
Single<String> singleString3 = Single.just("s3");
Single<String> singleString4 = Single.just("s4");
Single<String> singleString5 = Single.just("s5");
Single<String> singleString6 = Single.just("s6");
Single<String> singleString7 = Single.just("s7");
Single<String> singleString8 = Single.just("s8");
Single<String> singleString9 = Single.just("s9");
Single<String> singleString10 = Single.just("s10");
Single<Function<Integer, Function<Optional<Integer>, Function<String, Function<String, Function<String, Function<String, Function<String, Function<String, Function<String, Function<String, String>>>>>>>>>>> applied1 = (SingleExtension.zipOver(applicativeCurryF, singleString1));
Single<Function<Optional<Integer>, Function<String, Function<String, Function<String, Function<String, Function<String, Function<String, Function<String, Function<String, String>>>>>>>>>> applied2 = (SingleExtension.zipOver(applied1, singleOne));
Single<Function<String, Function<String, Function<String, Function<String, Function<String, Function<String, Function<String, Function<String, String>>>>>>>>> applied3 = (SingleExtension.zipOverOptional(applied2, singleOptionalTwo));
Single<Function<String, Function<String, Function<String, Function<String, Function<String, Function<String, Function<String, String>>>>>>>> applied4 = (SingleExtension.zipOver(applied3, singleString2));
Single<Function<String, Function<String, Function<String, Function<String, Function<String, Function<String, String>>>>>>> applied5 = (SingleExtension.zipOver(applied4, singleString3));
Single<Function<String, Function<String, Function<String, Function<String, Function<String, String>>>>>> applied6 = (SingleExtension.zipOver(applied5, singleString4));
Single<Function<String, Function<String, Function<String, Function<String, String>>>>> applied7 = (SingleExtension.zipOver(applied6, singleString5));
Single<Function<String, Function<String, Function<String, String>>>> applied8 = (SingleExtension.zipOver(applied7, singleString6));
Single<Function<String, Function<String, String>>> applied9 = (SingleExtension.zipOver(applied8, singleString7));
Single<Function<String, String>> applied10 = (SingleExtension.zipOver(applied9, singleString8));
Single<String> result = (SingleExtension.zipOver(applied10, singleString9));
result
.test()
.assertValue("s1-i1-i2-s2-s3-s4-s5-s6-s7-s8-s9");
System.out.println("All ok");
}
Если бы библиотека включала эти методы, это было бы так просто чтобы связать их как:
String result = applicativeCurryF
.zipOver(singleString1)
.zipOverOptional(singleOptionalTwo)
.zipOver(singleString2)
.zipOver(singleString3)
.zipOver(singleString4)
.zipOver(singleString5)
.zipOver(singleString6)
.zipOver(singleString7)
.zipOver(singleString8)
.zipOver(singleString9)