Skip to content

Commit dfd61ae

Browse files
Andrii BobrovAndrii Bobrov
Andrii Bobrov
authored and
Andrii Bobrov
committed
HOMEWORK bobocode-projects#7 – FUNCTIONAL PROGRAMMING
- p2
1 parent 3d74eb1 commit dfd61ae

File tree

1 file changed

+20
-15
lines changed
  • 5-0-functional-programming/5-1-1-crazy-lambdas/src/main/java/com/bobocode/fp

1 file changed

+20
-15
lines changed

5-0-functional-programming/5-1-1-crazy-lambdas/src/main/java/com/bobocode/fp/CrazyLambdas.java

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import java.math.BigDecimal;
66
import java.util.Map;
7+
import java.util.concurrent.ThreadLocalRandom;
78
import java.util.function.*;
89

910
/**
@@ -21,7 +22,7 @@ public class CrazyLambdas {
2122
* @return a string supplier
2223
*/
2324
public static Supplier<String> helloSupplier() {
24-
throw new ExerciseNotCompletedException();
25+
return () -> "Hello";
2526
}
2627

2728
/**
@@ -30,7 +31,7 @@ public static Supplier<String> helloSupplier() {
3031
* @return a string predicate
3132
*/
3233
public static Predicate<String> isEmptyPredicate() {
33-
throw new ExerciseNotCompletedException();
34+
return String::isEmpty;
3435
}
3536

3637
/**
@@ -40,7 +41,7 @@ public static Predicate<String> isEmptyPredicate() {
4041
* @return function that repeats Strings
4142
*/
4243
public static BiFunction<String, Integer, String> stringMultiplier() {
43-
throw new ExerciseNotCompletedException();
44+
return String::repeat;
4445
}
4546

4647
/**
@@ -50,7 +51,7 @@ public static BiFunction<String, Integer, String> stringMultiplier() {
5051
* @return function that converts adds dollar sign
5152
*/
5253
public static Function<BigDecimal, String> toDollarStringFunction() {
53-
throw new ExerciseNotCompletedException();
54+
return big -> String.format("$%s", big);
5455
}
5556

5657
/**
@@ -62,7 +63,7 @@ public static Function<BigDecimal, String> toDollarStringFunction() {
6263
* @return a string predicate
6364
*/
6465
public static Predicate<String> lengthInRangePredicate(int min, int max) {
65-
throw new ExerciseNotCompletedException();
66+
return str -> min <= str.length() && str.length() < max;
6667
}
6768

6869
/**
@@ -71,7 +72,7 @@ public static Predicate<String> lengthInRangePredicate(int min, int max) {
7172
* @return int supplier
7273
*/
7374
public static IntSupplier randomIntSupplier() {
74-
throw new ExerciseNotCompletedException();
75+
return () -> ThreadLocalRandom.current().nextInt();
7576
}
7677

7778

@@ -81,7 +82,7 @@ public static IntSupplier randomIntSupplier() {
8182
* @return int operation
8283
*/
8384
public static IntUnaryOperator boundedRandomIntSupplier() {
84-
throw new ExerciseNotCompletedException();
85+
return bound -> ThreadLocalRandom.current().nextInt(bound);
8586
}
8687

8788
/**
@@ -90,7 +91,7 @@ public static IntUnaryOperator boundedRandomIntSupplier() {
9091
* @return square operation
9192
*/
9293
public static IntUnaryOperator intSquareOperation() {
93-
throw new ExerciseNotCompletedException();
94+
return len -> len * len;
9495
}
9596

9697
/**
@@ -99,7 +100,7 @@ public static IntUnaryOperator intSquareOperation() {
99100
* @return binary sum operation
100101
*/
101102
public static LongBinaryOperator longSumOperation() {
102-
throw new ExerciseNotCompletedException();
103+
return Long::sum;
103104
}
104105

105106
/**
@@ -108,7 +109,7 @@ public static LongBinaryOperator longSumOperation() {
108109
* @return string to int converter
109110
*/
110111
public static ToIntFunction<String> stringToIntConverter() {
111-
throw new ExerciseNotCompletedException();
112+
return Integer::parseInt;
112113
}
113114

114115
/**
@@ -119,7 +120,7 @@ public static ToIntFunction<String> stringToIntConverter() {
119120
* @return a function supplier
120121
*/
121122
public static Supplier<IntUnaryOperator> nMultiplyFunctionSupplier(int n) {
122-
throw new ExerciseNotCompletedException();
123+
return () -> x -> x * n;
123124
}
124125

125126
/**
@@ -128,7 +129,7 @@ public static Supplier<IntUnaryOperator> nMultiplyFunctionSupplier(int n) {
128129
* @return function that composes functions with trim() function
129130
*/
130131
public static UnaryOperator<Function<String, String>> composeWithTrimFunction() {
131-
throw new ExerciseNotCompletedException();
132+
return function -> function.compose(String::trim);
132133
}
133134

134135
/**
@@ -139,7 +140,11 @@ public static UnaryOperator<Function<String, String>> composeWithTrimFunction()
139140
* @return a thread supplier
140141
*/
141142
public static Supplier<Thread> runningThreadSupplier(Runnable runnable) {
142-
throw new ExerciseNotCompletedException();
143+
return () -> {
144+
Thread thread = new Thread(runnable);
145+
thread.start();
146+
return thread;
147+
};
143148
}
144149

145150
/**
@@ -148,7 +153,7 @@ public static Supplier<Thread> runningThreadSupplier(Runnable runnable) {
148153
* @return a runnable consumer
149154
*/
150155
public static Consumer<Runnable> newThreadRunnableConsumer() {
151-
throw new ExerciseNotCompletedException();
156+
return Runnable::run;
152157
}
153158

154159
/**
@@ -158,7 +163,7 @@ public static Consumer<Runnable> newThreadRunnableConsumer() {
158163
* @return a function that transforms runnable into a thread supplier
159164
*/
160165
public static Function<Runnable, Supplier<Thread>> runnableToThreadSupplierFunction() {
161-
throw new ExerciseNotCompletedException();
166+
return CrazyLambdas::runningThreadSupplier;
162167
}
163168

164169
/**

0 commit comments

Comments
 (0)