Skip to content
Closed
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 @@ -5,6 +5,7 @@
import java.math.BigDecimal;
import java.util.Comparator;
import java.util.Map;
import java.util.Random;
import java.util.function.*;

/**
Expand All @@ -27,7 +28,7 @@ public class CrazyLambdas {
* @return a string supplier
*/
public static Supplier<String> helloSupplier() {
throw new ExerciseNotCompletedException();
return () -> "Hello";
}

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

/**
Expand All @@ -46,7 +47,7 @@ public static Predicate<String> isEmptyPredicate() {
* @return function that repeats Strings
*/
public static BiFunction<String, Integer, String> stringMultiplier() {
throw new ExerciseNotCompletedException();
return String::repeat;
}

/**
Expand All @@ -56,7 +57,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 number -> "$" + number;
}

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

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


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

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

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

/**
Expand All @@ -114,7 +115,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 +126,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 +135,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 +146,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 +159,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 +169,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 +186,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, intCondition) -> intParam -> intCondition.test(intParam) ? intFunction.applyAsInt(intParam) : intParam;
}

/**
Expand All @@ -188,7 +197,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 (map, functionName) -> map.getOrDefault(functionName, IntUnaryOperator.identity());
}

/**
Expand All @@ -206,7 +215,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 Comparator.comparing(mapper);
}

/**
Expand All @@ -226,7 +235,12 @@ 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) -> {
int result = comparator.compare(o1, o2);
return result != 0 ? result
: mapper.apply(o1).compareTo(mapper.apply(o2));
};
}

/**
Expand All @@ -235,7 +249,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!";
}
}