Skip to content

homework_part3_Komarova #182

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

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
52 changes: 38 additions & 14 deletions src/test/java/lambda/part3/exercise/Mapping.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,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> result = new ArrayList<>();
for (T element : list) {
result.add(f.apply(element));
}
return new MapHelper<>(result);
}

// [T] -> (T -> [R]) -> [R]
Expand Down Expand Up @@ -76,11 +79,9 @@ 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
* */
.map(e -> e.withPerson(e.getPerson().withFirstName("John")))
.map(e -> e.withJobHistory(addOneYear(e.getJobHistory())))
.map(e -> e.withJobHistory(replaceQA(e.getJobHistory())))
.getList();

final List<Employee> expectedResult =
Expand Down Expand Up @@ -108,26 +109,48 @@ public void mapping() {
assertEquals(mappedEmployees, expectedResult);
}

private List<JobHistoryEntry> replaceQA(List<JobHistoryEntry> jobHistory) {
return new MapHelper<>(jobHistory)
.map(j -> j.getPosition().equals("qa")
? j.withPosition("QA")
: j.withPosition(j.getPosition()))
.getList();
}

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


private static class LazyMapHelper<T, R> {
private List<T> list;
private 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();
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();
}

return new LazyMapHelper<>(list, (function.andThen(f)));

}
}

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



@Test
public void lazy_mapping() {
final List<Employee> employees =
Expand All @@ -193,6 +214,9 @@ public void lazy_mapping() {

final List<Employee> mappedEmployees =
LazyMapHelper.from(employees)
.map(e -> e.withPerson(e.getPerson().withFirstName("John")))
.map(e->e.withJobHistory(addOneYear(e.getJobHistory())))
.map(e ->e.withJobHistory(replaceQA(e.getJobHistory())))
/*
.map(TODO) // change name to John
.map(TODO) // add 1 year to experience duration
Expand Down