From 9e83c2fdf91b6f79b1e879c39ccb7c399014c39d Mon Sep 17 00:00:00 2001 From: Ben Culkin Date: Mon, 14 Dec 2020 18:23:52 -0500 Subject: Add more combinators --- src/main/java/bjc/functypes/Combinators.java | 53 +++++++++++++--------------- 1 file changed, 25 insertions(+), 28 deletions(-) (limited to 'src/main') diff --git a/src/main/java/bjc/functypes/Combinators.java b/src/main/java/bjc/functypes/Combinators.java index bf63e47..fde13df 100644 --- a/src/main/java/bjc/functypes/Combinators.java +++ b/src/main/java/bjc/functypes/Combinators.java @@ -35,6 +35,25 @@ public class Combinators { return arg -> in.test(arg) ? ifTrue.apply(arg) : ifFalse.apply(arg); } + /** + * If-then-else expression. + * + * @param The output type. + * + * @param in The boolean to use. + * @param ifTrue The condition to run when it is true. + * @param ifFalse The condition to run when it is false. + * + * @return A value, based on the provided parameter + */ + public static Output iftt( + boolean in, + Supplier ifTrue, + Supplier ifFalse) + { + return in ? ifTrue.get() : ifFalse.get(); + } + /** * Execute an action before calling a function. * @@ -264,37 +283,15 @@ public class Combinators { } /** - * Return a function that does a series of actions upon a value, then returns - * that value. - * - * @param The type given as an argument - * - * @param functions The actions to perform on the value. - * - * @return A function that performs those arguments on a value. - */ - @SafeVarargs - public static Function doWith(Function... functions) - { - return (arg) -> { - for (Function function : functions) function.apply(arg); - return arg; - }; - } - - /** - * Perform a series of actions upon a value, then return that value. + * Convert a function into a consumer, ignoring its output. * - * @param The type given as an argument + * @param The input to the function. * - * @param input The value to use. - * @param functions The actions to perform on the value. + * @param func The function to convert. * - * @return A function that performs those arguments on a value. + * @return A consumer which calls the function, and ignores the output. */ - @SafeVarargs - public static Type with(Type input, Function... functions) - { - return doWith(functions).apply(input); + public static Consumer ignore(Function func) { + return (inp) -> func.apply(inp); } } \ No newline at end of file -- cgit v1.2.3