From 2bd629c936711e97acf3f4301cbae577b0a5ef37 Mon Sep 17 00:00:00 2001 From: Nikita Golubev Date: Wed, 5 Jul 2017 16:04:02 +0300 Subject: [PATCH 1/3] [WIP] part2 --- .../part2/exercise/ArrowNotationExercise.java | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/test/java/lambda/part2/exercise/ArrowNotationExercise.java b/src/test/java/lambda/part2/exercise/ArrowNotationExercise.java index c43edc4..102a613 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,31 +23,36 @@ public void getAge() { public void compareAges() { // TODO use BiPredicate // compareAges: (Person, Person) -> boolean + final BiFunction compareAges = (person1, person2) -> person1.getAge() == person2.getAge(); - throw new UnsupportedOperationException("Not implemented"); - //assertEquals(true, compareAges.test(new Person("a", "b", 22), new Person("c", "d", 22))); + assertEquals(true, compareAges.apply(new Person("a", "b", 22), new Person("c", "d", 22))); } // TODO // getFullName: Person -> String + private Function getFullNameFunc(){ + return person -> String.format("%1%s %2$s", person.getFirstName(), person.getLastName()); + } // TODO // ageOfPersonWithTheLongestFullName: (Person -> String) -> (Person, Person) -> int - // - + private BiFunction ageOfPersonWithTheLongestFullNameFunc(Function getFullNameFunc){ + return (person1, person2) -> getFullNameFunc.apply(person1).length() > getFullNameFunc.apply(person2).length()? + person1.getAge():person2.getAge(); + } @Test public void getAgeOfPersonWithTheLongestFullName() { // Person -> String - final Function getFullName = null; // TODO + final Function getFullName = getFullNameFunc(); // TODO // (Person, Person) -> Integer // TODO use ageOfPersonWithTheLongestFullName(getFullName) - final BiFunction ageOfPersonWithTheLongestFullName = null; + final BiFunction ageOfPersonWithTheLongestFullName = + ageOfPersonWithTheLongestFullNameFunc(getFullName); assertEquals( Integer.valueOf(1), - ageOfPersonWithTheLongestFullName.apply( - new Person("a", "b", 2), + ageOfPersonWithTheLongestFullName.apply(new Person("a", "b", 2), new Person("aa", "b", 1))); } } From 1d04f94d93789834595513ca4d2f23056ecb1ecd Mon Sep 17 00:00:00 2001 From: Nikita Golubev Date: Sun, 9 Jul 2017 19:40:11 +0300 Subject: [PATCH 2/3] part2 is done --- .../lambda/part2/exercise/ArrowNotationExercise.java | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/test/java/lambda/part2/exercise/ArrowNotationExercise.java b/src/test/java/lambda/part2/exercise/ArrowNotationExercise.java index 102a613..9f4b41f 100644 --- a/src/test/java/lambda/part2/exercise/ArrowNotationExercise.java +++ b/src/test/java/lambda/part2/exercise/ArrowNotationExercise.java @@ -14,27 +14,24 @@ public class ArrowNotationExercise { @Test public void getAge() { // Person -> Integer - final Function getAge = Person::getAge; // TODO + final Function getAge = Person::getAge; assertEquals(Integer.valueOf(33), getAge.apply(new Person("", "", 33))); } @Test public void compareAges() { - // TODO use BiPredicate // compareAges: (Person, Person) -> boolean final BiFunction compareAges = (person1, person2) -> person1.getAge() == person2.getAge(); assertEquals(true, compareAges.apply(new Person("a", "b", 22), new Person("c", "d", 22))); } - // TODO // getFullName: Person -> String - private Function getFullNameFunc(){ - return person -> String.format("%1%s %2$s", person.getFirstName(), person.getLastName()); + private String getFullNameFunc(Person person){ + return String.format("%1%s %2$s", person.getFirstName(), person.getLastName()); } - // TODO // ageOfPersonWithTheLongestFullName: (Person -> String) -> (Person, Person) -> int private BiFunction ageOfPersonWithTheLongestFullNameFunc(Function getFullNameFunc){ return (person1, person2) -> getFullNameFunc.apply(person1).length() > getFullNameFunc.apply(person2).length()? @@ -43,10 +40,9 @@ private BiFunction ageOfPersonWithTheLongestFullNameFun @Test public void getAgeOfPersonWithTheLongestFullName() { // Person -> String - final Function getFullName = getFullNameFunc(); // TODO + final Function getFullName = this::getFullNameFunc; // (Person, Person) -> Integer - // TODO use ageOfPersonWithTheLongestFullName(getFullName) final BiFunction ageOfPersonWithTheLongestFullName = ageOfPersonWithTheLongestFullNameFunc(getFullName); From 31e3b40954f769bb8a420a71c77a3dbec82a47f5 Mon Sep 17 00:00:00 2001 From: Nikita Golubev Date: Tue, 11 Jul 2017 09:39:56 +0300 Subject: [PATCH 3/3] part2 --- .../exercise/FunctionCombinationExercise.java | 32 ++++++------------- 1 file changed, 10 insertions(+), 22 deletions(-) diff --git a/src/test/java/lambda/part2/exercise/FunctionCombinationExercise.java b/src/test/java/lambda/part2/exercise/FunctionCombinationExercise.java index ce7080f..ae6ac78 100644 --- a/src/test/java/lambda/part2/exercise/FunctionCombinationExercise.java +++ b/src/test/java/lambda/part2/exercise/FunctionCombinationExercise.java @@ -19,22 +19,14 @@ public void personHasNotEmptyLastNameAndFirstName0() { assertEquals(false, validate.test(new Person("a", "", 0))); } - // 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 +44,14 @@ 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 p -> !test.test(p); } - // TODO // and: (T -> boolean, T -> boolean) -> (T -> boolean) private Predicate and(Predicate t1, Predicate t2) { - // TODO - throw new UnsupportedOperationException(); + return p -> t1.test(p) && t2.test(p); } @Test @@ -71,10 +59,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); + final Predicate validateLastName = negate(hasEmptyLastName); - final Predicate validate = null; // TODO use and + final Predicate validate = and(validateFirstName, validateLastName); assertEquals(true, validate.test(new Person("a", "b", 0))); assertEquals(false, validate.test(new Person("", "b", 0))); @@ -86,10 +74,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(); + final Predicate validateLastName = hasEmptyLastName.negate(); - final Predicate validate = null; // TODO use Predicate::and + final Predicate validate = validateFirstName.and(validateLastName); assertEquals(true, validate.test(new Person("a", "b", 0))); assertEquals(false, validate.test(new Person("", "b", 0)));