From 6724f552ed29ddc3ac59f4af002d81bc64005c4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jyri-Matti=20La=CC=88hteenma=CC=88ki?= Date: Thu, 4 Jan 2024 09:27:48 +0200 Subject: [PATCH] add some function helpers --- pom.xml | 2 +- .../fi/solita/utils/functional/Function.java | 180 ++++++++++++++++++ .../solita/utils/functional/Functional.java | 7 + 3 files changed, 188 insertions(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 28ad348..99e481e 100644 --- a/pom.xml +++ b/pom.xml @@ -2,7 +2,7 @@ 4.0.0 fi.solita.utils functional-utils - 0.12.43 + 0.12.44 diff --git a/src/main/java/fi/solita/utils/functional/Function.java b/src/main/java/fi/solita/utils/functional/Function.java index eaebf55..47360f4 100644 --- a/src/main/java/fi/solita/utils/functional/Function.java +++ b/src/main/java/fi/solita/utils/functional/Function.java @@ -493,6 +493,186 @@ public final E apply(A t1, B t2, C t3, D t4) { } }; } + + /** + * Partially apply {@code arg1} to {@code f} + */ + public static final Function1 ap(final Apply2 f, final T1 arg1) { + return new Function1() { + @Override + public final R apply(T2 arg2) { + return f.apply(arg1, arg2); + } + }; + } + + /** + * Partially apply {@code arg1} to {@code f} + */ + public static final Function2 ap(final Apply3 f, final T1 arg1) { + return new Function2() { + @Override + public final R apply(T2 arg2, T3 arg3) { + return f.apply(arg1, arg2, arg3); + } + }; + } + + /** + * Partially apply {@code arg1} and {@code arg2} to {@code f} + */ + public static final Function1 ap(final Apply3 f, final T1 arg1, final T2 arg2) { + return new Function1() { + @Override + public final R apply(T3 arg3) { + return f.apply(arg1, arg2, arg3); + } + }; + } + + /** + * Partially apply {@code arg1} to {@code f} + */ + public static final Function3 ap(final Apply4 f, final T1 arg1) { + return new Function3() { + @Override + public final R apply(T2 arg2, T3 arg3, T4 arg4) { + return f.apply(arg1, arg2, arg3, arg4); + } + }; + } + + /** + * Partially apply {@code arg1} and {@code arg2} to {@code f} + */ + public static final Function2 ap(final Apply4 f, final T1 arg1, final T2 arg2) { + return new Function2() { + @Override + public final R apply(T3 arg3, T4 arg4) { + return f.apply(arg1, arg2, arg3, arg4); + } + }; + } + + /** + * Partially apply {@code arg1}, {@code arg2} and {@code arg3} to {@code f} + */ + public static final Function1 ap(final Apply4 f, final T1 arg1, final T2 arg2, final T3 arg3) { + return new Function1() { + @Override + public final R apply(T4 arg4) { + return f.apply(arg1, arg2, arg3, arg4); + } + }; + } + + /** + * Partially apply {@code arg1} to {@code f} + */ + public static final Function4 ap(final Apply5 f, final T1 arg1) { + return new Function4() { + @Override + public final R apply(T2 arg2, T3 arg3, T4 arg4, T5 arg5) { + return f.apply(arg1, arg2, arg3, arg4, arg5); + } + }; + } + + /** + * Partially apply {@code arg1} and {@code arg2} to {@code f} + */ + public static final Function3 ap(final Apply5 f, final T1 arg1, final T2 arg2) { + return new Function3() { + @Override + public final R apply(T3 arg3, T4 arg4, T5 arg5) { + return f.apply(arg1, arg2, arg3, arg4, arg5); + } + }; + } + + /** + * Partially apply {@code arg1}, {@code arg2} and {@code arg3} to {@code f} + */ + public static final Function2 ap(final Apply5 f, final T1 arg1, final T2 arg2, final T3 arg3) { + return new Function2() { + @Override + public final R apply(T4 arg4, T5 arg5) { + return f.apply(arg1, arg2, arg3, arg4, arg5); + } + }; + } + + /** + * Partially apply {@code arg1}, {@code arg2}, {@code arg3} and {@code arg4} to {@code f} + */ + public static final Function1 ap(final Apply5 f, final T1 arg1, final T2 arg2, final T3 arg3, final T4 arg4) { + return new Function1() { + @Override + public final R apply(T5 arg5) { + return f.apply(arg1, arg2, arg3, arg4, arg5); + } + }; + } + + /** + * Partially apply {@code arg1} to {@code f} + */ + public static final Function5 ap(final Apply6 f, final T1 arg1) { + return new Function5() { + @Override + public final R apply(T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6) { + return f.apply(arg1, arg2, arg3, arg4, arg5, arg6); + } + }; + } + + /** + * Partially apply {@code arg1} and {@code arg2} to {@code f} + */ + public static final Function4 ap(final Apply6 f, final T1 arg1, final T2 arg2) { + return new Function4() { + @Override + public final R apply(T3 arg3, T4 arg4, T5 arg5, T6 arg6) { + return f.apply(arg1, arg2, arg3, arg4, arg5, arg6); + } + }; + } + + /** + * Partially apply {@code arg1}, {@code arg2} and {@code arg3} to {@code f} + */ + public static final Function3 ap(final Apply6 f, final T1 arg1, final T2 arg2, final T3 arg3) { + return new Function3() { + @Override + public final R apply(T4 arg4, T5 arg5, T6 arg6) { + return f.apply(arg1, arg2, arg3, arg4, arg5, arg6); + } + }; + } + + /** + * Partially apply {@code arg1}, {@code arg2}, {@code arg3} and {@code arg4} to {@code f} + */ + public static final Function2 ap(final Apply6 f, final T1 arg1, final T2 arg2, final T3 arg3, final T4 arg4) { + return new Function2() { + @Override + public final R apply(T5 arg5, T6 arg6) { + return f.apply(arg1, arg2, arg3, arg4, arg5, arg6); + } + }; + } + + /** + * Partially apply {@code arg1}, {@code arg2}, {@code arg3}, {@code arg4} and {@code arg5} to {@code f} + */ + public static final Function1 ap(final Apply6 f, final T1 arg1, final T2 arg2, final T3 arg3, final T4 arg4, final T5 arg5) { + return new Function1() { + @Override + public final R apply(T6 arg6) { + return f.apply(arg1, arg2, arg3, arg4, arg5, arg6); + } + }; + } } class PartialApplicationHelper { diff --git a/src/main/java/fi/solita/utils/functional/Functional.java b/src/main/java/fi/solita/utils/functional/Functional.java index 5b6f2d2..aec1cc0 100644 --- a/src/main/java/fi/solita/utils/functional/Functional.java +++ b/src/main/java/fi/solita/utils/functional/Functional.java @@ -60,6 +60,13 @@ public static final Iterable map(Apply f, Iter public static final Iterable map(ApplyBi f, Iterable> xs) { return FunctionalImpl.map(Function.of(f), xs); } + + /** + * @return all elements in {@code xs} transformed with {@code f}. + */ + public static final Iterable map(Apply3 f, Iterable> xs) { + return FunctionalImpl.map(Function.of(f), xs); + } /** * @return all elements in {@code xs} transformed with {@code f1} and {@code f2} respectively.