Skip to content

Commit

Permalink
add some function helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
jyrimatti committed Jan 4, 2024
1 parent 37f5a96 commit 6724f55
Show file tree
Hide file tree
Showing 3 changed files with 188 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>fi.solita.utils</groupId>
<artifactId>functional-utils</artifactId>
<version>0.12.43</version>
<version>0.12.44</version>
<build>
<resources>
<resource>
Expand Down
180 changes: 180 additions & 0 deletions src/main/java/fi/solita/utils/functional/Function.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 <T1,T2,R> Function1<T2,R> ap(final Apply2<T1,T2,R> f, final T1 arg1) {
return new Function1<T2,R>() {
@Override
public final R apply(T2 arg2) {
return f.apply(arg1, arg2);
}
};
}

/**
* Partially apply {@code arg1} to {@code f}
*/
public static final <T1,T2,T3,R> Function2<T2,T3,R> ap(final Apply3<T1,T2,T3,R> f, final T1 arg1) {
return new Function2<T2,T3,R>() {
@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 <T1,T2,T3,R> Function1<T3,R> ap(final Apply3<T1,T2,T3,R> f, final T1 arg1, final T2 arg2) {
return new Function1<T3,R>() {
@Override
public final R apply(T3 arg3) {
return f.apply(arg1, arg2, arg3);
}
};
}

/**
* Partially apply {@code arg1} to {@code f}
*/
public static final <T1,T2,T3,T4,R> Function3<T2,T3,T4,R> ap(final Apply4<T1,T2,T3,T4,R> f, final T1 arg1) {
return new Function3<T2,T3,T4,R>() {
@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 <T1,T2,T3,T4,R> Function2<T3,T4,R> ap(final Apply4<T1,T2,T3,T4,R> f, final T1 arg1, final T2 arg2) {
return new Function2<T3,T4,R>() {
@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 <T1,T2,T3,T4,R> Function1<T4,R> ap(final Apply4<T1,T2,T3,T4,R> f, final T1 arg1, final T2 arg2, final T3 arg3) {
return new Function1<T4,R>() {
@Override
public final R apply(T4 arg4) {
return f.apply(arg1, arg2, arg3, arg4);
}
};
}

/**
* Partially apply {@code arg1} to {@code f}
*/
public static final <T1,T2,T3,T4,T5,R> Function4<T2,T3,T4,T5,R> ap(final Apply5<T1,T2,T3,T4,T5,R> f, final T1 arg1) {
return new Function4<T2,T3,T4,T5,R>() {
@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 <T1,T2,T3,T4,T5,R> Function3<T3,T4,T5,R> ap(final Apply5<T1,T2,T3,T4,T5,R> f, final T1 arg1, final T2 arg2) {
return new Function3<T3,T4,T5,R>() {
@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 <T1,T2,T3,T4,T5,R> Function2<T4,T5,R> ap(final Apply5<T1,T2,T3,T4,T5,R> f, final T1 arg1, final T2 arg2, final T3 arg3) {
return new Function2<T4,T5,R>() {
@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 <T1,T2,T3,T4,T5,R> Function1<T5,R> ap(final Apply5<T1,T2,T3,T4,T5,R> f, final T1 arg1, final T2 arg2, final T3 arg3, final T4 arg4) {
return new Function1<T5,R>() {
@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 <T1,T2,T3,T4,T5,T6,R> Function5<T2,T3,T4,T5,T6,R> ap(final Apply6<T1,T2,T3,T4,T5,T6,R> f, final T1 arg1) {
return new Function5<T2,T3,T4,T5,T6,R>() {
@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 <T1,T2,T3,T4,T5,T6,R> Function4<T3,T4,T5,T6,R> ap(final Apply6<T1,T2,T3,T4,T5,T6,R> f, final T1 arg1, final T2 arg2) {
return new Function4<T3,T4,T5,T6,R>() {
@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 <T1,T2,T3,T4,T5,T6,R> Function3<T4,T5,T6,R> ap(final Apply6<T1,T2,T3,T4,T5,T6,R> f, final T1 arg1, final T2 arg2, final T3 arg3) {
return new Function3<T4,T5,T6,R>() {
@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 <T1,T2,T3,T4,T5,T6,R> Function2<T5,T6,R> ap(final Apply6<T1,T2,T3,T4,T5,T6,R> f, final T1 arg1, final T2 arg2, final T3 arg3, final T4 arg4) {
return new Function2<T5,T6,R>() {
@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 <T1,T2,T3,T4,T5,T6,R> Function1<T6,R> ap(final Apply6<T1,T2,T3,T4,T5,T6,R> f, final T1 arg1, final T2 arg2, final T3 arg3, final T4 arg4, final T5 arg5) {
return new Function1<T6,R>() {
@Override
public final R apply(T6 arg6) {
return f.apply(arg1, arg2, arg3, arg4, arg5, arg6);
}
};
}
}

class PartialApplicationHelper {
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/fi/solita/utils/functional/Functional.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ public static final <S, T> Iterable<T> map(Apply<? super S, ? extends T> f, Iter
public static final <S1, S2, T> Iterable<T> map(ApplyBi<? super S1, ? super S2, ? extends T> f, Iterable<? extends Map.Entry<S1, S2>> xs) {
return FunctionalImpl.map(Function.of(f), xs);
}

/**
* @return all elements in {@code xs} transformed with {@code f}.
*/
public static final <S1, S2, S3, T> Iterable<T> map(Apply3<? super S1, ? super S2, ? super S3, ? extends T> f, Iterable<Tuple3<S1, S2, S3>> xs) {
return FunctionalImpl.map(Function.of(f), xs);
}

/**
* @return all elements in {@code xs} transformed with {@code f1} and {@code f2} respectively.
Expand Down

0 comments on commit 6724f55

Please sign in to comment.