This repository was archived by the owner on Feb 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathCrazyLambdas.java
More file actions
198 lines (175 loc) · 6.84 KB
/
CrazyLambdas.java
File metadata and controls
198 lines (175 loc) · 6.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package com.bobocode;
import java.math.BigDecimal;
import java.util.concurrent.ThreadLocalRandom;
import java.util.Map;
import java.util.function.*;
public class CrazyLambdas {
/**
* Returns {@link Supplier} that always supply "Hello"
*
* @return a string supplier
*/
public static Supplier<String> helloSupplier() {
return () -> "Hello";
}
/**
* Returns a {@link Predicate} of string that checks if string is empty
*
* @return a string predicate
*/
public static Predicate<String> isEmptyPredicate() {
return String::isEmpty;
}
/**
* Return a {@link Function} that accepts {@link String} and returns that string repeated n time, where n is passed
* as function argument
*
* @return function that repeats Strings
*/
public static BiFunction<String, Integer, String> stringMultiplier() {
return String::repeat;
}
/**
* Returns a {@link Function} that converts a {@link BigDecimal} number into a {@link String} that start with
* a dollar sign and then gets a value
*
* @return function that converts adds dollar sign
*/
public static Function<BigDecimal, String> toDollarStringFunction() {
return bigDecimal -> "$" + bigDecimal;
}
/**
* Receives two parameter that represent a range and returns a {@link Predicate<String>} that verifies if string
* length is in the specified range. E.g. min <= length < max
*
* @param min min length
* @param max max length
* @return a string predicate
*/
public static Predicate<String> lengthInRangePredicate(int min, int max) {
return str -> str.length() >= min && str.length() < max;
}
/**
* Returns a {@link Supplier} of random integers
*
* @return int supplier
*/
public static IntSupplier randomIntSupplier() {
return () -> ThreadLocalRandom.current().nextInt();
}
/**
* Returns an {@link IntUnaryOperator} that receives an int as a bound parameter, and returns a random int
*
* @return int operation
*/
public static IntUnaryOperator boundedRandomIntSupplier() {
return bound -> ThreadLocalRandom.current().nextInt(bound);
}
/**
* Returns {@link IntUnaryOperator} that calculates an integer square
*
* @return square operation
*/
public static IntUnaryOperator intSquareOperation() {
return a -> a * a;
}
/**
* Returns a {@link LongBinaryOperator} sum operation.
*
* @return binary sum operation
*/
public static LongBinaryOperator longSumOperation() {
return (a, b) -> a + b;
}
/**
* Returns a {@link ToIntFunction<String>} that converts string to integer.
*
* @return string to int converter
*/
public static ToIntFunction<String> stringToIntConverter() {
return Integer::parseInt;
}
/**
* Receives int parameter n, and returns a {@link Supplier} that supplies {@link IntUnaryOperator}
* that is a function f(x) = n * x
*
* @param n a multiplier
* @return a function supplier
*/
public static Supplier<IntUnaryOperator> nMultiplyFunctionSupplier(int n) {
return () -> a -> n * a;
}
/**
* Returns a {@link UnaryOperator} that accepts str to str function and returns the same function composed with trim
*
* @return function that composes functions with trim() function
*/
public static UnaryOperator<Function<String, String>> composeWithTrimFunction() {
return stringFunction -> stringFunction.compose(String::trim);
}
/**
* Receives a {@link Runnable} parameter, and returns a {@link Supplier<Thread>}. The thread will be started only
* when you call supplier method {@link Supplier#get()}
*
* @param runnable the code you want to tun in new thread
* @return a thread supplier
*/
public static Supplier<Thread> runningThreadSupplier(Runnable runnable) {
return () -> {
Thread thread = new Thread(runnable);
thread.start();
return thread;
};
}
/**
* Returns a {@link Consumer} that accepts {@link Runnable} as a parameter and runs in in a new thread.
*
* @return a runnable consumer
*/
public static Consumer<Runnable> newThreadRunnableConsumer() {
return runnable -> new Thread(runnable).start();
}
/**
* Returns a {@link Function} that accepts an instance of {@link Runnable} and returns a {@link Supplier} of a
* started {@link Thread} that is created from a given {@link Runnable}
*
* @return a function that transforms runnable into a thread supplier
*/
public static Function<Runnable, Supplier<Thread>> runnableToThreadSupplierFunction() {
return runnable -> () -> {
Thread thread = new Thread(runnable);
thread.start();
return thread;
};
}
/**
* Returns a {@link BiFunction} that has two parameters. First is {@link IntUnaryOperator} which is some integer function.
* Second is {@link IntPredicate} which is some integer condition. And the third is {@link IntUnaryOperator} which is
* a new composed function that uses provided predicate (second parameter of binary function) to verify its input
* parameter. If predicate returns {@code true} it applies a provided integer function
* (first parameter of binary function) and returns a result value, otherwise it returns an element itself.
*
* @return a binary function that receiver predicate and function and compose them to create a new function
*/
public static BiFunction<IntUnaryOperator, IntPredicate, IntUnaryOperator> functionToConditionalFunction() {
return (intOperation, intPredicate) -> a -> intPredicate.test(a) ? intOperation.applyAsInt(a) : a;
}
/**
* Returns a {@link BiFunction} which first parameter is a {@link Map} where key is a function name, and value is some
* {@link IntUnaryOperator}, and second parameter is a {@link String} which is a function name. If the map contains a
* function by a given name then it is returned by high order function otherwise an identity() is returned.
*
* @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() {
return (functionMap, functionName) -> functionMap.getOrDefault(functionName, IntUnaryOperator.identity());
}
/**
* Returns {@link Supplier} of {@link Supplier} of {@link Supplier} of {@link String} "WELL DONE".
*
* @return a supplier instance
*/
public static Supplier<Supplier<Supplier<String>>> trickyWellDoneSupplier() {
return () -> () -> () -> "WELL DONE!";
}
}