Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions src/test/java/lambda/part2/exercise/ArrowNotationExercise.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -14,35 +16,36 @@ public class ArrowNotationExercise {
@Test
public void getAge() {
// Person -> Integer
final Function<Person, Integer> getAge = null; // TODO

final Function<Person, Integer> getAge = Person::getAge;
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)));
final BiPredicate<Person, Person> 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<Person, String> getFullName = person -> person.getFirstName() + person.getLastName();
final BiPredicate<Person, Person> compareNamesLength = (p1, p2) -> p1.getAge() == p2.getAge();

// TODO
// ageOfPersonWithTheLongestFullName: (Person -> String) -> (Person, Person) -> int
//
final BiFunction<Person, Person, Integer> ageOfPersonWithTheLongestFullName(Function<Person, String> 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<Person, String> getFullName = null; // TODO
final Function<Person, String> getFullName = person -> person.getFirstName() + person.getLastName();

// (Person, Person) -> Integer
// TODO use ageOfPersonWithTheLongestFullName(getFullName)
final BiFunction<Person, Person, Integer> ageOfPersonWithTheLongestFullName = null;
final BiFunction<Person, Person, Integer> ageOfPersonWithTheLongestFullName = ageOfPersonWithTheLongestFullName(getFullName);

assertEquals(
Integer.valueOf(1),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,16 @@ public void personHasNotEmptyLastNameAndFirstName0() {
// TODO
// negate1: (Person -> boolean) -> (Person -> boolean)
private Predicate<Person> negate1(Predicate<Person> test) {
return p -> {
// TODO
throw new UnsupportedOperationException();
};
return p -> !(test.test(p));

}

// TODO

// validateFirstNameAndLastName: (Person -> boolean, Person -> boolean) -> (Person -> boolean)
private Predicate<Person> validateFirstNameAndLastName(Predicate<Person> t1, Predicate<Person> t2) {
return p -> {
// TODO
throw new UnsupportedOperationException();
};
return p ->
t1.test(p)&& t2.test(p);

}

@Test
Expand All @@ -52,29 +49,27 @@ public void personHasNotEmptyLastNameAndFirstName1() {
assertEquals(false, validate.test(new Person("a", "", 0)));
}

// TODO
// negate: (T -> boolean) -> (T -> boolean)
private <T> Predicate<T> negate(Predicate<T> test) {
// TODO
throw new UnsupportedOperationException();
return t -> !test.test(t);
//throw new UnsupportedOperationException();
}

// TODO
// and: (T -> boolean, T -> boolean) -> (T -> boolean)
private <T> Predicate<T> and(Predicate<T> t1, Predicate<T> t2) {
// TODO
throw new UnsupportedOperationException();
return t -> t1.test(t) && t2.test(t);
// throw new UnsupportedOperationException();
}

@Test
public void personHasNotEmptyLastNameAndFirstName2() {
final Predicate<Person> hasEmptyFirstName = p -> p.getFirstName().isEmpty();
final Predicate<Person> hasEmptyLastName = p -> p.getLastName().isEmpty();

final Predicate<Person> validateFirstName = null; // TODO use negate
final Predicate<Person> validateLastName = null; // TODO use negate
final Predicate<Person> validateFirstName = negate(hasEmptyFirstName); // TODO use negate
final Predicate<Person> validateLastName = negate(hasEmptyLastName); // TODO use negate

final Predicate<Person> validate = null; // TODO use and
final Predicate<Person> validate = and(validateFirstName,validateLastName); // TODO use and

assertEquals(true, validate.test(new Person("a", "b", 0)));
assertEquals(false, validate.test(new Person("", "b", 0)));
Expand All @@ -86,10 +81,10 @@ public void personHasNotEmptyLastNameAndFirstName3() {
final Predicate<Person> hasEmptyFirstName = p -> p.getFirstName().isEmpty();
final Predicate<Person> hasEmptyLastName = p -> p.getLastName().isEmpty();

final Predicate<Person> validateFirstName = null; // TODO use Predicate::negate
final Predicate<Person> validateLastName = null; // TODO use Predicate::negate
final Predicate<Person> validateFirstName = hasEmptyFirstName.negate(); // TODO use Predicate::negate
final Predicate<Person> validateLastName = hasEmptyLastName.negate(); // TODO use Predicate::negate

final Predicate<Person> validate = null; // TODO use Predicate::and
final Predicate<Person> 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)));
Expand Down
61 changes: 52 additions & 9 deletions src/test/java/lambda/part3/exercise/Mapping.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,15 @@ public List<T> getList() {
// [T] -> (T -> R) -> [R]
// [T1, T2, T3] -> (T -> R) -> [R1, R2, R3]
public <R> MapHelper<R> map(Function<T, R> f) {
final List<R> result = new ArrayList<R>();
list.forEach(t -> result.add(f.apply(t)));
return new MapHelper<R>(result);
// TODO
throw new UnsupportedOperationException();
// throw new UnsupportedOperationException();
}

;

// [T] -> (T -> [R]) -> [R]

// map: [T, T, T], T -> [R] => [[], [R1, R2], [R3, R4, R5]]
Expand Down Expand Up @@ -76,12 +81,15 @@ public void mapping() {

final List<Employee> 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<Employee> expectedResult =
Arrays.asList(
Expand All @@ -108,10 +116,27 @@ public void mapping() {
assertEquals(mappedEmployees, expectedResult);
}

private List<JobHistoryEntry> replaceQaWithQA(List<JobHistoryEntry> jobHistory) {
return new MapHelper<JobHistoryEntry>(jobHistory).map(jobHistoryEntry -> {
String position = jobHistoryEntry.getPosition();
return jobHistoryEntry.withPosition(position.equals("qa") ? "QA" : position);
}).getList();
}

private List<JobHistoryEntry> addOneYear(List<JobHistoryEntry> jobHistory) {
return new MapHelper<JobHistoryEntry>(jobHistory).map(jobHistoryEntry ->
jobHistoryEntry.withDuration(jobHistoryEntry.getDuration() + 1)
).getList();
}


private static class LazyMapHelper<T, R> {
private final List<T> list;
private final Function<T, R> function;

public LazyMapHelper(List<T> list, Function<T, R> function) {
this.list = list;
this.function = function;
}

public static <T> LazyMapHelper<T, T> from(List<T> list) {
Expand All @@ -120,19 +145,25 @@ public static <T> LazyMapHelper<T, T> from(List<T> list) {

public List<R> force() {
// TODO
throw new UnsupportedOperationException();
List<R> result = new ArrayList<R>(list.size());
list.forEach(item -> result.add(function.apply(item)));
return result;
}

public <R2> LazyMapHelper<T, R2> map(Function<R, R2> f) {
// TODO
throw new UnsupportedOperationException();
return new LazyMapHelper<T, R2>(list, function.andThen(f));
}

}

private static class LazyFlatMapHelper<T, R> {
private final List<T> list;
private final Function<T, List<R>> function;

public LazyFlatMapHelper(List<T> list, Function<T, List<R>> function) {
this.list = list;
this.function = function;
}

public static <T> LazyFlatMapHelper<T, T> from(List<T> list) {
Expand All @@ -141,13 +172,23 @@ public static <T> LazyFlatMapHelper<T, T> from(List<T> list) {

public List<R> force() {
// TODO
throw new UnsupportedOperationException();
List<R> result = new ArrayList<R>(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<T, R> filter(Predicate<R> p) {
final Function<R, List<R>> listFunction = rTorListR(p);
return flatMap(listFunction);
}

private Function<R, List<R>> rTorListR(Predicate<R> p) {
return r -> p.test(r) ? Collections.singletonList(r) : Collections.emptyList();
}

public <R2> LazyFlatMapHelper<T, R2> map(Function<R, R2> f) {
final Function<R, List<R2>> listFunction = rR2TorListR2(f);
Expand All @@ -156,7 +197,7 @@ public <R2> LazyFlatMapHelper<T, R2> map(Function<R, R2> f) {

// (R -> R2) -> (R -> [R2])
private <R2> Function<R, List<R2>> rR2TorListR2(Function<R, R2> f) {
throw new UnsupportedOperationException();
return f.andThen(Arrays::asList);
}

// TODO *
Expand All @@ -166,7 +207,6 @@ public <R2> LazyFlatMapHelper<T, R2> flatMap(Function<R, List<R2>> f) {
}



@Test
public void lazy_mapping() {
final List<Employee> employees =
Expand All @@ -193,13 +233,16 @@ public void lazy_mapping() {

final List<Employee> 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<Employee> expectedResult =
Arrays.asList(
new Employee(
Expand Down