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(); } }