Skip to content
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
68 changes: 44 additions & 24 deletions src/test/java/lambda/part3/exercise/Mapping.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
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;
Expand All @@ -32,8 +31,10 @@ 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<R>();
list.forEach((T t) -> result.add(f.apply(t)));

return new MapHelper<R>(result);
}

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

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

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

private List<JobHistoryEntry> addOneYear(List<JobHistoryEntry> jobHistory) {
return new MapHelper<>(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) {
return new LazyMapHelper<>(list, Function.identity());
}

public List<R> force() {
// TODO
throw new UnsupportedOperationException();
return new MapHelper<>(list).map(function).getList();
}

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 List<T> list;
private Function<T,R> function;

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

public static <T> LazyFlatMapHelper<T, T> from(List<T> list) {
throw new UnsupportedOperationException();
return new LazyFlatMapHelper<T, T>(list, Function.identity());
}

public List<R> force() {
// TODO
throw new UnsupportedOperationException();
return new MapHelper<>(list).map(function).getList();
}

public LazyFlatMapHelper<T, R> filter(Predicate<R> p) {
return flatMap(r -> p.test(r) ? Collections.singletonList(r) : Collections.emptyList());
}

// TODO filter
// (T -> boolean) -> (T -> [T])
// filter: [T1, T2] -> (T -> boolean) -> [T2]
// flatMap": [T1, T2] -> (T -> [T]) -> [T2]

public <R2> LazyFlatMapHelper<T, R2> map(Function<R, R2> f) {
final Function<R, List<R2>> listFunction = rR2TorListR2(f);
return flatMap(listFunction);
Expand All @@ -165,7 +185,9 @@ public <R2> LazyFlatMapHelper<T, R2> flatMap(Function<R, List<R2>> f) {
}
}


interface Traversable<T> {
void forEach(Consumer<T> consumer);
}

@Test
public void lazy_mapping() {
Expand Down Expand Up @@ -193,11 +215,9 @@ 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
* */
.map(employee -> employee.withPerson(employee.getPerson().withFirstName("John")))
.map(employee -> employee.withJobHistory(addOneYear(employee.getJobHistory())))
.map(employee -> employee.withJobHistory(replaceQA(employee.getJobHistory())))
.force();

final List<Employee> expectedResult =
Expand Down