From db41a9ab79bb58472a19a6897265e5499c55b99a Mon Sep 17 00:00:00 2001 From: sausageRoll Date: Thu, 6 Jul 2017 12:59:11 +0300 Subject: [PATCH] part3 completed --- .../java/lambda/part3/exercise/Mapping.java | 68 ++++++++++++------- 1 file changed, 44 insertions(+), 24 deletions(-) diff --git a/src/test/java/lambda/part3/exercise/Mapping.java b/src/test/java/lambda/part3/exercise/Mapping.java index c0a814a..fcc0b5c 100644 --- a/src/test/java/lambda/part3/exercise/Mapping.java +++ b/src/test/java/lambda/part3/exercise/Mapping.java @@ -9,7 +9,6 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; -import java.util.function.BiConsumer; import java.util.function.Consumer; import java.util.function.Function; import java.util.function.Predicate; @@ -32,8 +31,10 @@ public List getList() { // [T] -> (T -> R) -> [R] // [T1, T2, T3] -> (T -> R) -> [R1, R2, R3] public MapHelper map(Function f) { - // TODO - throw new UnsupportedOperationException(); + final List result = new ArrayList(); + list.forEach((T t) -> result.add(f.apply(t))); + + return new MapHelper(result); } // [T] -> (T -> [R]) -> [R] @@ -76,11 +77,9 @@ public void mapping() { final List mappedEmployees = new MapHelper<>(employees) - /* - .map(TODO) // change name to John .map(e -> e.withPerson(e.getPerson().withFirstName("John"))) - .map(TODO) // add 1 year to experience duration .map(e -> e.withJobHistory(addOneYear(e.getJobHistory()))) - .map(TODO) // replace qa with QA - * */ + .map(employee -> employee.withPerson(employee.getPerson().withFirstName("John"))) + .map(employee -> employee.withJobHistory(addOneYear(employee.getJobHistory()))) + .map(employee -> employee.withJobHistory(replaceQA(employee.getJobHistory()))) .getList(); final List expectedResult = @@ -108,10 +107,28 @@ public void mapping() { assertEquals(mappedEmployees, expectedResult); } + private List replaceQA(List jobHistory) { + return new MapHelper<>(jobHistory) + .map(job -> job.getPosition().equals("qa") ? + job.withPosition("QA") : + job + ) + .getList(); + } + + private List addOneYear(List jobHistory) { + return new MapHelper<>(jobHistory) + .map(jobHistoryEntry -> jobHistoryEntry.withDuration(jobHistoryEntry.getDuration() + 1)) + .getList(); + } private static class LazyMapHelper { + private final List list; + private final Function function; public LazyMapHelper(List list, Function function) { + this.list = list; + this.function = function; } public static LazyMapHelper from(List list) { @@ -119,36 +136,39 @@ public static LazyMapHelper from(List list) { } public List force() { - // TODO - throw new UnsupportedOperationException(); + return new MapHelper<>(list).map(function).getList(); } public LazyMapHelper map(Function f) { - // TODO - throw new UnsupportedOperationException(); + return new LazyMapHelper(list, function.andThen(f)); } - } private static class LazyFlatMapHelper { + private List list; + private Function function; - public LazyFlatMapHelper(List list, Function> function) { + public LazyFlatMapHelper(List list, Function function) { + this.list = list; + this.function = function; } public static LazyFlatMapHelper from(List list) { - throw new UnsupportedOperationException(); + return new LazyFlatMapHelper(list, Function.identity()); } public List force() { - // TODO - throw new UnsupportedOperationException(); + return new MapHelper<>(list).map(function).getList(); + } + + public LazyFlatMapHelper filter(Predicate p) { + return flatMap(r -> p.test(r) ? Collections.singletonList(r) : Collections.emptyList()); } // TODO filter // (T -> boolean) -> (T -> [T]) // filter: [T1, T2] -> (T -> boolean) -> [T2] // flatMap": [T1, T2] -> (T -> [T]) -> [T2] - public LazyFlatMapHelper map(Function f) { final Function> listFunction = rR2TorListR2(f); return flatMap(listFunction); @@ -165,7 +185,9 @@ public LazyFlatMapHelper flatMap(Function> f) { } } - + interface Traversable { + void forEach(Consumer consumer); + } @Test public void lazy_mapping() { @@ -193,11 +215,9 @@ public void lazy_mapping() { final List mappedEmployees = LazyMapHelper.from(employees) - /* - .map(TODO) // change name to John - .map(TODO) // add 1 year to experience duration - .map(TODO) // replace qa with QA - * */ + .map(employee -> employee.withPerson(employee.getPerson().withFirstName("John"))) + .map(employee -> employee.withJobHistory(addOneYear(employee.getJobHistory()))) + .map(employee -> employee.withJobHistory(replaceQA(employee.getJobHistory()))) .force(); final List expectedResult =