Skip to content

Commit 1ff71c7

Browse files
Integerouswotjd243
authored andcommitted
3주차 - Step1 리뷰 요청 드립니다. (#149)
* [step1] docs : 수업내용 저장 * [step1] feat : Lambda 실습 * [step1] feat : Stream 실습 * [step1] feat : Optional 실습
1 parent 0e9032f commit 1ff71c7

File tree

8 files changed

+64
-57
lines changed

8 files changed

+64
-57
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package nextstep.fp;
2+
3+
@FunctionalInterface
4+
public interface Conditional {
5+
boolean test(int number);
6+
}

src/main/java/nextstep/fp/Lambda.java

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,39 +18,24 @@ public static void printAllLambda(List<Integer> numbers) {
1818
}
1919

2020
public static void runThread() {
21-
new Thread(new Runnable() {
22-
@Override
23-
public void run() {
24-
System.out.println("Hello from thread");
25-
}
26-
}).start();
21+
new Thread(() -> System.out.println("Hello from thread")).start();
22+
}
23+
24+
public static int sum(List<Integer> numbers, Conditional condition) {
25+
return numbers.stream()
26+
.filter(condition::test)
27+
.reduce(0, Integer::sum);
2728
}
2829

2930
public static int sumAll(List<Integer> numbers) {
30-
int total = 0;
31-
for (int number : numbers) {
32-
total += number;
33-
}
34-
return total;
31+
return sum(numbers, number -> true);
3532
}
36-
33+
3734
public static int sumAllEven(List<Integer> numbers) {
38-
int total = 0;
39-
for (int number : numbers) {
40-
if (number % 2 == 0) {
41-
total += number;
42-
}
43-
}
44-
return total;
35+
return sum(numbers, number -> number % 2 == 0);
4536
}
4637

4738
public static int sumAllOverThree(List<Integer> numbers) {
48-
int total = 0;
49-
for (int number : numbers) {
50-
if (number > 3) {
51-
total += number;
52-
}
53-
}
54-
return total;
39+
return sum(numbers, number -> number > 3);
5540
}
5641
}

src/main/java/nextstep/fp/StreamStudy.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.nio.file.Files;
66
import java.nio.file.Paths;
77
import java.util.Arrays;
8+
import java.util.Comparator;
89
import java.util.List;
910
import java.util.stream.Collectors;
1011

@@ -22,12 +23,18 @@ public static long countWords() throws IOException {
2223
return count;
2324
}
2425

25-
public static void printLongestWordTop100() throws IOException {
26+
public static List<String> printLongestWordTop100() throws IOException {
2627
String contents = new String(Files.readAllBytes(Paths
2728
.get("src/main/resources/fp/war-and-peace.txt")), StandardCharsets.UTF_8);
2829
List<String> words = Arrays.asList(contents.split("[\\P{L}]+"));
29-
30-
// TODO 이 부분에 구현한다.
30+
31+
return words.stream()
32+
.filter(word -> word.length() > 12)
33+
.distinct()
34+
.limit(100)
35+
.sorted(Comparator.comparing(String::length).reversed())
36+
.map(String::toLowerCase)
37+
.collect(Collectors.toList());
3138
}
3239

3340
public static List<Integer> doubleNumbers(List<Integer> numbers) {
@@ -39,6 +46,9 @@ public static long sumAll(List<Integer> numbers) {
3946
}
4047

4148
public static long sumOverThreeAndDouble(List<Integer> numbers) {
42-
return 0;
49+
return numbers.stream()
50+
.filter(number -> number > 3)
51+
.map(number -> 2 * number)
52+
.reduce(0, Integer::sum);
4353
}
4454
}
Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package nextstep.optional;
22

3+
import java.util.Arrays;
4+
35
enum Expression {
46
PLUS("+"), MINUS("-"), TIMES("*"), DIVIDE("/");
57

@@ -14,12 +16,9 @@ private static boolean matchExpression(Expression e, String expression) {
1416
}
1517

1618
static Expression of(String expression) {
17-
for (Expression v : values()) {
18-
if (matchExpression(v, expression)) {
19-
return v;
20-
}
21-
}
22-
23-
throw new IllegalArgumentException(String.format("%s는 사칙연산에 해당하지 않는 표현식입니다.", expression));
19+
return Arrays.stream(Expression.values())
20+
.filter(value -> matchExpression(value, expression))
21+
.findFirst()
22+
.orElseThrow(() -> new IllegalArgumentException(String.format("%s는 사칙연산에 해당하지 않는 표현식입니다.", expression)));
2423
}
2524
}

src/main/java/nextstep/optional/User.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package nextstep.optional;
22

3+
import jdk.nashorn.internal.runtime.options.Option;
4+
5+
import java.util.Optional;
6+
37
public class User {
48
private String name;
59
private Integer age;
@@ -33,7 +37,10 @@ public static boolean ageIsInRange1(User user) {
3337
}
3438

3539
public static boolean ageIsInRange2(User user) {
36-
return false;
40+
return Optional.ofNullable(user)
41+
.map(User::getAge)
42+
.filter(age -> age >= 30 && age <= 45)
43+
.isPresent();
3744
}
3845

3946
@Override

src/main/java/nextstep/optional/Users.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,9 @@ public class Users {
1313
new User("honux", 45));
1414

1515
User getUser(String name) {
16-
for (User user : users) {
17-
if (user.matchName(name)) {
18-
return user;
19-
}
20-
}
21-
return DEFAULT_USER;
16+
return users.stream()
17+
.filter(user -> user.matchName(name))
18+
.findFirst()
19+
.orElse(DEFAULT_USER);
2220
}
2321
}

src/test/java/nextstep/fp/CarTest.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,29 @@
55
import static org.assertj.core.api.Assertions.assertThat;
66

77
public class CarTest {
8+
// @Test
9+
// public void 이동() {
10+
// Car car = new Car("pobi", 0);
11+
// Car actual = car.move(new MoveStrategy() {
12+
// @Override
13+
// public boolean isMovable() {
14+
// return true;
15+
// }
16+
// });
17+
// assertThat(actual).isEqualTo(new Car("pobi", 1));
18+
// }
19+
820
@Test
921
public void 이동() {
1022
Car car = new Car("pobi", 0);
11-
Car actual = car.move(new MoveStrategy() {
12-
@Override
13-
public boolean isMovable() {
14-
return true;
15-
}
16-
});
23+
Car actual = car.move(() -> true);
1724
assertThat(actual).isEqualTo(new Car("pobi", 1));
1825
}
1926

2027
@Test
2128
public void 정지() {
2229
Car car = new Car("pobi", 0);
23-
Car actual = car.move(new MoveStrategy() {
24-
@Override
25-
public boolean isMovable() {
26-
return false;
27-
}
28-
});
30+
Car actual = car.move(() -> false);
2931
assertThat(actual).isEqualTo(new Car("pobi", 0));
3032
}
3133
}

src/test/java/nextstep/fp/StreamStudyTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public void countWords() throws Exception {
2424

2525
@Test
2626
public void printLongestWordTop100() throws Exception {
27-
StreamStudy.printLongestWordTop100();
27+
StreamStudy.printLongestWordTop100().forEach(System.out::println);
2828
}
2929

3030
@Test

0 commit comments

Comments
 (0)