From 994a8648a47bcb5cf8b1c6db638eaabe1cef3830 Mon Sep 17 00:00:00 2001 From: "ivan.kolbukh" Date: Tue, 11 Oct 2022 21:58:28 +0300 Subject: [PATCH 1/2] 5-1-1-crazy-lambdas - test commit. --- .../src/main/java/com/bobocode/fp/CrazyLambdas.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/5-0-functional-programming/5-1-1-crazy-lambdas/src/main/java/com/bobocode/fp/CrazyLambdas.java b/5-0-functional-programming/5-1-1-crazy-lambdas/src/main/java/com/bobocode/fp/CrazyLambdas.java index 8b8a8168a..1985e12c3 100644 --- a/5-0-functional-programming/5-1-1-crazy-lambdas/src/main/java/com/bobocode/fp/CrazyLambdas.java +++ b/5-0-functional-programming/5-1-1-crazy-lambdas/src/main/java/com/bobocode/fp/CrazyLambdas.java @@ -26,7 +26,7 @@ public class CrazyLambdas { * @return a string supplier */ public static Supplier helloSupplier() { - throw new ExerciseNotCompletedException(); + return () -> "Hello"; } /** From 976698f93c9117345e61be552be82199cb668f2c Mon Sep 17 00:00:00 2001 From: "ivan.kolbukh" Date: Sun, 16 Oct 2022 09:42:24 +0300 Subject: [PATCH 2/2] 5-1-1-crazy-lambdas - completed task. --- .../java/com/bobocode/fp/CrazyLambdas.java | 368 +++++++++--------- 1 file changed, 191 insertions(+), 177 deletions(-) diff --git a/5-0-functional-programming/5-1-1-crazy-lambdas/src/main/java/com/bobocode/fp/CrazyLambdas.java b/5-0-functional-programming/5-1-1-crazy-lambdas/src/main/java/com/bobocode/fp/CrazyLambdas.java index 1985e12c3..799f2a367 100644 --- a/5-0-functional-programming/5-1-1-crazy-lambdas/src/main/java/com/bobocode/fp/CrazyLambdas.java +++ b/5-0-functional-programming/5-1-1-crazy-lambdas/src/main/java/com/bobocode/fp/CrazyLambdas.java @@ -4,6 +4,7 @@ import java.math.BigDecimal; import java.util.Map; +import java.util.concurrent.ThreadLocalRandom; import java.util.function.*; /** @@ -20,183 +21,196 @@ */ public class CrazyLambdas { - /** - * Returns {@link Supplier} that always supply "Hello" - * - * @return a string supplier - */ - public static Supplier helloSupplier() { + /** + * Returns {@link Supplier} that always supply "Hello" + * + * @return a string supplier + */ + public static Supplier helloSupplier() { return () -> "Hello"; - } - - /** - * Returns a {@link Predicate} of string that checks if string is empty - * - * @return a string predicate - */ - public static Predicate isEmptyPredicate() { - throw new ExerciseNotCompletedException(); - } - - /** - * Return a {@link Function} that accepts {@link String} and returns that string repeated n time, where n is passed - * as function argument - * - * @return function that repeats Strings - */ - public static BiFunction stringMultiplier() { - throw new ExerciseNotCompletedException(); - } - - /** - * Returns a {@link Function} that converts a {@link BigDecimal} number into a {@link String} that start with - * a dollar sign and then gets a value - * - * @return function that converts adds dollar sign - */ - public static Function toDollarStringFunction() { - throw new ExerciseNotCompletedException(); - } - - /** - * Receives two parameter that represent a range and returns a {@link Predicate} that verifies if string - * length is in the specified range. E.g. min <= length < max - * - * @param min min length - * @param max max length - * @return a string predicate - */ - public static Predicate lengthInRangePredicate(int min, int max) { - throw new ExerciseNotCompletedException(); - } - - /** - * Returns a {@link Supplier} of random integers - * - * @return int supplier - */ - public static IntSupplier randomIntSupplier() { - throw new ExerciseNotCompletedException(); - } - - - /** - * Returns an {@link IntUnaryOperator} that receives an int as a bound parameter, and returns a random int - * - * @return int operation - */ - public static IntUnaryOperator boundedRandomIntSupplier() { - throw new ExerciseNotCompletedException(); - } - - /** - * Returns {@link IntUnaryOperator} that calculates an integer square - * - * @return square operation - */ - public static IntUnaryOperator intSquareOperation() { - throw new ExerciseNotCompletedException(); - } - - /** - * Returns a {@link LongBinaryOperator} sum operation. - * - * @return binary sum operation - */ - public static LongBinaryOperator longSumOperation() { - throw new ExerciseNotCompletedException(); - } - - /** - * Returns a {@link ToIntFunction} that converts string to integer. - * - * @return string to int converter - */ - public static ToIntFunction stringToIntConverter() { - throw new ExerciseNotCompletedException(); - } - - /** - * Receives int parameter n, and returns a {@link Supplier} that supplies {@link IntUnaryOperator} - * that is a function f(x) = n * x - * - * @param n a multiplier - * @return a function supplier - */ - public static Supplier nMultiplyFunctionSupplier(int n) { - throw new ExerciseNotCompletedException(); - } - - /** - * Returns a {@link UnaryOperator} that accepts str to str function and returns the same function composed with trim - * - * @return function that composes functions with trim() function - */ - public static UnaryOperator> composeWithTrimFunction() { - throw new ExerciseNotCompletedException(); - } - - /** - * Receives a {@link Runnable} parameter, and returns a {@link Supplier}. The thread will be started only - * when you call supplier method {@link Supplier#get()} - * - * @param runnable the code you want to tun in new thread - * @return a thread supplier - */ - public static Supplier runningThreadSupplier(Runnable runnable) { - throw new ExerciseNotCompletedException(); - } - - /** - * Returns a {@link Consumer} that accepts {@link Runnable} as a parameter and runs in a new thread. - * - * @return a runnable consumer - */ - public static Consumer newThreadRunnableConsumer() { - throw new ExerciseNotCompletedException(); - } - - /** - * Returns a {@link Function} that accepts an instance of {@link Runnable} and returns a {@link Supplier} of a - * started {@link Thread} that is created from a given {@link Runnable} - * - * @return a function that transforms runnable into a thread supplier - */ - public static Function> runnableToThreadSupplierFunction() { - throw new ExerciseNotCompletedException(); - } - - /** - * Returns a {@link BiFunction} that has two parameters. First is {@link IntUnaryOperator} which is some integer function. - * Second is {@link IntPredicate} which is some integer condition. And the third is {@link IntUnaryOperator} which is - * a new composed function that uses provided predicate (second parameter of binary function) to verify its input - * parameter. If predicate returns {@code true} it applies a provided integer function - * (first parameter of binary function) and returns a result value, otherwise it returns an element itself. - * - * @return a binary function that receiver predicate and function and compose them to create a new function - */ - public static BiFunction functionToConditionalFunction() { - throw new ExerciseNotCompletedException(); - } - - /** - * Returns a {@link BiFunction} which first parameter is a {@link Map} where key is a function name, and value is some - * {@link IntUnaryOperator}, and second parameter is a {@link String} which is a function name. If the map contains a - * function by a given name then it is returned by high order function otherwise an identity() is returned. - * - * @return a high-order function that fetches a function from a function map by a given name or returns identity() - */ - public static BiFunction, String, IntUnaryOperator> functionLoader() { - throw new ExerciseNotCompletedException(); - } - - /** - * Returns {@link Supplier} of {@link Supplier} of {@link Supplier} of {@link String} "WELL DONE!". - * - * @return a supplier instance - */ - public static Supplier>> trickyWellDoneSupplier() { - throw new ExerciseNotCompletedException(); - } + } + + /** + * Returns a {@link Predicate} of string that checks if string is empty + * + * @return a string predicate + */ + public static Predicate isEmptyPredicate() { + return String::isEmpty; + } + + /** + * Return a {@link Function} that accepts {@link String} and returns that string repeated n time, where n is passed + * as function argument + * + * @return function that repeats Strings + */ + public static BiFunction stringMultiplier() { + return String::repeat; + } + + /** + * Returns a {@link Function} that converts a {@link BigDecimal} number into a {@link String} that start with + * a dollar sign and then gets a value + * + * @return function that converts adds dollar sign + */ + public static Function toDollarStringFunction() { + return numb -> "$" + numb; + } + + /** + * Receives two parameter that represent a range and returns a {@link Predicate} that verifies if string + * length is in the specified range. E.g. min <= length < max + * + * @param min min length + * @param max max length + * @return a string predicate + */ + public static Predicate lengthInRangePredicate(int min, int max) { + return str -> str.length() >= min && str.length() < max; + } + + /** + * Returns a {@link Supplier} of random integers + * + * @return int supplier + */ + public static IntSupplier randomIntSupplier() { + return () -> ThreadLocalRandom.current().nextInt(); + } + + + /** + * Returns an {@link IntUnaryOperator} that receives an int as a bound parameter, and returns a random int + * + * @return int operation + */ + public static IntUnaryOperator boundedRandomIntSupplier() { + return (bound) -> ThreadLocalRandom.current().nextInt(bound); + } + + /** + * Returns {@link IntUnaryOperator} that calculates an integer square + * + * @return square operation + */ + public static IntUnaryOperator intSquareOperation() { + return numb -> numb * numb; + } + + /** + * Returns a {@link LongBinaryOperator} sum operation. + * + * @return binary sum operation + */ + public static LongBinaryOperator longSumOperation() { + return Long::sum; + } + + /** + * Returns a {@link ToIntFunction} that converts string to integer. + * + * @return string to int converter + */ + public static ToIntFunction stringToIntConverter() { + return Integer::parseInt; + } + + /** + * Receives int parameter n, and returns a {@link Supplier} that supplies {@link IntUnaryOperator} + * that is a function f(x) = n * x + * + * @param n a multiplier + * @return a function supplier + */ + public static Supplier nMultiplyFunctionSupplier(int n) { + return () -> x -> x * n; + } + + /** + * Returns a {@link UnaryOperator} that accepts str to str function and returns the same function composed with trim + * + * @return function that composes functions with trim() function + */ + public static UnaryOperator> composeWithTrimFunction() { + return str -> str.compose(String::trim); + } + + /** + * Receives a {@link Runnable} parameter, and returns a {@link Supplier}. The thread will be started only + * when you call supplier method {@link Supplier#get()} + * + * @param runnable the code you want to tun in new thread + * @return a thread supplier + */ + public static Supplier runningThreadSupplier(Runnable runnable) { + return () -> { + Thread thread = new Thread(runnable); + thread.start(); + return thread; + }; + } + + /** + * Returns a {@link Consumer} that accepts {@link Runnable} as a parameter and runs in a new thread. + * + * @return a runnable consumer + */ + public static Consumer newThreadRunnableConsumer() { +// return Runnable::run; + return runnable -> new Thread(runnable).start(); + } + + /** + * Returns a {@link Function} that accepts an instance of {@link Runnable} and returns a {@link Supplier} of a + * started {@link Thread} that is created from a given {@link Runnable} + * + * @return a function that transforms runnable into a thread supplier + */ + public static Function> runnableToThreadSupplierFunction() { + return runnable -> () -> { + Thread thread = new Thread(runnable); + thread.start(); + return thread; + }; + } + + /** + * Returns a {@link BiFunction} that has two parameters. First is {@link IntUnaryOperator} which is some integer function. + * Second is {@link IntPredicate} which is some integer condition. And the third is {@link IntUnaryOperator} which is + * a new composed function that uses provided predicate (second parameter of binary function) to verify its input + * parameter. If predicate returns {@code true} it applies a provided integer function + * (first parameter of binary function) and returns a result value, otherwise it returns an element itself. + * + * @return a binary function that receiver predicate and function and compose them to create a new function + */ + public static BiFunction functionToConditionalFunction() { + return (first, second) -> a -> second.test(a) ? first.applyAsInt(a) : a; + } + + /** + * Returns a {@link BiFunction} which first parameter is a {@link Map} where key is a function name, and value is some + * {@link IntUnaryOperator}, and second parameter is a {@link String} which is a function name. If the map contains a + * function by a given name then it is returned by high order function otherwise an identity() is returned. + * + * @return a high-order function that fetches a function from a function map by a given name or returns identity() + */ + public static BiFunction, String, IntUnaryOperator> functionLoader() { + // This BiFunction accepts a map of functions and a function name, so we start form this + // `(functionMap, functionName) -> ...` then using a name we need to extract a function from map and return it + // or return `IntUnaryOperator.identity()` if no function was found. For this use case there is a default method + // of a class `Map` called `getOrDefault` + return (mapParam, name) -> (mapParam.getOrDefault(name, IntUnaryOperator.identity())); + } + + /** + * Returns {@link Supplier} of {@link Supplier} of {@link Supplier} of {@link String} "WELL DONE!". + * + * @return a supplier instance + */ + public static Supplier>> trickyWellDoneSupplier() { + return () -> () -> () -> "WELL DONE!"; + } }