From 2597999da8ecb7bc9cfc09246ed53a6154b7ec5b Mon Sep 17 00:00:00 2001 From: yjlee Date: Tue, 28 Jun 2022 08:22:43 +0900 Subject: [PATCH 1/5] =?UTF-8?q?=ED=8C=8C=EB=9D=BC=EB=AF=B8=ED=84=B0?= =?UTF-8?q?=EA=B0=80=20=EC=97=86=EB=8A=94=20=EC=9D=B5=EB=AA=85=ED=95=A8?= =?UTF-8?q?=EC=88=98=20=EB=9E=8C=EB=8B=A4=EC=8B=9D=EC=9C=BC=EB=A1=9C=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/nextstep/fp/CarTest.java | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/test/java/nextstep/fp/CarTest.java b/src/test/java/nextstep/fp/CarTest.java index 1ab1106f..13f23a49 100644 --- a/src/test/java/nextstep/fp/CarTest.java +++ b/src/test/java/nextstep/fp/CarTest.java @@ -8,13 +8,18 @@ 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(new MoveStrategy() { +// @Override +// public boolean isMovable() { +// return true; +// } +// }); + + Car lambdaCar = car.move(() ->{ + return true; }); - assertThat(actual).isEqualTo(new Car("pobi", 1)); + + assertThat(lambdaCar).isEqualTo(new Car("pobi", 1)); } @Test @@ -26,6 +31,9 @@ public boolean isMovable() { return false; } }); - assertThat(actual).isEqualTo(new Car("pobi", 0)); + + Car lambdaCar = car.move(() -> {return false;}); + + assertThat(lambdaCar).isEqualTo(new Car("pobi", 0)); } } From 0c6411e7d9585dfcfd047423ccf018d3c8bf7524 Mon Sep 17 00:00:00 2001 From: yjlee Date: Tue, 28 Jun 2022 08:45:27 +0900 Subject: [PATCH 2/5] =?UTF-8?q?return=EC=9D=B4=20=EC=97=86=EB=8A=94=20?= =?UTF-8?q?=EC=B6=94=EC=83=81=EB=A9=94=EC=84=9C=EB=93=9C=EB=A5=BC=20?= =?UTF-8?q?=ED=95=98=EB=82=98=20=EA=B0=80=EC=A7=84=20=EC=B6=94=EC=83=81=20?= =?UTF-8?q?=ED=81=B4=EB=9E=98=EC=8A=A4=20=EB=9E=8C=EB=8B=A4=EC=8B=9D=20?= =?UTF-8?q?=ED=91=9C=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/nextstep/fp/Lambda.java | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/main/java/nextstep/fp/Lambda.java b/src/main/java/nextstep/fp/Lambda.java index bd68fe1c..5183e22f 100644 --- a/src/main/java/nextstep/fp/Lambda.java +++ b/src/main/java/nextstep/fp/Lambda.java @@ -18,12 +18,14 @@ 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(new Runnable() { +// @Override +// public void run() { +// System.out.println("Hello from thread"); +// } +// }).start(); + // 추상메서드(abstract method) 가 한개인 Runnable 인터페이스를 파라미터로 받아 생성하는 Thread를 람다식으로 변경 + new Thread(() -> System.out.println("Hello from thread")); } public static int sumAll(List numbers) { From 13182d8d98efa70fbaab257b01398ad75c9669c8 Mon Sep 17 00:00:00 2001 From: yjlee Date: Thu, 30 Jun 2022 08:43:13 +0900 Subject: [PATCH 3/5] =?UTF-8?q?=EB=B3=80=EA=B2=BD=EB=90=98=EB=8A=94=20?= =?UTF-8?q?=EB=B6=80=EB=B6=84=EC=9D=84=20=EC=9D=B8=ED=84=B0=ED=8E=98?= =?UTF-8?q?=EC=9D=B4=EC=8A=A4=EB=A1=9C=20=EC=84=A0=EC=96=B8=ED=95=98?= =?UTF-8?q?=EA=B3=A0=20=EC=9D=B8=ED=84=B0=ED=8E=98=EC=9D=B4=EC=8A=A4?= =?UTF-8?q?=EC=97=90=20=EB=8C=80=ED=95=9C=20=EA=B5=AC=ED=98=84=EC=B2=B4?= =?UTF-8?q?=EB=A5=BC=20=EB=9E=8C=EB=8B=A4=EC=8B=9D=EC=9C=BC=EB=A1=9C=20?= =?UTF-8?q?=ED=91=9C=ED=98=84=ED=95=B4=20=EB=A9=94=EC=86=8C=EB=93=9C=20?= =?UTF-8?q?=EC=9D=B8=EC=9E=90=EB=A1=9C=20=EC=A0=84=EB=8B=AC=ED=95=98?= =?UTF-8?q?=EC=97=AC=20=EB=B3=80=EA=B2=BD=ED=95=9C=20=EB=9E=8C=EB=8B=A4?= =?UTF-8?q?=EC=8B=9D?= 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 | 17 +++++++++++++---- src/test/java/nextstep/fp/LambdaTest.java | 6 +++--- 3 files changed, 22 insertions(+), 7 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 00000000..ccb4bb1b --- /dev/null +++ b/src/main/java/nextstep/fp/Conditional.java @@ -0,0 +1,6 @@ +package nextstep.fp; + +public interface Conditional { + + boolean test(Integer number); +} diff --git a/src/main/java/nextstep/fp/Lambda.java b/src/main/java/nextstep/fp/Lambda.java index 5183e22f..99b996a5 100644 --- a/src/main/java/nextstep/fp/Lambda.java +++ b/src/main/java/nextstep/fp/Lambda.java @@ -1,8 +1,10 @@ package nextstep.fp; import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; -public class Lambda { +public class Lambda implements Conditional{ public static void printAllOld(List numbers) { System.out.println("printAllOld"); @@ -28,15 +30,16 @@ public static void runThread() { new Thread(() -> System.out.println("Hello from thread")); } - public static int sumAll(List numbers) { + public static int sumAll(List numbers,Conditional conditional) { int total = 0; for (int number : numbers) { total += number; } + return total; } - public static int sumAllEven(List numbers) { + public static int sumAllEven(List numbers,Conditional conditional) { int total = 0; for (int number : numbers) { if (number % 2 == 0) { @@ -46,13 +49,19 @@ public static int sumAllEven(List numbers) { return total; } - public static int sumAllOverThree(List numbers) { + public static int sumAllOverThree(List numbers, Conditional conditional) { int total = 0; for (int number : numbers) { if (number > 3) { total += number; } } + return total; } + + @Override + public boolean test(Integer number) { + return false; + } } diff --git a/src/test/java/nextstep/fp/LambdaTest.java b/src/test/java/nextstep/fp/LambdaTest.java index f240ac65..d1e5bfe7 100644 --- a/src/test/java/nextstep/fp/LambdaTest.java +++ b/src/test/java/nextstep/fp/LambdaTest.java @@ -33,19 +33,19 @@ public void runThread() throws Exception { @Test public void sumAll() throws Exception { - int sum = Lambda.sumAll(numbers); + int sum = Lambda.sumAll(numbers,(number) -> true); assertThat(sum).isEqualTo(21); } @Test public void sumAllEven() throws Exception { - int sum = Lambda.sumAllEven(numbers); + int sum = Lambda.sumAllEven(numbers,(number -> number %2 ==0)); assertThat(sum).isEqualTo(12); } @Test public void sumAllOverThree() throws Exception { - int sum = Lambda.sumAllOverThree(numbers); + int sum = Lambda.sumAllOverThree(numbers, (number) -> number > 3); assertThat(sum).isEqualTo(15); } } From b7e2d6004d035492ad56204d38d756b104308c15 Mon Sep 17 00:00:00 2001 From: yjlee Date: Thu, 30 Jun 2022 10:38:59 +0900 Subject: [PATCH 4/5] =?UTF-8?q?stream=EC=9D=84=20=ED=99=9C=EC=9A=A9?= =?UTF-8?q?=ED=95=9C=20list=20=EA=B0=92=20=EB=B3=80=EA=B2=BD=20Test?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/nextstep/fp/StreamStudy.java | 26 +++++++++++++++---- .../java/nextstep/fp/StreamStudyTest.java | 3 ++- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/src/main/java/nextstep/fp/StreamStudy.java b/src/main/java/nextstep/fp/StreamStudy.java index b446983a..211ffb16 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; @@ -16,18 +17,28 @@ public static long countWords() throws IOException { List words = Arrays.asList(contents.split("[\\P{L}]+")); long count = 0; - for (String w : words) { - if (w.length() > 12) count++; - } +// for (String w : words) { +// if (w.length() > 12) count++; +// } + count = words.stream().filter(word -> word.length() > 12 ).count(); 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 이 부분에 구현한다. + List longestSortedWordsTop100 = words.stream() + .distinct() + .filter(word -> word.length() > 12) + .sorted((a,b) -> {return b.length() - a.length();}) + .limit(100) + .map(word -> word.toLowerCase()) + .collect(Collectors.toList()); + + return longestSortedWordsTop100; } public static List doubleNumbers(List numbers) { @@ -39,6 +50,11 @@ public static long sumAll(List numbers) { } public static long sumOverThreeAndDouble(List numbers) { - return 0; + long result = numbers.stream() + .filter(number -> number > 3) + .map(number -> number * 2) + .reduce(0, (x, y) -> x + y); + + return result; } } \ 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 2962521a..0ecb003a 100644 --- a/src/test/java/nextstep/fp/StreamStudyTest.java +++ b/src/test/java/nextstep/fp/StreamStudyTest.java @@ -24,7 +24,8 @@ public void countWords() throws Exception { @Test public void printLongestWordTop100() throws Exception { - StreamStudy.printLongestWordTop100(); + List top100Words = StreamStudy.printLongestWordTop100(); + top100Words.forEach(System.out::println); } @Test From 2b587eb52e3b3c455d3b5e9b0451869ff6208349 Mon Sep 17 00:00:00 2001 From: yjlee Date: Thu, 30 Jun 2022 11:16:17 +0900 Subject: [PATCH 5/5] =?UTF-8?q?=EB=9E=8C=EB=8B=A4=EC=8B=9D=EC=9C=BC?= =?UTF-8?q?=EB=A1=9C=20=EB=A7=8C=EB=93=A0=20=EA=B0=9D=EC=B2=B4=EC=97=90=20?= =?UTF-8?q?=EC=A0=91=EA=B7=BC=ED=95=98=EA=B8=B0=20=EC=9C=84=ED=95=B4?= =?UTF-8?q?=EC=84=9C=20=EB=A7=8C=EB=93=A0=20=ED=95=A8=EC=88=98=ED=98=95=20?= =?UTF-8?q?=EC=9D=B8=ED=84=B0=ED=8E=98=EC=9D=B4=EC=8A=A4=20(=EC=B6=94?= =?UTF-8?q?=EC=83=81=20=EB=A9=94=EC=84=9C=EB=93=9C=EB=A5=BC=20=ED=95=98?= =?UTF-8?q?=EB=82=98=20=EA=B0=80=EC=A7=84=20=EC=9D=B8=ED=84=B0=ED=8E=98?= =?UTF-8?q?=EC=9D=B4=EC=8A=A4)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/nextstep/fp/Conditional.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/nextstep/fp/Conditional.java b/src/main/java/nextstep/fp/Conditional.java index ccb4bb1b..0cd77cdf 100644 --- a/src/main/java/nextstep/fp/Conditional.java +++ b/src/main/java/nextstep/fp/Conditional.java @@ -1,6 +1,6 @@ package nextstep.fp; +@FunctionalInterface public interface Conditional { - - boolean test(Integer number); + abstract boolean test(Integer number); }