Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 070ada1

Browse files
committed
GP-24 add CrazyLambdas, CrazyStreams and CrazyOptionals to functional-programming module
1 parent d6a431d commit 070ada1

File tree

22 files changed

+1805
-0
lines changed

22 files changed

+1805
-0
lines changed

‎6-0-functional-programming/pom.xml‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,12 @@
1616
<maven.compiler.source>11</maven.compiler.source>
1717
</properties>
1818

19+
<dependencies>
20+
<dependency>
21+
<groupId>com.bobocode</groupId>
22+
<artifactId>java-fundamentals-util</artifactId>
23+
<version>1.0-SNAPSHOT</version>
24+
</dependency>
25+
</dependencies>
26+
1927
</project>
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
package com.bobocode.lambdas;
2+
3+
import com.bobocode.util.ExerciseNotCompletedException;
4+
5+
import java.math.BigDecimal;
6+
import java.util.Map;
7+
import java.util.function.*;
8+
9+
public class CrazyLambdas {
10+
11+
/**
12+
* Returns {@link Supplier} that always supply "Hello"
13+
*
14+
* @return a string supplier
15+
*/
16+
public static Supplier<String> helloSupplier() {
17+
throw new ExerciseNotCompletedException();
18+
}
19+
20+
/**
21+
* Returns a {@link Predicate} of string that checks if string is empty
22+
*
23+
* @return a string predicate
24+
*/
25+
public static Predicate<String> isEmptyPredicate() {
26+
throw new ExerciseNotCompletedException();
27+
}
28+
29+
/**
30+
* Return a {@link Function} that accepts {@link String} and returns that string repeated n time, where n is passed
31+
* as function argument
32+
*
33+
* @return function that repeats Strings
34+
*/
35+
public static BiFunction<String, Integer, String> stringMultiplier() {
36+
throw new ExerciseNotCompletedException();
37+
}
38+
39+
/**
40+
* Returns a {@link Function} that converts a {@link BigDecimal} number into a {@link String} that start with
41+
* a dollar sign and then gets a value
42+
*
43+
* @return function that converts adds dollar sign
44+
*/
45+
public static Function<BigDecimal, String> toDollarStringFunction() {
46+
throw new ExerciseNotCompletedException();
47+
}
48+
49+
/**
50+
* Receives two parameter that represent a range and returns a {@link Predicate<String>} that verifies if string
51+
* length is in the specified range. E.g. min <= length < max
52+
*
53+
* @param min min length
54+
* @param max max length
55+
* @return a string predicate
56+
*/
57+
public static Predicate<String> lengthInRangePredicate(int min, int max) {
58+
throw new ExerciseNotCompletedException();
59+
}
60+
61+
/**
62+
* Returns a {@link Supplier} of random integers
63+
*
64+
* @return int supplier
65+
*/
66+
public static IntSupplier randomIntSupplier() {
67+
throw new ExerciseNotCompletedException();
68+
}
69+
70+
71+
/**
72+
* Returns an {@link IntUnaryOperator} that receives an int as a bound parameter, and returns a random int
73+
*
74+
* @return int operation
75+
*/
76+
public static IntUnaryOperator boundedRandomIntSupplier() {
77+
throw new ExerciseNotCompletedException();
78+
}
79+
80+
/**
81+
* Returns {@link IntUnaryOperator} that calculates an integer square
82+
*
83+
* @return square operation
84+
*/
85+
public static IntUnaryOperator intSquareOperation() {
86+
throw new ExerciseNotCompletedException();
87+
}
88+
89+
/**
90+
* Returns a {@link LongBinaryOperator} sum operation.
91+
*
92+
* @return binary sum operation
93+
*/
94+
public static LongBinaryOperator longSumOperation() {
95+
throw new ExerciseNotCompletedException();
96+
}
97+
98+
/**
99+
* Returns a {@link ToIntFunction<String>} that converts string to integer.
100+
*
101+
* @return string to int converter
102+
*/
103+
public static ToIntFunction<String> stringToIntConverter() {
104+
throw new ExerciseNotCompletedException();
105+
}
106+
107+
/**
108+
* Receives int parameter n, and returns a {@link Supplier} that supplies {@link IntUnaryOperator}
109+
* that is a function f(x) = n * x
110+
*
111+
* @param n a multiplier
112+
* @return a function supplier
113+
*/
114+
public static Supplier<IntUnaryOperator> nMultiplyFunctionSupplier(int n) {
115+
throw new ExerciseNotCompletedException();
116+
}
117+
118+
/**
119+
* Returns a {@link UnaryOperator} that accepts str to str function and returns the same function composed with trim
120+
*
121+
* @return function that composes functions with trim() function
122+
*/
123+
public static UnaryOperator<Function<String, String>> composeWithTrimFunction() {
124+
throw new ExerciseNotCompletedException();
125+
}
126+
127+
/**
128+
* Receives a {@link Runnable} parameter, and returns a {@link Supplier<Thread>}. The thread will be started only
129+
* when you call supplier method {@link Supplier#get()}
130+
*
131+
* @param runnable the code you want to tun in new thread
132+
* @return a thread supplier
133+
*/
134+
public static Supplier<Thread> runningThreadSupplier(Runnable runnable) {
135+
throw new ExerciseNotCompletedException();
136+
}
137+
138+
/**
139+
* Returns a {@link Consumer} that accepts {@link Runnable} as a parameter and runs in in a new thread.
140+
*
141+
* @return a runnable consumer
142+
*/
143+
public static Consumer<Runnable> newThreadRunnableConsumer() {
144+
throw new ExerciseNotCompletedException();
145+
}
146+
147+
/**
148+
* Returns a {@link Function} that accepts an instance of {@link Runnable} and returns a {@link Supplier} of a
149+
* started {@link Thread} that is created from a given {@link Runnable}
150+
*
151+
* @return a function that transforms runnable into a thread supplier
152+
*/
153+
public static Function<Runnable, Supplier<Thread>> runnableToThreadSupplierFunction() {
154+
throw new ExerciseNotCompletedException();
155+
}
156+
157+
/**
158+
* Returns a {@link BiFunction} that has two parameters. First is {@link IntUnaryOperator} which is some integer function.
159+
* Second is {@link IntPredicate} which is some integer condition. And the third is {@link IntUnaryOperator} which is
160+
* a new composed function that uses provided predicate (second parameter of binary function) to verify its input
161+
* parameter. If predicate returns {@code true} it applies a provided integer function
162+
* (first parameter of binary function) and returns a result value, otherwise it returns an element itself.
163+
*
164+
* @return a binary function that receiver predicate and function and compose them to create a new function
165+
*/
166+
public static BiFunction<IntUnaryOperator, IntPredicate, IntUnaryOperator> functionToConditionalFunction() {
167+
throw new ExerciseNotCompletedException();
168+
}
169+
170+
/**
171+
* Returns a {@link BiFunction} which first parameter is a {@link Map} where key is a function name, and value is some
172+
* {@link IntUnaryOperator}, and second parameter is a {@link String} which is a function name. If the map contains a
173+
* function by a given name then it is returned by high order function otherwise an identity() is returned.
174+
*
175+
* @return a high-order function that fetches a function from a function map by a given name or returns identity()
176+
*/
177+
public static BiFunction<Map<String, IntUnaryOperator>, String, IntUnaryOperator> functionLoader() {
178+
throw new ExerciseNotCompletedException();
179+
}
180+
181+
/**
182+
* Returns {@link Supplier} of {@link Supplier} of {@link Supplier} of {@link String} "WELL DONE".
183+
*
184+
* @return a supplier instance
185+
*/
186+
public static Supplier<Supplier<Supplier<String>>> trickyWellDoneSupplier() {
187+
throw new ExerciseNotCompletedException();
188+
}
189+
}
190+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# <img src="https://raw.githubusercontent.com/bobocode-projects/resources/master/image/logo_transparent_background.png" height=50/>Crazy lambda exercise :muscle:
2+
Improve your lambda skills
3+
### Task
4+
`CrazyLambdas` class consists of static methods that return various functions, operations and predicates.
5+
Your job is to implement the *todo* section of that class using **Lambda expressions** and **method reference**.
6+
To verify your implementation, run `CrazyLambdasTest.java`
7+
8+
### Pre-conditions :heavy_exclamation_mark:
9+
You're supposed to be familiar with Java 8
10+
11+
### How to start :question:
12+
* Just clone the repository and start implementing the **todo** section, verify your changes by running tests
13+
* If you don't have enough knowledge about this domain, check out the [links below](#related-materials-information_source)
14+
* Don't worry if you got stuck, checkout the **exercise/completed** branch and see the final implementation
15+
16+
### Related materials :information_source:
17+
* [Lambda tutorial](https://github.com/bobocode-projects/java-8-tutorial/tree/master/lambdas) <img src="https://raw.githubusercontent.com/bobocode-projects/resources/master/image/logo_transparent_background.png" height=20/>
18+
* [State of lambda (JSR 335)](http://htmlpreview.github.io/?https://github.com/bobocode-projects/resources/blob/master/java8/lambda/sotl.html)
19+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.bobocode.optionals;
2+
3+
public class AccountNotFoundException extends RuntimeException {
4+
public AccountNotFoundException(String message) {
5+
super(message);
6+
}
7+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.bobocode.optionals;
2+
3+
import com.bobocode.model.Account;
4+
5+
import java.util.Optional;
6+
7+
@FunctionalInterface
8+
public interface AccountProvider {
9+
Optional<Account> getAccount();
10+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.bobocode.optionals;
2+
3+
import com.bobocode.model.Account;
4+
5+
@FunctionalInterface
6+
public interface AccountService {
7+
void processAccount(Account account);
8+
9+
default void processWithNoAccount(){
10+
/* No operation */
11+
}
12+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /