From 3349768c9d65bcdd548188996539f8d653bab384 Mon Sep 17 00:00:00 2001 From: Kirill Date: Tue, 4 Jul 2017 17:28:16 +0300 Subject: [PATCH 1/3] part2 completed --- .../part2/exercise/ArrowNotationExercise.java | 18 ++++++++---- .../exercise/FunctionCombinationExercise.java | 28 +++++++------------ 2 files changed, 22 insertions(+), 24 deletions(-) diff --git a/src/test/java/lambda/part2/exercise/ArrowNotationExercise.java b/src/test/java/lambda/part2/exercise/ArrowNotationExercise.java index c43edc4..e837b24 100644 --- a/src/test/java/lambda/part2/exercise/ArrowNotationExercise.java +++ b/src/test/java/lambda/part2/exercise/ArrowNotationExercise.java @@ -14,7 +14,7 @@ public class ArrowNotationExercise { @Test public void getAge() { // Person -> Integer - final Function getAge = null; // TODO + final Function getAge = Person::getAge; // TODO assertEquals(Integer.valueOf(33), getAge.apply(new Person("", "", 33))); } @@ -23,17 +23,22 @@ public void getAge() { public void compareAges() { // TODO use BiPredicate // compareAges: (Person, Person) -> boolean + BiPredicate compareAges = (p1, p2) -> p1.getAge() == p2.getAge(); - throw new UnsupportedOperationException("Not implemented"); - //assertEquals(true, compareAges.test(new Person("a", "b", 22), new Person("c", "d", 22))); + assertEquals(true, compareAges.test(new Person("a", "b", 22), new Person("c", "d", 22))); } // TODO // getFullName: Person -> String + public String getFullName(Person p) { + return String.format("%s %s", p.getFirstName(), p.getLastName()); + } // TODO - // ageOfPersonWithTheLongestFullName: (Person -> String) -> (Person, Person) -> int - // + // ageOfPersonWithTheLongestFullName: (Person -> String) -> (Person, Person) -> Integer + public BiFunction ageOfPersonWithTheLongestFullName(Function function) { + return (p1, p2) -> getFullName(p1).length() > getFullName(p2).length() ? p1.getAge(): p2.getAge(); + } @Test public void getAgeOfPersonWithTheLongestFullName() { @@ -42,7 +47,7 @@ public void getAgeOfPersonWithTheLongestFullName() { // (Person, Person) -> Integer // TODO use ageOfPersonWithTheLongestFullName(getFullName) - final BiFunction ageOfPersonWithTheLongestFullName = null; + final BiFunction ageOfPersonWithTheLongestFullName = ageOfPersonWithTheLongestFullName(getFullName); assertEquals( Integer.valueOf(1), @@ -51,3 +56,4 @@ public void getAgeOfPersonWithTheLongestFullName() { new Person("aa", "b", 1))); } } + diff --git a/src/test/java/lambda/part2/exercise/FunctionCombinationExercise.java b/src/test/java/lambda/part2/exercise/FunctionCombinationExercise.java index ce7080f..ea636b7 100644 --- a/src/test/java/lambda/part2/exercise/FunctionCombinationExercise.java +++ b/src/test/java/lambda/part2/exercise/FunctionCombinationExercise.java @@ -22,19 +22,13 @@ 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 @@ -55,15 +49,13 @@ public void personHasNotEmptyLastNameAndFirstName1() { // TODO // negate: (T -> boolean) -> (T -> boolean) private Predicate negate(Predicate test) { - // TODO - throw new UnsupportedOperationException(); + return t -> !test.test(t); } // 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); } @Test @@ -71,10 +63,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 +78,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 634dbbda0604e1e03fcd249d3fdcad4e102ba230 Mon Sep 17 00:00:00 2001 From: Kirill Date: Tue, 4 Jul 2017 17:32:24 +0300 Subject: [PATCH 2/3] code reformatting --- .../lambda/part2/exercise/ArrowNotationExercise.java | 12 ++++++++---- .../part2/exercise/FunctionCombinationExercise.java | 4 ++-- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/test/java/lambda/part2/exercise/ArrowNotationExercise.java b/src/test/java/lambda/part2/exercise/ArrowNotationExercise.java index e837b24..0af2bb2 100644 --- a/src/test/java/lambda/part2/exercise/ArrowNotationExercise.java +++ b/src/test/java/lambda/part2/exercise/ArrowNotationExercise.java @@ -25,18 +25,21 @@ public void compareAges() { // compareAges: (Person, Person) -> boolean BiPredicate compareAges = (p1, p2) -> p1.getAge() == p2.getAge(); - assertEquals(true, compareAges.test(new Person("a", "b", 22), new Person("c", "d", 22))); + assertEquals(true, compareAges.test( + new Person("a", "b", 22), + new Person("c", "d", 22))); } // TODO // getFullName: Person -> String - public String getFullName(Person p) { + public String getFullName(final Person p) { return String.format("%s %s", p.getFirstName(), p.getLastName()); } // TODO // ageOfPersonWithTheLongestFullName: (Person -> String) -> (Person, Person) -> Integer - public BiFunction ageOfPersonWithTheLongestFullName(Function function) { + public BiFunction ageOfPersonWithTheLongestFullName( + final Function function) { return (p1, p2) -> getFullName(p1).length() > getFullName(p2).length() ? p1.getAge(): p2.getAge(); } @@ -47,7 +50,8 @@ public void getAgeOfPersonWithTheLongestFullName() { // (Person, Person) -> Integer // TODO use ageOfPersonWithTheLongestFullName(getFullName) - final BiFunction ageOfPersonWithTheLongestFullName = ageOfPersonWithTheLongestFullName(getFullName); + 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 ea636b7..551b0c4 100644 --- a/src/test/java/lambda/part2/exercise/FunctionCombinationExercise.java +++ b/src/test/java/lambda/part2/exercise/FunctionCombinationExercise.java @@ -48,13 +48,13 @@ public void personHasNotEmptyLastNameAndFirstName1() { // TODO // negate: (T -> boolean) -> (T -> boolean) - private Predicate negate(Predicate test) { + private Predicate negate(final Predicate test) { return t -> !test.test(t); } // TODO // and: (T -> boolean, T -> boolean) -> (T -> boolean) - private Predicate and(Predicate t1, Predicate t2) { + private Predicate and(final Predicate t1, final Predicate t2) { return t -> t1.test(t) && t2.test(t); } From f54441917b62a724215f641640705f779d683c69 Mon Sep 17 00:00:00 2001 From: Kirill Date: Wed, 5 Jul 2017 17:22:49 +0300 Subject: [PATCH 3/3] fixes --- .../java/lambda/part2/exercise/ArrowNotationExercise.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/lambda/part2/exercise/ArrowNotationExercise.java b/src/test/java/lambda/part2/exercise/ArrowNotationExercise.java index 0af2bb2..e128fe0 100644 --- a/src/test/java/lambda/part2/exercise/ArrowNotationExercise.java +++ b/src/test/java/lambda/part2/exercise/ArrowNotationExercise.java @@ -40,13 +40,13 @@ public String getFullName(final Person p) { // ageOfPersonWithTheLongestFullName: (Person -> String) -> (Person, Person) -> Integer public BiFunction ageOfPersonWithTheLongestFullName( final Function function) { - return (p1, p2) -> getFullName(p1).length() > getFullName(p2).length() ? p1.getAge(): p2.getAge(); + return (p1, p2) -> getFullName(p1).length() > function.apply(p2).length() ? p1.getAge(): p2.getAge(); } @Test public void getAgeOfPersonWithTheLongestFullName() { // Person -> String - final Function getFullName = null; // TODO + final Function getFullName = this::getFullName; // TODO // (Person, Person) -> Integer // TODO use ageOfPersonWithTheLongestFullName(getFullName)