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 1baec88

Browse files
committed
GP-24 update readme and structure
1 parent 070ada1 commit 1baec88

File tree

11 files changed

+125
-56
lines changed

11 files changed

+125
-56
lines changed
Lines changed: 57 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,67 @@
1-
# <img src="https://raw.githubusercontent.com/bobocode-projects/resources/master/image/logo_transparent_background.png" height=50/>Crazy lambda exercise :muscle:
1+
# <img src="https://raw.githubusercontent.com/bobocode-projects/resources/master/image/logo_transparent_background.png" height=50/>Lambda exercises :muscle:
22
Improve your lambda skills
33
### 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-
4+
Every package contains **an exercise (interactive tutorial) on lambdas**, where you need to write code in order
5+
to complete the implementation. If you need more theory, you can **check [tutorial section](#tutorial)**.
6+
To verify your solution, you should **run a corresponding test :arrow_forward:.**
7+
88
### Pre-conditions :heavy_exclamation_mark:
99
You're supposed to be familiar with Java 8
1010

1111
### How to start :question:
1212
* Just clone the repository and start implementing the **todo** section, verify your changes by running tests
1313
* If you don't have enough knowledge about this domain, check out the [links below](#related-materials-information_source)
1414
* 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)
1915

16+
### Tutorial
17+
Java is an OOP language, so it always works with classes and **doesn't support standalone functions**.
18+
In case you want to **pass some function as a method parameter**, or **store some code into a variable**,
19+
you should use a *Functional Interface* and a *Lambda expression*.
20+
21+
* A *Functional Interface (FI)* represents a **function signature**. It contains only one abstract method.
22+
* A *Lambda expression* represents a **function body**. Is an anonymous function that implements the abstract method
23+
of the functional interface
24+
25+
The purpose of the lambda and functional interfaces is to **make it easier to create function objects**
26+
and provide an **ability to use some functional programming technics in Java.**
27+
28+
A typical example is interface `Comparator<T>`:
29+
30+
```java
31+
accounts.sort(new Comparator<Account>() {
32+
@Override
33+
public int compare(Account o1, Account o2) {
34+
return o1.getFirstName().compareTo(o2.getFirstName());
35+
}
36+
});
37+
```
38+
It can be easily simplified using lambda expression:
39+
```java
40+
accounts.sort((a1, a2) -> a1.getFirstName().compareTo(a2.getFirstName()));
41+
```
42+
In case you are calling some existing method inside the lambda, you can reference that method instead of actually
43+
calling it. This technique is called *Method Reference*. Combining it with usefull default method `comparing()`
44+
it can help you to simplify the code even more:
45+
```java
46+
accounts.sort(comparing(Account::getFirstName));
47+
```
48+
49+
### Best practices
50+
* use **lambdas instead of anonymous classes**
51+
* **avoid lambda parameter types**, unless it can improve code readability
52+
* **keep lambda expression small** (1 line is the best option)
53+
* **always use `@FunctionalInterface` annotation** for custom functional interfaces
54+
* **prefer standard predefined functional interfaces** (`java.util.function`)
55+
* create a **custom FI**, in case it has some **specific contract**, and you can **benefit from self-descriptive name** and **default methods**
56+
* **use special FI for primitives** (e.g. `IntToDoubleFunction` instead of `Function<Integer, Double>`)
57+
* **prefer method reference** in all cases where it helps to improve readability
58+
59+
### Related materials :information_source:
60+
* [State of lambda (JSR 335)](http://htmlpreview.github.io/?https://github.com/bobocode-projects/resources/blob/master/java8/lambda/sotl.html)
61+
* [Modern Java in Action](https://amzn.to/2KwUKW5) :green_book:
62+
* Passing code with behaviour parameterization - Chapter 2
63+
* Lambda expressions - Chapter 3
64+
* [Effective Java 3rd Edition](https://amzn.to/3mYA0U1) :blue_book:
65+
* Prefer Lambdas to anonymous classes – Item 42
66+
* Prefer method reference to lambdas - Item 43
67+
* Favor the use of standard functional interfaces - Item 44

‎6-0-functional-programming/src/main/java/com/bobocode/lambdas/CrazyLambdas.java renamed to ‎6-0-functional-programming/src/main/java/com/bobocode/lambdas/level2/CrazyLambdas.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
1-
package com.bobocode.lambdas;
1+
package com.bobocode.lambdas.level2;
22

33
import com.bobocode.util.ExerciseNotCompletedException;
44

55
import java.math.BigDecimal;
66
import java.util.Map;
77
import java.util.function.*;
88

9+
/**
10+
* {@link CrazyLambdas} is an exercise class. Each method returns a functional interface and it should be implemented
11+
* using either lambda or a method reference. Every method that is not implemented yet throws
12+
* {@link ExerciseNotCompletedException}.
13+
* <p>
14+
* TODO: remove exception and implement each method of this class using lambda or method reference
15+
*/
916
public class CrazyLambdas {
1017

1118
/**

‎6-0-functional-programming/src/main/java/com/bobocode/optionals/CrazyOptionals.java

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
import com.bobocode.data.Accounts;
44
import com.bobocode.model.Account;
55
import com.bobocode.model.CreditAccount;
6+
import com.bobocode.optionals.exception.AccountNotFoundException;
7+
import com.bobocode.optionals.function.AccountProvider;
8+
import com.bobocode.optionals.function.AccountService;
9+
import com.bobocode.optionals.function.CreditAccountProvider;
10+
import com.bobocode.util.ExerciseNotCompletedException;
611

712
import javax.annotation.Nonnull;
813
import javax.annotation.Nullable;
@@ -11,6 +16,13 @@
1116
import java.util.Optional;
1217
import java.util.OptionalDouble;
1318

19+
/**
20+
* {@link CrazyOptionals} is an exercise class. Each method represents some operation with a {@link Account} and
21+
* should be implemented using Optional API. Every method that is not implemented yet throws
22+
* {@link ExerciseNotCompletedException}.
23+
* <p>
24+
* TODO: remove exception and implement each method of this class using Optional API
25+
*/
1426
public class CrazyOptionals {
1527

1628
/**
@@ -20,7 +32,7 @@ public class CrazyOptionals {
2032
* @return optional object that holds text
2133
*/
2234
public static Optional<String> optionalOfString(@Nullable String text) {
23-
throw new UnsupportedOperationException("Some people say that method does not work until you implement it");
35+
throw new ExerciseNotCompletedException();
2436
}
2537

2638
/**
@@ -30,7 +42,7 @@ public static Optional<String> optionalOfString(@Nullable String text) {
3042
* @param amount money to deposit
3143
*/
3244
public static void deposit(AccountProvider accountProvider, BigDecimal amount) {
33-
throw new UnsupportedOperationException("Some people say that method does not work until you implement it");
45+
throw new ExerciseNotCompletedException();
3446
}
3547

3648
/**
@@ -40,7 +52,7 @@ public static void deposit(AccountProvider accountProvider, BigDecimal amount) {
4052
* @return optional object that holds account
4153
*/
4254
public static Optional<Account> optionalOfAccount(@Nonnull Account account) {
43-
throw new UnsupportedOperationException("Some people say that method does not work until you implement it");
55+
throw new ExerciseNotCompletedException();
4456
}
4557

4658
/**
@@ -52,7 +64,7 @@ public static Optional<Account> optionalOfAccount(@Nonnull Account account) {
5264
* @return account from provider or defaultAccount
5365
*/
5466
public static Account getAccount(AccountProvider accountProvider, Account defaultAccount) {
55-
throw new UnsupportedOperationException("Some people say that method does not work until you implement it");
67+
throw new ExerciseNotCompletedException();
5668
}
5769

5870
/**
@@ -63,7 +75,7 @@ public static Account getAccount(AccountProvider accountProvider, Account defaul
6375
* @param accountService
6476
*/
6577
public static void processAccount(AccountProvider accountProvider, AccountService accountService) {
66-
throw new UnsupportedOperationException("Some people say that method does not work until you implement it");
78+
throw new ExerciseNotCompletedException();
6779
}
6880

6981
/**
@@ -74,7 +86,7 @@ public static void processAccount(AccountProvider accountProvider, AccountServic
7486
* @return provided or generated account
7587
*/
7688
public static Account getOrGenerateAccount(AccountProvider accountProvider) {
77-
throw new UnsupportedOperationException("Some people say that method does not work until you implement it");
89+
throw new ExerciseNotCompletedException();
7890
}
7991

8092
/**
@@ -84,7 +96,7 @@ public static Account getOrGenerateAccount(AccountProvider accountProvider) {
8496
* @return optional balance
8597
*/
8698
public static Optional<BigDecimal> retrieveBalance(AccountProvider accountProvider) {
87-
throw new UnsupportedOperationException("Some people say that method does not work until you implement it");
99+
throw new ExerciseNotCompletedException();
88100
}
89101

90102
/**
@@ -95,7 +107,7 @@ public static Optional<BigDecimal> retrieveBalance(AccountProvider accountProvid
95107
* @return provided account
96108
*/
97109
public static Account getAccount(AccountProvider accountProvider) {
98-
throw new UnsupportedOperationException("Some people say that method does not work until you implement it");
110+
throw new ExerciseNotCompletedException();
99111
}
100112

101113
/**
@@ -105,7 +117,7 @@ public static Account getAccount(AccountProvider accountProvider) {
105117
* @return optional credit balance
106118
*/
107119
public static Optional<BigDecimal> retrieveCreditBalance(CreditAccountProvider accountProvider) {
108-
throw new UnsupportedOperationException("Some people say that method does not work until you implement it");
120+
throw new ExerciseNotCompletedException();
109121
}
110122

111123

@@ -117,7 +129,7 @@ public static Optional<BigDecimal> retrieveCreditBalance(CreditAccountProvider a
117129
* @return optional gmail account
118130
*/
119131
public static Optional<Account> retrieveAccountGmail(AccountProvider accountProvider) {
120-
throw new UnsupportedOperationException("Some people say that method does not work until you implement it");
132+
throw new ExerciseNotCompletedException();
121133
}
122134

123135
/**
@@ -130,7 +142,7 @@ public static Optional<Account> retrieveAccountGmail(AccountProvider accountProv
130142
* @return account got from either accountProvider or fallbackProvider
131143
*/
132144
public static Account getAccountWithFallback(AccountProvider accountProvider, AccountProvider fallbackProvider) {
133-
throw new UnsupportedOperationException("Some people say that method does not work until you implement it");
145+
throw new ExerciseNotCompletedException();
134146
}
135147

136148
/**
@@ -141,7 +153,7 @@ public static Account getAccountWithFallback(AccountProvider accountProvider, Ac
141153
* @return account with the highest balance
142154
*/
143155
public static Account getAccountWithMaxBalance(List<Account> accounts) {
144-
throw new UnsupportedOperationException("Some people say that method does not work until you implement it");
156+
throw new ExerciseNotCompletedException();
145157
}
146158

147159
/**
@@ -151,7 +163,7 @@ public static Account getAccountWithMaxBalance(List<Account> accounts) {
151163
* @return the lowest balance values
152164
*/
153165
public static OptionalDouble findMinBalanceValue(List<Account> accounts) {
154-
throw new UnsupportedOperationException("Some people say that method does not work until you implement it");
166+
throw new ExerciseNotCompletedException();
155167
}
156168

157169
/**
@@ -161,7 +173,7 @@ public static OptionalDouble findMinBalanceValue(List<Account> accounts) {
161173
* @param accountService
162174
*/
163175
public static void processAccountWithMaxBalance(List<Account> accounts, AccountService accountService) {
164-
throw new UnsupportedOperationException("Some people say that method does not work until you implement it");
176+
throw new ExerciseNotCompletedException();
165177
}
166178

167179
/**
@@ -171,7 +183,7 @@ public static void processAccountWithMaxBalance(List<Account> accounts, AccountS
171183
* @return total credit balance
172184
*/
173185
public static double calculateTotalCreditBalance(List<CreditAccount> accounts) {
174-
throw new UnsupportedOperationException("Some people say that method does not work until you implement it");
186+
throw new ExerciseNotCompletedException();
175187
}
176188
}
177189

‎6-0-functional-programming/src/main/java/com/bobocode/optionals/AccountNotFoundException.java renamed to ‎6-0-functional-programming/src/main/java/com/bobocode/optionals/exception/AccountNotFoundException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.bobocode.optionals;
1+
package com.bobocode.optionals.exception;
22

33
public class AccountNotFoundException extends RuntimeException {
44
public AccountNotFoundException(String message) {

‎6-0-functional-programming/src/main/java/com/bobocode/optionals/AccountProvider.java renamed to ‎6-0-functional-programming/src/main/java/com/bobocode/optionals/function/AccountProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.bobocode.optionals;
1+
package com.bobocode.optionals.function;
22

33
import com.bobocode.model.Account;
44

‎6-0-functional-programming/src/main/java/com/bobocode/optionals/AccountService.java renamed to ‎6-0-functional-programming/src/main/java/com/bobocode/optionals/function/AccountService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.bobocode.optionals;
1+
package com.bobocode.optionals.function;
22

33
import com.bobocode.model.Account;
44

‎6-0-functional-programming/src/main/java/com/bobocode/optionals/CreditAccountProvider.java renamed to ‎6-0-functional-programming/src/main/java/com/bobocode/optionals/function/CreditAccountProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.bobocode.optionals;
1+
package com.bobocode.optionals.function;
22

33
import com.bobocode.model.CreditAccount;
44

0 commit comments

Comments
(0)

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