Skip to content
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

Part 3 (by Vasilii Bobkov) #183

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
60 changes: 37 additions & 23 deletions src/test/java/lambda/part3/exercise/Mapping.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;

import static org.junit.Assert.assertEquals;

Expand All @@ -32,8 +29,11 @@ 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) {
// TODO
throw new UnsupportedOperationException();
final List<R> newList = new ArrayList<>();
for (T element : list) {
newList.add(f.apply(element));
}
return new MapHelper<>(newList);
}

// [T] -> (T -> [R]) -> [R]
Expand Down Expand Up @@ -76,12 +76,10 @@ public void mapping() {

final List<Employee> mappedEmployees =
new MapHelper<>(employees)
/*
.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();
.map(e -> e.withPerson(e.getPerson().withFirstName("John")))
.map(e -> e.withJobHistory(addOneYear(e.getJobHistory())))
.map(e -> e.withJobHistory(replaceQaToUpperCase(e.getJobHistory()))) // replace qa with QA
.getList();

final List<Employee> expectedResult =
Arrays.asList(
Expand All @@ -108,24 +106,44 @@ public void mapping() {
assertEquals(mappedEmployees, expectedResult);
}

private List<JobHistoryEntry> addOneYear(List<JobHistoryEntry> jobHistoryEntries) {
MapHelper<JobHistoryEntry> mapHelper = new MapHelper<>(jobHistoryEntries);
return mapHelper
.map(entry -> entry.withDuration(entry.getDuration() + 1))
.getList();
}

private List<JobHistoryEntry> replaceQaToUpperCase(List<JobHistoryEntry> jobHistoryEntries) {
MapHelper<JobHistoryEntry> mapHelper = new MapHelper<>(jobHistoryEntries);
return mapHelper
.map(entry -> entry.getPosition().equals("qa") ? entry.withPosition("QA") : entry)
.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) {
return new LazyMapHelper<>(list, Function.identity());
}

public List<R> force() {
// TODO
throw new UnsupportedOperationException();
final List<R> result = new ArrayList<>();
for (T element : list) {
result.add(function.apply(element));
}
return result;
}

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

}
Expand Down Expand Up @@ -165,8 +183,6 @@ public <R2> LazyFlatMapHelper<T, R2> flatMap(Function<R, List<R2>> f) {
}
}



@Test
public void lazy_mapping() {
final List<Employee> employees =
Expand All @@ -193,12 +209,10 @@ public void lazy_mapping() {

final List<Employee> mappedEmployees =
LazyMapHelper.from(employees)
/*
.map(TODO) // change name to John
.map(TODO) // add 1 year to experience duration
.map(TODO) // replace qa with QA
* */
.force();
.map(e -> e.withPerson(e.getPerson().withFirstName("John")))
.map(e -> e.withJobHistory(addOneYear(e.getJobHistory())))
.map(e -> e.withJobHistory(replaceQaToUpperCase(e.getJobHistory())))
.force();

final List<Employee> expectedResult =
Arrays.asList(
Expand Down