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
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ dependencies {
compileOnly 'org.projectlombok:lombok:+',
'edu.washington.cs.types.checker:checker-framework:+'

testCompile 'org.junit.jupiter:junit-jupiter-api:+',
testCompile 'org.junit.jupiter:junit-jupiter-api:5.0.0-M2',
'org.hamcrest:java-hamcrest:+'
}
5 changes: 3 additions & 2 deletions src/test/java/lambda/part1/example/Lambdas01.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.google.common.collect.ImmutableList;
import data.Person;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import java.util.Arrays;
import java.util.Comparator;
Expand All @@ -19,8 +20,8 @@
// JSR-335 Lambda Expressions for the Java Programming Language

// https://github.com/Vyacheslav-Lapin/lambda

class Lambdas01 {
//@ExtendWith()
class Lambdas01{

@Test
void sortPersons() {
Expand Down
39 changes: 36 additions & 3 deletions src/test/java/lambda/part1/exercise/Lambdas01Exercise.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
package lambda.part1.exercise;

import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import data.Person;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;

class Lambdas01Exercise {

Expand All @@ -19,7 +27,20 @@ void sortPersonsByAge() {
new Person("name 2", "lastName 1", 30)
};

// TODO use Arrays.sort
Arrays.sort(persons, new Comparator<Person>() {
@Override
public int compare(Person o1, Person o2) {
if(o1.getAge()>o2.getAge()){
return 1;
}
else if(o1.getAge() < o2.getAge()){
return -1;
}
else {
return 0;
}
}
});

assertArrayEquals(persons, new Person[]{
new Person("name 3", "lastName 3", 20),
Expand All @@ -38,8 +59,20 @@ void findFirstWithAge30() {

Person person = null;

// TODO use FluentIterable
final Optional<Person> optional = FluentIterable.from(persons)
.firstMatch(new Predicate<Person>() {
@Override
public boolean apply(Person p) {
return p.getAge() == 30;
}
});

assertEquals(person, new Person("name 1", "lastName 2", 30));
// assertEquals(person, new Person("name 1", "lastName 2", 30));
if(optional.isPresent()){
assertThat(optional.get(),is(new Person("name 1", "lastName 2", 30)));
}
else {
fail("");
}
}
}
15 changes: 12 additions & 3 deletions src/test/java/lambda/part1/exercise/Lambdas02Exercise.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package lambda.part1.exercise;

import com.google.common.base.Optional;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import data.Person;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
Expand All @@ -18,7 +22,7 @@ void sortPersonsByAge() {
new Person("name 2", "lastName 1", 30)
};

// TODO use Arrays.sort
Arrays.sort(persons, Comparator.comparing(p -> p.getAge()));

assertArrayEquals(persons, new Person[]{
new Person("name 3", "lastName 3", 20),
Expand All @@ -37,8 +41,13 @@ void findFirstWithAge30() {

Person person = null;

// TODO use FluentIterable
Optional<Person> optional = FluentIterable.from(persons)
.firstMatch(p -> p.getAge() == 30);

assertEquals(person, new Person("name 1", "lastName 2", 30));
if(optional.isPresent()){
optional.get().print();
}

assertEquals(optional.get(), new Person("name 1", "lastName 2", 30));
}
}
22 changes: 12 additions & 10 deletions src/test/java/lambda/part2/exercise/ArrowNotationExercise.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.junit.jupiter.api.Test;

import java.util.function.BiFunction;
import java.util.function.BiPredicate;
import java.util.function.Function;

import static org.junit.jupiter.api.Assertions.assertEquals;
Expand All @@ -13,35 +14,36 @@ class ArrowNotationExercise {
@Test
void getAge() {
// Person -> Integer
final Function<Person, Integer> getAge = null; // TODO
final Function<Person, Integer> getAge = p -> p.getAge();

assertEquals(Integer.valueOf(33), getAge.apply(new Person("", "", 33)));
}

@Test
void compareAges() {
// TODO use BiPredicate
final BiPredicate<Person, Person> compareAges = (p1, p2) -> p1.getAge() == p2.getAge();
// compareAges: (Person, Person) -> boolean

throw new UnsupportedOperationException("Not implemented");
//assertEquals(true, compareAges.test(new Person("a", "b", 22), new Person("c", "d", 22)));
assertEquals(true, compareAges.test(new Person("a", "b", 22), new Person("c", "d", 22)));
}

// TODO
// getFullName: Person -> String
private static final Function<Person,String> getFullName = person ->
person.getFirstName() + " " + person.getLastName();

// TODO
// ageOfPersonWithTheLongestFullName: (Person -> String) -> (Person, Person) -> int
//
private BiFunction<Person, Person, Integer> ageOfPersonWithTheLongestFullName(Function<Person,String> f){
return (p1, p2) -> f.apply(p1).length() > f.apply(p2).length() ? p1.getAge() : p2.getAge();
}

@Test
void getAgeOfPersonWithTheLongestFullName() {
// Person -> String
final Function<Person, String> getFullName = null; // TODO
final Function<Person, String> getFullName = ArrowNotationExercise.getFullName;

// (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
55 changes: 35 additions & 20 deletions src/test/java/lambda/part3/exercise/Mapping.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@ 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> res = new ArrayList<>();
for (T t : list) {
res.add(f.apply(t));
}

return new MapHelper<R>(res);
}

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

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

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

@Test
void mapping() {
final List<Employee> employees =
Expand All @@ -73,12 +86,10 @@ 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(employee -> employee.withPerson(employee.getPerson().withFirstName("John")))
.map(employee -> employee.withJobHistory(ageOneYear(employee.getJobHistory())))
.map(employee -> employee.withJobHistory(replaceQa(employee.getJobHistory())))
.getList();

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

private static class LazyMapHelper<T, R> {

private final List<T> list;
private final Function<T, R> function;

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

static <T> LazyMapHelper<T, T> from(List<T> list) {
return new LazyMapHelper<>(list, Function.identity());
}

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

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

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



@Test
void lazy_mapping() {
final List<Employee> employees =
Expand All @@ -190,12 +207,10 @@ 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(ageOneYear(e.getJobHistory())))
.map(e -> e.withJobHistory(replaceQa(e.getJobHistory())))
.force();

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