From 2138031fd9123b93bc23c6e0399476265ee837fd Mon Sep 17 00:00:00 2001 From: Nesmy Date: Fri, 11 Aug 2023 16:09:07 +0300 Subject: [PATCH] CrazyLambdas methods --- .../java/com/bobocode/fp/CrazyLambdas.java | 54 ++++++++++++------- 1 file changed, 34 insertions(+), 20 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 c836ad51a..387ee0064 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 @@ -5,6 +5,7 @@ import java.math.BigDecimal; import java.util.Comparator; import java.util.Map; +import java.util.Random; import java.util.function.*; /** @@ -27,7 +28,7 @@ public class CrazyLambdas { * @return a string supplier */ public static Supplier helloSupplier() { - throw new ExerciseNotCompletedException(); + return () -> "Hello"; } /** @@ -36,7 +37,7 @@ public static Supplier helloSupplier() { * @return a string predicate */ public static Predicate isEmptyPredicate() { - throw new ExerciseNotCompletedException(); + return String::isEmpty; } /** @@ -46,7 +47,7 @@ public static Predicate isEmptyPredicate() { * @return function that repeats Strings */ public static BiFunction stringMultiplier() { - throw new ExerciseNotCompletedException(); + return String::repeat; } /** @@ -56,7 +57,7 @@ public static BiFunction stringMultiplier() { * @return function that converts adds dollar sign */ public static Function toDollarStringFunction() { - throw new ExerciseNotCompletedException(); + return number -> "$" + number; } /** @@ -68,7 +69,7 @@ public static Function toDollarStringFunction() { * @return a string predicate */ public static Predicate lengthInRangePredicate(int min, int max) { - throw new ExerciseNotCompletedException(); + return str -> min <= str.length() && str.length() < max; } /** @@ -77,7 +78,7 @@ public static Predicate lengthInRangePredicate(int min, int max) { * @return int supplier */ public static IntSupplier randomIntSupplier() { - throw new ExerciseNotCompletedException(); + return () -> new Random().nextInt(); } @@ -87,7 +88,7 @@ public static IntSupplier randomIntSupplier() { * @return int operation */ public static IntUnaryOperator boundedRandomIntSupplier() { - throw new ExerciseNotCompletedException(); + return bound -> new Random().nextInt(bound); } /** @@ -96,7 +97,7 @@ public static IntUnaryOperator boundedRandomIntSupplier() { * @return square operation */ public static IntUnaryOperator intSquareOperation() { - throw new ExerciseNotCompletedException(); + return num -> num * num; } /** @@ -105,7 +106,7 @@ public static IntUnaryOperator intSquareOperation() { * @return binary sum operation */ public static LongBinaryOperator longSumOperation() { - throw new ExerciseNotCompletedException(); + return Long::sum; } /** @@ -114,7 +115,7 @@ public static LongBinaryOperator longSumOperation() { * @return string to int converter */ public static ToIntFunction stringToIntConverter() { - throw new ExerciseNotCompletedException(); + return Integer::parseInt; } /** @@ -125,7 +126,7 @@ public static ToIntFunction stringToIntConverter() { * @return a function supplier */ public static Supplier nMultiplyFunctionSupplier(int n) { - throw new ExerciseNotCompletedException(); + return () -> x -> n * x; } /** @@ -134,7 +135,7 @@ public static Supplier nMultiplyFunctionSupplier(int n) { * @return function that composes functions with trim() function */ public static UnaryOperator> composeWithTrimFunction() { - throw new ExerciseNotCompletedException(); + return strFunction -> strFunction.compose(String::trim); } /** @@ -145,7 +146,11 @@ public static UnaryOperator> composeWithTrimFunction() * @return a thread supplier */ public static Supplier runningThreadSupplier(Runnable runnable) { - throw new ExerciseNotCompletedException(); + return () -> { + Thread t = new Thread(runnable); + t.start(); + return t; + }; } /** @@ -154,7 +159,7 @@ public static Supplier runningThreadSupplier(Runnable runnable) { * @return a runnable consumer */ public static Consumer newThreadRunnableConsumer() { - throw new ExerciseNotCompletedException(); + return runnable -> new Thread(runnable).start(); } /** @@ -164,7 +169,11 @@ public static Consumer newThreadRunnableConsumer() { * @return a function that transforms runnable into a thread supplier */ public static Function> runnableToThreadSupplierFunction() { - throw new ExerciseNotCompletedException(); + return runnable -> () -> { + Thread t = new Thread(runnable); + t.start(); + return t; + }; } /** @@ -177,7 +186,7 @@ public static Function> runnableToThreadSupplierFunct * @return a binary function that receiver predicate and function and compose them to create a new function */ public static BiFunction functionToConditionalFunction() { - throw new ExerciseNotCompletedException(); + return (intFunction, intCondition) -> intParam -> intCondition.test(intParam) ? intFunction.applyAsInt(intParam) : intParam; } /** @@ -188,7 +197,7 @@ public static BiFunction funct * @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(); + return (map, functionName) -> map.getOrDefault(functionName, IntUnaryOperator.identity()); } /** @@ -206,7 +215,7 @@ public static BiFunction, String, IntUnaryOperator * @return a comparator instance */ public static > Comparator comparing(Function mapper) { - throw new ExerciseNotCompletedException(); + return Comparator.comparing(mapper); } /** @@ -226,7 +235,12 @@ public static > Comparator comparing(Funct */ public static > Comparator thenComparing( Comparator comparator, Function mapper) { - throw new ExerciseNotCompletedException(); + + return (o1, o2) -> { + int result = comparator.compare(o1, o2); + return result != 0 ? result + : mapper.apply(o1).compareTo(mapper.apply(o2)); + }; } /** @@ -235,7 +249,7 @@ public static > Comparator thenComparing( * @return a supplier instance */ public static Supplier>> trickyWellDoneSupplier() { - throw new ExerciseNotCompletedException(); + return () -> () -> () -> "WELL DONE!"; } }