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
57 changes: 38 additions & 19 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> newList = new ArrayList<>();
list.forEach(
(T t) -> newList.add(f.apply(t))
);
return new MapHelper<R>(newList);
}

// [T] -> (T -> [R]) -> [R]
Expand All @@ -50,6 +53,19 @@ public <R> MapHelper<R> flatMap(Function<T, List<R>> f) {
}
}


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

private static List<JobHistoryEntry> replace(List<JobHistoryEntry> jobHistoryEntries){
return new MapHelper<JobHistoryEntry>(jobHistoryEntries)
.map(e -> e.withPosition(e.getPosition().replace("qa","QA")))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't replace qabalah specialist with QAbalah specialist

.getList();
}

@Test
public void mapping() {
final List<Employee> employees =
Expand All @@ -76,12 +92,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(replace(e.getJobHistory())))
.getList();

final List<Employee> expectedResult =
Arrays.asList(
Expand Down Expand Up @@ -111,21 +125,28 @@ public void mapping() {

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<R>();
list.forEach(e -> result.add(function.apply(e)));
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));
}

}
Expand Down Expand Up @@ -166,7 +187,6 @@ public <R2> LazyFlatMapHelper<T, R2> flatMap(Function<R, List<R2>> f) {
}



@Test
public void lazy_mapping() {
final List<Employee> employees =
Expand All @@ -191,14 +211,13 @@ 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(replace(e.getJobHistory())))
.force();

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