From fefd6eb2917b9a0856c247353545cc13876b6eda Mon Sep 17 00:00:00 2001 From: Ben Culkin Date: Tue, 1 Dec 2020 20:19:34 -0500 Subject: An assortment of changes/new things --- .../main/java/bjc/utils/funcutils/Callables.java | 60 ++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 base/src/main/java/bjc/utils/funcutils/Callables.java (limited to 'base/src/main/java/bjc/utils/funcutils/Callables.java') diff --git a/base/src/main/java/bjc/utils/funcutils/Callables.java b/base/src/main/java/bjc/utils/funcutils/Callables.java new file mode 100644 index 0000000..5895347 --- /dev/null +++ b/base/src/main/java/bjc/utils/funcutils/Callables.java @@ -0,0 +1,60 @@ +package bjc.utils.funcutils; + +import java.util.concurrent.*; +import java.util.function.*; + +/** + * Utility function for dealing with callables and other things. + * + * @author Ben Culkin + * + */ +public class Callables +{ + /** + * Perform a 'bind' that appends a function to a callable. + * + * @param The type originally returned by the callable. + * @param The type returned by the function. + * + * @param call The original callable. + * @param func The function to use to transform the result. + * + * @return A callable which applies the given function to the result of them. + */ + public static Callable bind( + Callable call, Function> func) + { + return () -> func.apply(call.call()).call(); + } + + /** + * Convert a normal function to a function on callables. + * + * @param The input to the function. + * @param The output from the function. + * + * @param func The function to convert. + * + * @return The function, made to work over callables. + */ + public static Function, Callable> + fmap(Function func) + { + return (inp) -> () -> func.apply(inp.call()); + } + + /** + * Convert a future into a callable. + * + * @param The type returned by the future. + * + * @param fut The future to convert. + * + * @return A future which yields that value. + */ + public static Callable obtain(Future fut) + { + return () -> fut.get(); + } +} -- cgit v1.2.3