Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 12 additions & 19 deletions src/main/java/nextstep/fp/Lambda.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,23 @@ public void run() {
}

public static int sumAll(List<Integer> numbers) {
int total = 0;
for (int number : numbers) {
total += number;
}
return total;
return numbers.stream().reduce(Integer::sum).get();
}

public interface Conditional {
boolean test(Integer number);
}
Comment on lines +33 to 35

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Condition 인터페이스를 내부에 선언하신 이유가 있으실까요?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

그리고 test 말고도 좋은 메서드 이름이 있을 것 같아요!


public static int sumAll(List<Integer> numbers,
Conditional c) {
// c.test(number)를 활용해 구현할 수 있다.
return numbers.stream().filter(c::test).reduce(Integer::sum).get();
}
public static int sumAllEven(List<Integer> numbers) {
int total = 0;
for (int number : numbers) {
if (number % 2 == 0) {
total += number;
}
}
return total;
return sumAll(numbers, (integer)->{return integer%2==0;});
}

public static int sumAllOverThree(List<Integer> numbers) {
int total = 0;
for (int number : numbers) {
if (number > 3) {
total += number;
}
}
return total;
return sumAll(numbers, (integer)->{return integer>3;});
Comment on lines 42 to +47

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

조건들도 메서드로 선언해서 깔끔하게 관리하는 것도 좋아보여요!

}
}
7 changes: 5 additions & 2 deletions src/main/java/nextstep/fp/StreamStudy.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

Expand All @@ -28,6 +29,7 @@ public static void printLongestWordTop100() throws IOException {
List<String> words = Arrays.asList(contents.split("[\\P{L}]+"));

// TODO 이 부분에 구현한다.
words.stream().filter(w -> w.length() > 12).sorted(Comparator.comparing(String::length).reversed()).distinct().limit(100).map(String::toLowerCase).forEach(System.out::println);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 방식으로 조건들을 한 번에 해결할 수 있군요! 멋집니다. 👍

}

public static List<Integer> doubleNumbers(List<Integer> numbers) {
Expand All @@ -39,6 +41,7 @@ public static long sumAll(List<Integer> numbers) {
}

public static long sumOverThreeAndDouble(List<Integer> numbers) {
return 0;
return doubleNumbers(numbers.stream().filter(x -> x>3).collect(Collectors.toList()))
.stream().reduce(Integer::sum).get();
}
}
}
11 changes: 4 additions & 7 deletions src/main/java/nextstep/optional/Expression.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package nextstep.optional;

import java.util.Arrays;
import java.util.Optional;

enum Expression {
PLUS("+"), MINUS("-"), TIMES("*"), DIVIDE("/");

Expand All @@ -14,12 +17,6 @@ private static boolean matchExpression(Expression e, String expression) {
}

static Expression of(String expression) {
for (Expression v : values()) {
if (matchExpression(v, expression)) {
return v;
}
}

throw new IllegalArgumentException(String.format("%s는 사칙연산에 해당하지 않는 표현식입니다.", expression));
return Arrays.stream(values()).filter(v -> matchExpression(v, expression)).findFirst().orElseThrow(() -> new IllegalArgumentException(String.format("%s는 사칙연산에 해당하지 않는 표현식입니다.", expression)));
}
}
4 changes: 3 additions & 1 deletion src/main/java/nextstep/optional/User.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package nextstep.optional;

import java.util.Optional;

public class User {
private String name;
private Integer age;
Expand Down Expand Up @@ -33,7 +35,7 @@ public static boolean ageIsInRange1(User user) {
}

public static boolean ageIsInRange2(User user) {
return false;
return Optional.ofNullable(user).map(x -> x.getAge()).filter(x -> x >=30 && x <= 45).isPresent();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

람다를 추가적으로 사용할 수 있습니다. ☺️

Suggested change
return Optional.ofNullable(user).map(x -> x.getAge()).filter(x -> x >=30 && x <= 45).isPresent();
return Optional.ofNullable(user).map(User::getAge).filter(x -> x >=30 && x <= 45).isPresent();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

x >=30 && x <= 45와 같은 조건을 의미있는 이름으로 메서드를 만들면 가독성이 더 좋을 것 같아요!

}

@Override
Expand Down
8 changes: 2 additions & 6 deletions src/main/java/nextstep/optional/Users.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

public class Users {
static final User DEFAULT_USER = new User("codesquad", 100);
Expand All @@ -13,11 +14,6 @@ public class Users {
new User("honux", 45));

User getUser(String name) {
for (User user : users) {
if (user.matchName(name)) {
return user;
}
}
return DEFAULT_USER;
return users.stream().filter(user -> user.matchName(name)).findAny().orElse(DEFAULT_USER);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

findAny() 메서드를 사용하신 이유가 있을까요? ☺️

}
}
14 changes: 2 additions & 12 deletions src/test/java/nextstep/fp/CarTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,14 @@ public class CarTest {
@Test
public void 이동() {
Car car = new Car("pobi", 0);
Car actual = car.move(new MoveStrategy() {
@Override
public boolean isMovable() {
return true;
}
});
Car actual = car.move(() -> {return true;});

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Car actual = car.move(() -> {return true;});
Car actual = car.move(() -> true);

assertThat(actual).isEqualTo(new Car("pobi", 1));
}

@Test
public void 정지() {
Car car = new Car("pobi", 0);
Car actual = car.move(new MoveStrategy() {
@Override
public boolean isMovable() {
return false;
}
});
Car actual = car.move(() ->{return false;});

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Car actual = car.move(() ->{return false;});
Car actual = car.move(() -> false);

assertThat(actual).isEqualTo(new Car("pobi", 0));
}
}