From aa9bbc119e2c3e634b4a35643ab63d379993e6b6 Mon Sep 17 00:00:00 2001 From: Aliya Mansurova Date: Sun, 9 Jul 2017 20:52:51 +0300 Subject: [PATCH 1/2] part2 done --- .../part2/exercise/ArrowNotationExercise.java | 23 +++++++----- .../exercise/FunctionCombinationExercise.java | 37 ++++++++----------- 2 files changed, 29 insertions(+), 31 deletions(-) diff --git a/src/test/java/lambda/part2/exercise/ArrowNotationExercise.java b/src/test/java/lambda/part2/exercise/ArrowNotationExercise.java index c43edc4..2ae414b 100644 --- a/src/test/java/lambda/part2/exercise/ArrowNotationExercise.java +++ b/src/test/java/lambda/part2/exercise/ArrowNotationExercise.java @@ -1,8 +1,10 @@ package lambda.part2.exercise; import data.Person; +import javafx.util.Pair; import org.junit.Test; +import java.util.Comparator; import java.util.function.BiFunction; import java.util.function.BiPredicate; import java.util.function.Function; @@ -14,8 +16,7 @@ public class ArrowNotationExercise { @Test public void getAge() { // Person -> Integer - final Function getAge = null; // TODO - + final Function getAge = Person::getAge; assertEquals(Integer.valueOf(33), getAge.apply(new Person("", "", 33))); } @@ -23,26 +24,28 @@ public void getAge() { public void compareAges() { // TODO use BiPredicate // compareAges: (Person, Person) -> boolean - - throw new UnsupportedOperationException("Not implemented"); - //assertEquals(true, compareAges.test(new Person("a", "b", 22), new Person("c", "d", 22))); + final BiPredicate compareAges = (p1, p2) -> p1.getAge() == p2.getAge(); + assertEquals(true, compareAges.test(new Person("a", "b", 22), new Person("c", "d", 22))); } - // TODO // getFullName: Person -> String + final Function getFullName = person -> person.getFirstName() + person.getLastName(); + final BiPredicate compareNamesLength = (p1, p2) -> p1.getAge() == p2.getAge(); - // TODO // ageOfPersonWithTheLongestFullName: (Person -> String) -> (Person, Person) -> int - // + final BiFunction ageOfPersonWithTheLongestFullName(Function fullName) { + return (Person p1, Person p2) -> + (fullName.apply(p1).length() > fullName.apply(p2).length()) ? p1.getAge() : p2.getAge(); + } @Test public void getAgeOfPersonWithTheLongestFullName() { // Person -> String - final Function getFullName = null; // TODO + final Function getFullName = person -> person.getFirstName() + person.getLastName(); // (Person, Person) -> Integer // TODO use ageOfPersonWithTheLongestFullName(getFullName) - final BiFunction ageOfPersonWithTheLongestFullName = null; + final BiFunction ageOfPersonWithTheLongestFullName = ageOfPersonWithTheLongestFullName(getFullName); assertEquals( Integer.valueOf(1), diff --git a/src/test/java/lambda/part2/exercise/FunctionCombinationExercise.java b/src/test/java/lambda/part2/exercise/FunctionCombinationExercise.java index ce7080f..898780a 100644 --- a/src/test/java/lambda/part2/exercise/FunctionCombinationExercise.java +++ b/src/test/java/lambda/part2/exercise/FunctionCombinationExercise.java @@ -22,19 +22,16 @@ public void personHasNotEmptyLastNameAndFirstName0() { // TODO // negate1: (Person -> boolean) -> (Person -> boolean) private Predicate negate1(Predicate test) { - return p -> { - // TODO - throw new UnsupportedOperationException(); - }; + return p -> !(test.test(p)); + } - // TODO + // validateFirstNameAndLastName: (Person -> boolean, Person -> boolean) -> (Person -> boolean) private Predicate validateFirstNameAndLastName(Predicate t1, Predicate t2) { - return p -> { - // TODO - throw new UnsupportedOperationException(); - }; + return p -> + t1.test(p)&& t2.test(p); + } @Test @@ -52,18 +49,16 @@ public void personHasNotEmptyLastNameAndFirstName1() { assertEquals(false, validate.test(new Person("a", "", 0))); } - // TODO // negate: (T -> boolean) -> (T -> boolean) private Predicate negate(Predicate test) { - // TODO - throw new UnsupportedOperationException(); + return t -> !test.test(t); + //throw new UnsupportedOperationException(); } - // TODO // and: (T -> boolean, T -> boolean) -> (T -> boolean) private Predicate and(Predicate t1, Predicate t2) { - // TODO - throw new UnsupportedOperationException(); + return t -> t1.test(t) && t2.test(t); + // throw new UnsupportedOperationException(); } @Test @@ -71,10 +66,10 @@ public void personHasNotEmptyLastNameAndFirstName2() { final Predicate hasEmptyFirstName = p -> p.getFirstName().isEmpty(); final Predicate hasEmptyLastName = p -> p.getLastName().isEmpty(); - final Predicate validateFirstName = null; // TODO use negate - final Predicate validateLastName = null; // TODO use negate + final Predicate validateFirstName = negate(hasEmptyFirstName); // TODO use negate + final Predicate validateLastName = negate(hasEmptyLastName); // TODO use negate - final Predicate validate = null; // TODO use and + final Predicate validate = and(validateFirstName,validateLastName); // TODO use and assertEquals(true, validate.test(new Person("a", "b", 0))); assertEquals(false, validate.test(new Person("", "b", 0))); @@ -86,10 +81,10 @@ public void personHasNotEmptyLastNameAndFirstName3() { final Predicate hasEmptyFirstName = p -> p.getFirstName().isEmpty(); final Predicate hasEmptyLastName = p -> p.getLastName().isEmpty(); - final Predicate validateFirstName = null; // TODO use Predicate::negate - final Predicate validateLastName = null; // TODO use Predicate::negate + final Predicate validateFirstName = hasEmptyFirstName.negate(); // TODO use Predicate::negate + final Predicate validateLastName = hasEmptyLastName.negate(); // TODO use Predicate::negate - final Predicate validate = null; // TODO use Predicate::and + final Predicate validate = validateFirstName.and(validateLastName); // TODO use Predicate::and assertEquals(true, validate.test(new Person("a", "b", 0))); assertEquals(false, validate.test(new Person("", "b", 0))); From 5de5f22a429f8a18b58ab2aea53b4cecc5dbe811 Mon Sep 17 00:00:00 2001 From: Aliya Mansurova Date: Sun, 9 Jul 2017 21:50:08 +0300 Subject: [PATCH 2/2] part3 done --- .../java/lambda/part3/exercise/Mapping.java | 61 ++++++++++++++++--- 1 file changed, 52 insertions(+), 9 deletions(-) diff --git a/src/test/java/lambda/part3/exercise/Mapping.java b/src/test/java/lambda/part3/exercise/Mapping.java index c0a814a..7293aa3 100644 --- a/src/test/java/lambda/part3/exercise/Mapping.java +++ b/src/test/java/lambda/part3/exercise/Mapping.java @@ -32,10 +32,15 @@ public List getList() { // [T] -> (T -> R) -> [R] // [T1, T2, T3] -> (T -> R) -> [R1, R2, R3] public MapHelper map(Function f) { + final List result = new ArrayList(); + list.forEach(t -> result.add(f.apply(t))); + return new MapHelper(result); // TODO - throw new UnsupportedOperationException(); + // throw new UnsupportedOperationException(); } + ; + // [T] -> (T -> [R]) -> [R] // map: [T, T, T], T -> [R] => [[], [R1, R2], [R3, R4, R5]] @@ -76,12 +81,15 @@ public void mapping() { final List mappedEmployees = new MapHelper<>(employees) + .map(e -> e.withPerson(e.getPerson().withFirstName("John"))) + .map(e -> e.withJobHistory(addOneYear(e.getJobHistory()))) + .map(e -> e.withJobHistory(replaceQaWithQA(e.getJobHistory()))) + .getList(); /* .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 * */ - .getList(); final List expectedResult = Arrays.asList( @@ -108,10 +116,27 @@ public void mapping() { assertEquals(mappedEmployees, expectedResult); } + private List replaceQaWithQA(List jobHistory) { + return new MapHelper(jobHistory).map(jobHistoryEntry -> { + String position = jobHistoryEntry.getPosition(); + return jobHistoryEntry.withPosition(position.equals("qa") ? "QA" : position); + }).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) { @@ -120,19 +145,25 @@ public static LazyMapHelper from(List list) { public List force() { // TODO - throw new UnsupportedOperationException(); + List result = new ArrayList(list.size()); + list.forEach(item -> result.add(function.apply(item))); + return result; } public LazyMapHelper map(Function f) { // TODO - throw new UnsupportedOperationException(); + return new LazyMapHelper(list, function.andThen(f)); } } private static class LazyFlatMapHelper { + private final List list; + private final Function> function; public LazyFlatMapHelper(List list, Function> function) { + this.list = list; + this.function = function; } public static LazyFlatMapHelper from(List list) { @@ -141,13 +172,23 @@ public static LazyFlatMapHelper from(List list) { public List force() { // TODO - throw new UnsupportedOperationException(); + List result = new ArrayList(list.size()); + list.forEach(item -> result.addAll(function.apply(item))); + return result; } // TODO filter // (T -> boolean) -> (T -> [T]) // filter: [T1, T2] -> (T -> boolean) -> [T2] // flatMap": [T1, T2] -> (T -> [T]) -> [T2] + public LazyFlatMapHelper filter(Predicate p) { + final Function> listFunction = rTorListR(p); + return flatMap(listFunction); + } + + private Function> rTorListR(Predicate p) { + return r -> p.test(r) ? Collections.singletonList(r) : Collections.emptyList(); + } public LazyFlatMapHelper map(Function f) { final Function> listFunction = rR2TorListR2(f); @@ -156,7 +197,7 @@ public LazyFlatMapHelper map(Function f) { // (R -> R2) -> (R -> [R2]) private Function> rR2TorListR2(Function f) { - throw new UnsupportedOperationException(); + return f.andThen(Arrays::asList); } // TODO * @@ -166,7 +207,6 @@ public LazyFlatMapHelper flatMap(Function> f) { } - @Test public void lazy_mapping() { final List employees = @@ -193,13 +233,16 @@ public void lazy_mapping() { final List mappedEmployees = LazyMapHelper.from(employees) + .map(e -> e.withPerson(e.getPerson().withFirstName("John"))) // change name to John + .map(e -> e.withJobHistory(addOneYear(e.getJobHistory()))) // add 1 year to experience duration + .map(e -> e.withJobHistory(replaceQaWithQA(e.getJobHistory()))) // replace qa with QA + .force(); + /* .map(TODO) // change name to John .map(TODO) // add 1 year to experience duration .map(TODO) // replace qa with QA * */ - .force(); - final List expectedResult = Arrays.asList( new Employee(