Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@
* <p>
* 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<T> {
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ private Functions() {
public static FunctionMap<Integer, Integer> intFunctionMap() {
FunctionMap<Integer, Integer> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<String> helloSupplier() {
throw new ExerciseNotCompletedException();
return () -> "Hello";
}

/**
Expand All @@ -36,7 +39,8 @@ public static Supplier<String> helloSupplier() {
* @return a string predicate
*/
public static Predicate<String> isEmptyPredicate() {
throw new ExerciseNotCompletedException();

return String::isEmpty;
}

/**
Expand All @@ -46,7 +50,9 @@ public static Predicate<String> isEmptyPredicate() {
* @return function that repeats Strings
*/
public static BiFunction<String, Integer, String> stringMultiplier() {
throw new ExerciseNotCompletedException();
return (str,inter)-> Stream.iterate(str, x->x)
.limit(inter)
.collect(Collectors.joining(""));
}

/**
Expand All @@ -56,7 +62,7 @@ public static BiFunction<String, Integer, String> stringMultiplier() {
* @return function that converts adds dollar sign
*/
public static Function<BigDecimal, String> toDollarStringFunction() {
throw new ExerciseNotCompletedException();
return dec-> "$" + dec.toString();
}

/**
Expand All @@ -68,7 +74,7 @@ public static Function<BigDecimal, String> toDollarStringFunction() {
* @return a string predicate
*/
public static Predicate<String> lengthInRangePredicate(int min, int max) {
throw new ExerciseNotCompletedException();
return (s) -> min <= s.length() && s.length()< max ;
}

/**
Expand All @@ -77,7 +83,7 @@ public static Predicate<String> lengthInRangePredicate(int min, int max) {
* @return int supplier
*/
public static IntSupplier randomIntSupplier() {
throw new ExerciseNotCompletedException();
return random::nextInt;
}


Expand All @@ -87,7 +93,7 @@ public static IntSupplier randomIntSupplier() {
* @return int operation
*/
public static IntUnaryOperator boundedRandomIntSupplier() {
throw new ExerciseNotCompletedException();
return inter -> random.nextInt(inter);
}

/**
Expand All @@ -96,7 +102,7 @@ public static IntUnaryOperator boundedRandomIntSupplier() {
* @return square operation
*/
public static IntUnaryOperator intSquareOperation() {
throw new ExerciseNotCompletedException();
return inter -> inter * inter;
}

/**
Expand All @@ -105,7 +111,7 @@ public static IntUnaryOperator intSquareOperation() {
* @return binary sum operation
*/
public static LongBinaryOperator longSumOperation() {
throw new ExerciseNotCompletedException();
return Long::sum;
}

/**
Expand All @@ -114,7 +120,7 @@ public static LongBinaryOperator longSumOperation() {
* @return string to int converter
*/
public static ToIntFunction<String> stringToIntConverter() {
throw new ExerciseNotCompletedException();
return Integer::parseInt;
}

/**
Expand All @@ -125,7 +131,7 @@ public static ToIntFunction<String> stringToIntConverter() {
* @return a function supplier
*/
public static Supplier<IntUnaryOperator> nMultiplyFunctionSupplier(int n) {
throw new ExerciseNotCompletedException();
return ()-> x-> n * x;
}

/**
Expand All @@ -134,7 +140,7 @@ public static Supplier<IntUnaryOperator> nMultiplyFunctionSupplier(int n) {
* @return function that composes functions with trim() function
*/
public static UnaryOperator<Function<String, String>> composeWithTrimFunction() {
throw new ExerciseNotCompletedException();
return strFunction -> strFunction.compose(String::trim);
}

/**
Expand All @@ -145,7 +151,11 @@ public static UnaryOperator<Function<String, String>> composeWithTrimFunction()
* @return a thread supplier
*/
public static Supplier<Thread> runningThreadSupplier(Runnable runnable) {
throw new ExerciseNotCompletedException();
return () -> {
Thread t = new Thread(runnable);
t.start();
return t;
};
}

/**
Expand All @@ -154,7 +164,7 @@ public static Supplier<Thread> runningThreadSupplier(Runnable runnable) {
* @return a runnable consumer
*/
public static Consumer<Runnable> newThreadRunnableConsumer() {
throw new ExerciseNotCompletedException();
return runnable -> new Thread(runnable).start();
}

/**
Expand All @@ -164,7 +174,11 @@ public static Consumer<Runnable> newThreadRunnableConsumer() {
* @return a function that transforms runnable into a thread supplier
*/
public static Function<Runnable, Supplier<Thread>> runnableToThreadSupplierFunction() {
throw new ExerciseNotCompletedException();
return runnable -> () -> {
Thread t = new Thread(runnable);
t.start();
return t;
};
}

/**
Expand All @@ -177,7 +191,7 @@ public static Function<Runnable, Supplier<Thread>> runnableToThreadSupplierFunct
* @return a binary function that receiver predicate and function and compose them to create a new function
*/
public static BiFunction<IntUnaryOperator, IntPredicate, IntUnaryOperator> functionToConditionalFunction() {
throw new ExerciseNotCompletedException();
return (intFunction, condition) -> x -> condition.test(x) ? intFunction.applyAsInt(x) : x;
}

/**
Expand All @@ -188,7 +202,7 @@ public static BiFunction<IntUnaryOperator, IntPredicate, IntUnaryOperator> funct
* @return a high-order function that fetches a function from a function map by a given name or returns identity()
*/
public static BiFunction<Map<String, IntUnaryOperator>, String, IntUnaryOperator> functionLoader() {
throw new ExerciseNotCompletedException();
return (functionMap, functionName) -> functionMap.getOrDefault(functionName, IntUnaryOperator.identity());
}

/**
Expand All @@ -206,7 +220,7 @@ public static BiFunction<Map<String, IntUnaryOperator>, String, IntUnaryOperator
* @return a comparator instance
*/
public static <T, U extends Comparable<? super U>> Comparator<T> comparing(Function<? super T, ? extends U> mapper) {
throw new ExerciseNotCompletedException();
return (o1, o2) -> mapper.apply(o1).compareTo(mapper.apply(o2));
}

/**
Expand All @@ -226,7 +240,13 @@ public static <T, U extends Comparable<? super U>> Comparator<T> comparing(Funct
*/
public static <T, U extends Comparable<? super U>> Comparator<T> thenComparing(
Comparator<? super T> comparator, Function<? super T, ? extends U> mapper) {
throw new ExerciseNotCompletedException();
return (o1, o2) -> {
var initialResult = comparator.compare(o1, o2);
if (initialResult != 0) {
return initialResult;
}
return mapper.apply(o1).compareTo(mapper.apply(o2));
};
}

/**
Expand All @@ -235,7 +255,7 @@ public static <T, U extends Comparable<? super U>> Comparator<T> thenComparing(
* @return a supplier instance
*/
public static Supplier<Supplier<Supplier<String>>> trickyWellDoneSupplier() {
throw new ExerciseNotCompletedException();
return () -> () -> () -> "WELL DONE!";
}
}

Loading