summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/funcutils/FuncUtils.java
blob: cb15d40dfa2d6dece85f216147c058c4cd1cd1d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package bjc.utils.funcutils;

import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;

/**
 * Utility things for functions
 *
 * @author ben
 *
 */
public class FuncUtils {
	/**
	 * Convert a binary function into a unary function that returns a
	 * function
	 *
	 * @param <A>
	 *                The initial type of the function
	 * @param <B>
	 *                The intermediate type of the function
	 * @param <C>
	 *                The terminal type of the function
	 * @param func
	 *                The function to transform
	 * @return The function transformed into a unary function returning a
	 *         function
	 */
	public static <A, B, C> Function<A, Function<B, C>> curry2(BiFunction<A, B, C> func) {
		return (arg1) -> (arg2) -> {
			return func.apply(arg1, arg2);
		};
	}

	/**
	 * Do the specified action the specified number of times
	 *
	 * @param nTimes
	 *                The number of times to do the action
	 * @param cons
	 *                The action to perform
	 */
	public static void doTimes(int nTimes, Consumer<Integer> cons) {
		for(int i = 0; i < nTimes; i++) {
			cons.accept(i);
		}
	}
}