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
162 lines (141 loc) · 5.14 KB
/
CrazyLambdas.java
File metadata and controls
162 lines (141 loc) · 5.14 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
package com.bobocode;
import java.math.BigDecimal;
import java.util.concurrent.ThreadLocalRandom;
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;
}
/**
* 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;
}
/**
* 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();
}
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 {@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!";
}
}