From 73318030f21aa95a66423e9a92532d24f170fd4e Mon Sep 17 00:00:00 2001 From: 1nteger Date: Fri, 28 Jun 2019 00:01:43 +0900 Subject: [PATCH 1/4] =?UTF-8?q?[step1]=20docs=20:=20=EC=88=98=EC=97=85?= =?UTF-8?q?=EB=82=B4=EC=9A=A9=20=EC=A0=80=EC=9E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/nextstep/fp/Conditional.java | 6 +++ src/main/java/nextstep/fp/Lambda.java | 60 ++++++++++++++-------- src/test/java/nextstep/fp/CarTest.java | 26 +++++----- 3 files changed, 60 insertions(+), 32 deletions(-) create mode 100644 src/main/java/nextstep/fp/Conditional.java diff --git a/src/main/java/nextstep/fp/Conditional.java b/src/main/java/nextstep/fp/Conditional.java new file mode 100644 index 0000000000..ca44d14bf2 --- /dev/null +++ b/src/main/java/nextstep/fp/Conditional.java @@ -0,0 +1,6 @@ +package nextstep.fp; + +@FunctionalInterface +public interface Conditional { + boolean test(int number); +} diff --git a/src/main/java/nextstep/fp/Lambda.java b/src/main/java/nextstep/fp/Lambda.java index 8fb6f38c91..9a60a04285 100644 --- a/src/main/java/nextstep/fp/Lambda.java +++ b/src/main/java/nextstep/fp/Lambda.java @@ -26,31 +26,51 @@ public void run() { }).start(); } + public static int sum(List numbers, Conditional conditional) { + int total = 0; + for (int number : numbers) { + if(conditional.test(number)) + total += number; + } + return total; + } + public static int sumAll(List numbers) { - int total = 0; - for (int number : numbers) { - total += number; - } - return total; +// int total = 0; +// for (int number : numbers) { +// total += number; +// } +// return total; + +// return sum(numbers, number -> true); + + return sum(numbers, new Conditional() { + @Override + public boolean test(int number) { + return false; + } + }); } - + public static int sumAllEven(List numbers) { - int total = 0; - for (int number : numbers) { - if (number % 2 == 0) { - total += number; - } - } - return total; +// int total = 0; +// for (int number : numbers) { +// if (number % 2 == 0) { +// total += number; +// } +// } +// return total; + return sum(numbers, number -> number % 2 == 0); } public static int sumAllOverThree(List numbers) { - int total = 0; - for (int number : numbers) { - if (number > 3) { - total += number; - } - } - return total; +// int total = 0; +// for (int number : numbers) { +// if (number > 3) { +// total += number; +// } +// } +// return total; + return sum(numbers, number -> number > 3); } } diff --git a/src/test/java/nextstep/fp/CarTest.java b/src/test/java/nextstep/fp/CarTest.java index 1ab1106fe2..e3dca18e5a 100644 --- a/src/test/java/nextstep/fp/CarTest.java +++ b/src/test/java/nextstep/fp/CarTest.java @@ -5,27 +5,29 @@ import static org.assertj.core.api.Assertions.assertThat; public class CarTest { +// @Test +// public void 이동() { +// Car car = new Car("pobi", 0); +// Car actual = car.move(new MoveStrategy() { +// @Override +// public boolean isMovable() { +// return 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 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(() -> false); assertThat(actual).isEqualTo(new Car("pobi", 0)); } } From 12543e7c1885701fdc3c52cc2c2e65b755ed7b65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=ED=95=9C=EC=A0=95=EC=88=98?= Date: Fri, 28 Jun 2019 16:00:08 +0900 Subject: [PATCH 2/4] =?UTF-8?q?[step1]=20feat=20:=20Lambda=20=EC=8B=A4?= =?UTF-8?q?=EC=8A=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/nextstep/fp/Lambda.java | 47 ++++----------------------- 1 file changed, 6 insertions(+), 41 deletions(-) diff --git a/src/main/java/nextstep/fp/Lambda.java b/src/main/java/nextstep/fp/Lambda.java index 9a60a04285..4d1175deb3 100644 --- a/src/main/java/nextstep/fp/Lambda.java +++ b/src/main/java/nextstep/fp/Lambda.java @@ -18,59 +18,24 @@ public static void printAllLambda(List numbers) { } public static void runThread() { - new Thread(new Runnable() { - @Override - public void run() { - System.out.println("Hello from thread"); - } - }).start(); + new Thread(() -> System.out.println("Hello from thread")).start(); } - public static int sum(List numbers, Conditional conditional) { - int total = 0; - for (int number : numbers) { - if(conditional.test(number)) - total += number; - } - return total; + public static int sum(List numbers, Conditional condition) { + return numbers.stream() + .filter(condition::test) + .reduce(0, Integer::sum); } public static int sumAll(List numbers) { -// int total = 0; -// for (int number : numbers) { -// total += number; -// } -// return total; - -// return sum(numbers, number -> true); - - return sum(numbers, new Conditional() { - @Override - public boolean test(int number) { - return false; - } - }); + return sum(numbers, number -> true); } public static int sumAllEven(List numbers) { -// int total = 0; -// for (int number : numbers) { -// if (number % 2 == 0) { -// total += number; -// } -// } -// return total; return sum(numbers, number -> number % 2 == 0); } public static int sumAllOverThree(List numbers) { -// int total = 0; -// for (int number : numbers) { -// if (number > 3) { -// total += number; -// } -// } -// return total; return sum(numbers, number -> number > 3); } } From 8a79f8e90dd589b78bf5222a7417d64fe1bf00bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=ED=95=9C=EC=A0=95=EC=88=98?= Date: Fri, 28 Jun 2019 16:22:32 +0900 Subject: [PATCH 3/4] =?UTF-8?q?[step1]=20feat=20:=20Stream=20=EC=8B=A4?= =?UTF-8?q?=EC=8A=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/nextstep/fp/StreamStudy.java | 18 ++++++++++++++---- src/test/java/nextstep/fp/StreamStudyTest.java | 2 +- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/main/java/nextstep/fp/StreamStudy.java b/src/main/java/nextstep/fp/StreamStudy.java index 74b0e6c7b1..5a6fd972c7 100644 --- a/src/main/java/nextstep/fp/StreamStudy.java +++ b/src/main/java/nextstep/fp/StreamStudy.java @@ -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; @@ -22,12 +23,18 @@ public static long countWords() throws IOException { return count; } - public static void printLongestWordTop100() throws IOException { + public static List printLongestWordTop100() throws IOException { String contents = new String(Files.readAllBytes(Paths .get("src/main/resources/fp/war-and-peace.txt")), StandardCharsets.UTF_8); List words = Arrays.asList(contents.split("[\\P{L}]+")); - - // TODO 이 부분에 구현한다. + + return words.stream() + .filter(word -> word.length() > 12) + .distinct() + .limit(100) + .sorted(Comparator.comparing(String::length).reversed()) + .map(String::toLowerCase) + .collect(Collectors.toList()); } public static List doubleNumbers(List numbers) { @@ -39,6 +46,9 @@ public static long sumAll(List numbers) { } public static long sumOverThreeAndDouble(List numbers) { - return 0; + return numbers.stream() + .filter(number -> number > 3) + .map(number -> 2 * number) + .reduce(0, Integer::sum); } } \ No newline at end of file diff --git a/src/test/java/nextstep/fp/StreamStudyTest.java b/src/test/java/nextstep/fp/StreamStudyTest.java index fe8e975eac..f998ec5d85 100644 --- a/src/test/java/nextstep/fp/StreamStudyTest.java +++ b/src/test/java/nextstep/fp/StreamStudyTest.java @@ -24,7 +24,7 @@ public void countWords() throws Exception { @Test public void printLongestWordTop100() throws Exception { - StreamStudy.printLongestWordTop100(); + StreamStudy.printLongestWordTop100().forEach(System.out::println); } @Test From a7a55d2de689dab4f848c172f6bc29ffc6943952 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=ED=95=9C=EC=A0=95=EC=88=98?= Date: Fri, 28 Jun 2019 17:02:30 +0900 Subject: [PATCH 4/4] =?UTF-8?q?[step1]=20feat=20:=20Optional=20=EC=8B=A4?= =?UTF-8?q?=EC=8A=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/nextstep/optional/Expression.java | 13 ++++++------- src/main/java/nextstep/optional/User.java | 9 ++++++++- src/main/java/nextstep/optional/Users.java | 10 ++++------ 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/main/java/nextstep/optional/Expression.java b/src/main/java/nextstep/optional/Expression.java index 67263f5d79..d2d444f270 100644 --- a/src/main/java/nextstep/optional/Expression.java +++ b/src/main/java/nextstep/optional/Expression.java @@ -1,5 +1,7 @@ package nextstep.optional; +import java.util.Arrays; + enum Expression { PLUS("+"), MINUS("-"), TIMES("*"), DIVIDE("/"); @@ -14,12 +16,9 @@ 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(Expression.values()) + .filter(value -> matchExpression(value, expression)) + .findFirst() + .orElseThrow(() -> new IllegalArgumentException(String.format("%s는 사칙연산에 해당하지 않는 표현식입니다.", expression))); } } diff --git a/src/main/java/nextstep/optional/User.java b/src/main/java/nextstep/optional/User.java index 56d7ee64d5..962537dbb2 100644 --- a/src/main/java/nextstep/optional/User.java +++ b/src/main/java/nextstep/optional/User.java @@ -1,5 +1,9 @@ package nextstep.optional; +import jdk.nashorn.internal.runtime.options.Option; + +import java.util.Optional; + public class User { private String name; private Integer age; @@ -33,7 +37,10 @@ public static boolean ageIsInRange1(User user) { } public static boolean ageIsInRange2(User user) { - return false; + return Optional.ofNullable(user) + .map(User::getAge) + .filter(age -> age >= 30 && age <= 45) + .isPresent(); } @Override diff --git a/src/main/java/nextstep/optional/Users.java b/src/main/java/nextstep/optional/Users.java index a7b8ab02d1..21f0e5ef8f 100644 --- a/src/main/java/nextstep/optional/Users.java +++ b/src/main/java/nextstep/optional/Users.java @@ -13,11 +13,9 @@ 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)) + .findFirst() + .orElse(DEFAULT_USER); } }