diff --git a/1-0-java-basics/1-3-0-hello-generics/src/main/java/com/bobocode/basics/Box.java b/1-0-java-basics/1-3-0-hello-generics/src/main/java/com/bobocode/basics/Box.java
index 5a2d860ee..180603a28 100644
--- a/1-0-java-basics/1-3-0-hello-generics/src/main/java/com/bobocode/basics/Box.java
+++ b/1-0-java-basics/1-3-0-hello-generics/src/main/java/com/bobocode/basics/Box.java
@@ -7,18 +7,18 @@
*
* todo: refactor this class so it uses generic type "T" and run {@link com.bobocode.basics.BoxTest} to verify it
*/
-public class Box {
- private Object value;
+public class Box {
+ private T value;
- public Box(Object value) {
+ public Box(T value) {
this.value = value;
}
- public Object getValue() {
+ public T getValue() {
return value;
}
- public void setValue(Object value) {
+ public void setValue(T value) {
this.value = value;
}
}
diff --git a/5-0-functional-programming/5-0-1-lambda-functions-map/src/main/java/com/bobocode/fp/Functions.java b/5-0-functional-programming/5-0-1-lambda-functions-map/src/main/java/com/bobocode/fp/Functions.java
index a1eed08d4..7de57d4e8 100644
--- a/5-0-functional-programming/5-0-1-lambda-functions-map/src/main/java/com/bobocode/fp/Functions.java
+++ b/5-0-functional-programming/5-0-1-lambda-functions-map/src/main/java/com/bobocode/fp/Functions.java
@@ -28,7 +28,12 @@ private Functions() {
public static FunctionMap intFunctionMap() {
FunctionMap intFunctionMap = new FunctionMap<>();
- // todo: according to the javadoc add functions using lambda expression
+ intFunctionMap.addFunction("abs", Math::abs);
+ intFunctionMap.addFunction("sgn", x -> Integer.compare(x,0));
+ intFunctionMap.addFunction("increment", x -> x + 1);
+ intFunctionMap.addFunction("decrement", x -> x-1);
+ intFunctionMap.addFunction("square", x -> x*x);
+
return intFunctionMap;
}
diff --git a/5-0-functional-programming/5-0-2-stream-sum-of-squares/src/main/java/com/bobocode/fp/SumOfSquares.java b/5-0-functional-programming/5-0-2-stream-sum-of-squares/src/main/java/com/bobocode/fp/SumOfSquares.java
index b043454d1..c973d5849 100644
--- a/5-0-functional-programming/5-0-2-stream-sum-of-squares/src/main/java/com/bobocode/fp/SumOfSquares.java
+++ b/5-0-functional-programming/5-0-2-stream-sum-of-squares/src/main/java/com/bobocode/fp/SumOfSquares.java
@@ -2,6 +2,9 @@
import com.bobocode.fp.exception.InvalidRangeException;
+import java.util.stream.IntStream;
+import java.util.stream.Stream;
+
/**
* This class allow to calculate a sum of squares of integer number in a certain range. It was implemented using
* OO approach. Your job is to refactor it using functional approach. E.g. avoid using mutable variables
@@ -25,11 +28,8 @@ static int calculateSumOfSquaresInRange(int startInclusive, int endInclusive) {
throw new InvalidRangeException();
}
- // todo: refactor using functional approach – instead of using for loop, use IntStream.rangeClose()
- int sumOfSquares = 0;
- for (int i = startInclusive; i <= endInclusive; i++) {
- sumOfSquares += i * i;
- }
- return sumOfSquares;
+ return IntStream.rangeClosed(startInclusive,endInclusive)
+ .map(i -> i * i)
+ .sum();
}
}
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..d89fb7bab 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,7 +5,10 @@
import java.math.BigDecimal;
import java.util.Comparator;
import java.util.Map;
+import java.util.Random;
import java.util.function.*;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
/**
* {@link CrazyLambdas} is an exercise class. Each method returns a functional interface and it should be implemented
@@ -20,14 +23,14 @@
* @author Taras Boychuk
*/
public class CrazyLambdas {
-
+ private static Random random = new Random();
/**
* Returns {@link Supplier} that always supply "Hello"
*
* @return a string supplier
*/
public static Supplier helloSupplier() {
- throw new ExerciseNotCompletedException();
+ return () -> "Hello";
}
/**
@@ -36,7 +39,8 @@ public static Supplier helloSupplier() {
* @return a string predicate
*/
public static Predicate isEmptyPredicate() {
- throw new ExerciseNotCompletedException();
+
+ return String::isEmpty;
}
/**
@@ -46,7 +50,9 @@ public static Predicate isEmptyPredicate() {
* @return function that repeats Strings
*/
public static BiFunction stringMultiplier() {
- throw new ExerciseNotCompletedException();
+ return (str,inter)-> Stream.iterate(str, x->x)
+ .limit(inter)
+ .collect(Collectors.joining(""));
}
/**
@@ -56,7 +62,7 @@ public static BiFunction stringMultiplier() {
* @return function that converts adds dollar sign
*/
public static Function toDollarStringFunction() {
- throw new ExerciseNotCompletedException();
+ return dec-> "$" + dec.toString();
}
/**
@@ -68,7 +74,7 @@ public static Function toDollarStringFunction() {
* @return a string predicate
*/
public static Predicate lengthInRangePredicate(int min, int max) {
- throw new ExerciseNotCompletedException();
+ return (s) -> min <= s.length() && s.length()< max ;
}
/**
@@ -77,7 +83,7 @@ public static Predicate lengthInRangePredicate(int min, int max) {
* @return int supplier
*/
public static IntSupplier randomIntSupplier() {
- throw new ExerciseNotCompletedException();
+ return random::nextInt;
}
@@ -87,7 +93,7 @@ public static IntSupplier randomIntSupplier() {
* @return int operation
*/
public static IntUnaryOperator boundedRandomIntSupplier() {
- throw new ExerciseNotCompletedException();
+ return inter -> random.nextInt(inter);
}
/**
@@ -96,7 +102,7 @@ public static IntUnaryOperator boundedRandomIntSupplier() {
* @return square operation
*/
public static IntUnaryOperator intSquareOperation() {
- throw new ExerciseNotCompletedException();
+ return inter -> inter * inter;
}
/**
@@ -105,7 +111,7 @@ public static IntUnaryOperator intSquareOperation() {
* @return binary sum operation
*/
public static LongBinaryOperator longSumOperation() {
- throw new ExerciseNotCompletedException();
+ return Long::sum;
}
/**
@@ -114,7 +120,7 @@ public static LongBinaryOperator longSumOperation() {
* @return string to int converter
*/
public static ToIntFunction stringToIntConverter() {
- throw new ExerciseNotCompletedException();
+ return Integer::parseInt;
}
/**
@@ -125,7 +131,7 @@ public static ToIntFunction stringToIntConverter() {
* @return a function supplier
*/
public static Supplier nMultiplyFunctionSupplier(int n) {
- throw new ExerciseNotCompletedException();
+ return ()-> x-> n * x;
}
/**
@@ -134,7 +140,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 +151,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 +164,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 +174,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 +191,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, condition) -> x -> condition.test(x) ? intFunction.applyAsInt(x) : x;
}
/**
@@ -188,7 +202,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