-
Notifications
You must be signed in to change notification settings - Fork 66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Exercise from second lesson (by Narek Karapetian) #166
Open
NarekDW
wants to merge
3
commits into
java8-course:master
Choose a base branch
from
NarekDW:Part_2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
108 changes: 55 additions & 53 deletions
108
src/test/java/lambda/part2/exercise/ArrowNotationExercise.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,55 @@ | ||
package lambda.part2.exercise; | ||
|
||
import data.Person; | ||
import org.junit.Test; | ||
|
||
import java.util.function.BiFunction; | ||
import java.util.function.BiPredicate; | ||
import java.util.function.Function; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class ArrowNotationExercise { | ||
|
||
@Test | ||
public void getAge() { | ||
// Person -> Integer | ||
final Function<Person, Integer> getAge = null; // TODO | ||
|
||
assertEquals(Integer.valueOf(33), getAge.apply(new Person("", "", 33))); | ||
} | ||
|
||
@Test | ||
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))); | ||
} | ||
|
||
// TODO | ||
// getFullName: Person -> String | ||
|
||
// TODO | ||
// ageOfPersonWithTheLongestFullName: (Person -> String) -> (Person, Person) -> int | ||
// | ||
|
||
@Test | ||
public void getAgeOfPersonWithTheLongestFullName() { | ||
// Person -> String | ||
final Function<Person, String> getFullName = null; // TODO | ||
|
||
// (Person, Person) -> Integer | ||
// TODO use ageOfPersonWithTheLongestFullName(getFullName) | ||
final BiFunction<Person, Person, Integer> ageOfPersonWithTheLongestFullName = null; | ||
|
||
assertEquals( | ||
Integer.valueOf(1), | ||
ageOfPersonWithTheLongestFullName.apply( | ||
new Person("a", "b", 2), | ||
new Person("aa", "b", 1))); | ||
} | ||
} | ||
package lambda.part2.exercise; | ||
|
||
import data.Person; | ||
import org.junit.Test; | ||
|
||
import java.util.function.BiPredicate; | ||
import java.util.function.Function; | ||
import java.util.function.ToIntBiFunction; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class ArrowNotationExercise { | ||
|
||
@Test | ||
public void getAge() { | ||
// Person -> Integer | ||
final Function<Person, Integer> getAge = Person::getAge; | ||
|
||
assertEquals(Integer.valueOf(33), getAge.apply(new Person("", "", 33))); | ||
} | ||
|
||
@Test | ||
public void compareAges() { | ||
// compareAges: (Person, Person) -> boolean | ||
BiPredicate<Person, Person> compareAges = (person, person2) -> person.getAge() == person2.getAge(); | ||
|
||
assertEquals(true, compareAges.test(new Person("a", "b", 22), new Person("c", "d", 22))); | ||
} | ||
|
||
// getFullName: Person -> String | ||
public String getFullName(Person p) { | ||
return String.format("%s %s", p.getFirstName(), p.getLastName()); | ||
} | ||
|
||
// ageOfPersonWithTheLongestFullName: (Person -> String) -> (Person, Person) -> int | ||
public ToIntBiFunction<Person, Person> ageOfPersonWithTheLongestFullName(Function<Person, String> fullName) { | ||
return (p1, p2) -> fullName.apply(p1).length() > fullName.apply(p2).length() ? p1.getAge() : p2.getAge(); | ||
} | ||
|
||
@Test | ||
public void getAgeOfPersonWithTheLongestFullName() { | ||
// Person -> String | ||
final Function<Person, String> getFullName = this::getFullName; | ||
|
||
// (Person, Person) -> Integer | ||
final ToIntBiFunction<Person, Person> ageOfPersonWithTheLongestFullName = | ||
ageOfPersonWithTheLongestFullName(getFullName); | ||
|
||
assertEquals( | ||
1, | ||
ageOfPersonWithTheLongestFullName.applyAsInt( | ||
new Person("a", "b", 2), | ||
new Person("aa", "b", 1))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This diff is unreadable. I suspect you've changed line endings.
Fix it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.