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
3 changes: 1 addition & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ dependencies {
compile 'com.google.guava:guava:+',
'org.apache.commons:commons-lang3:+',
'ch.qos.logback:logback-classic:+',
'io.javaslang:javaslang:+',
'com.github.Vyacheslav-Lapin:Hegel:master-SNAPSHOT' //https://github.com/Vyacheslav-Lapin/Hegel.git
'io.javaslang:javaslang:+'

compileOnly 'org.projectlombok:lombok:+',
'edu.washington.cs.types.checker:checker-framework:+'
Expand Down
23 changes: 21 additions & 2 deletions src/test/java/lambda/part1/exercise/Lambdas01Exercise.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
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.junit.jupiter.api.Assertions.assertArrayEquals;
Expand All @@ -19,7 +24,12 @@ 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) {
return o1.getAge() - o2.getAge();
}
});

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

Person person = null;

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

if (personOptional.isPresent())
person = personOptional.get();

assertEquals(person, new Person("name 1", "lastName 2", 30));
}
Expand Down
10 changes: 8 additions & 2 deletions src/test/java/lambda/part1/exercise/Lambdas02Exercise.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import data.Person;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

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

// TODO use Arrays.sort
Arrays.sort(persons, (o1, o2) -> o1.getAge() - o2.getAge());

assertArrayEquals(persons, new Person[]{
new Person("name 3", "lastName 3", 20),
Expand All @@ -37,7 +39,11 @@ void findFirstWithAge30() {

Person person = null;

// TODO use FluentIterable
final Optional<Person> personOptional =
persons.stream().filter(p -> p.getAge() == 30).findFirst();

if (personOptional.isPresent())
person = personOptional.get();

assertEquals(person, new Person("name 1", "lastName 2", 30));
}
Expand Down
29 changes: 21 additions & 8 deletions src/test/java/lambda/part3/exercise/Mapping.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import static org.junit.jupiter.api.Assertions.assertEquals;


class Mapping {

private static class MapHelper<T> {
Expand All @@ -29,8 +30,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<R>();
list.forEach((T t) ->
result.add(f.apply(t)));

return new MapHelper<R>(result);
}

// [T] -> (T -> [R]) -> [R]
Expand Down Expand Up @@ -73,12 +77,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(addOneYear(employee.getJobHistory())))
.map(employee -> employee.withJobHistory(replaseQA(employee.getJobHistory())))
.getList();

final List<Employee> expectedResult =
Arrays.asList(
Expand All @@ -105,6 +107,17 @@ void mapping() {
assertEquals(mappedEmployees, expectedResult);
}

private List<JobHistoryEntry> replaseQA(List<JobHistoryEntry> jobHistory) {
MapHelper<JobHistoryEntry> jhem = new MapHelper<>(jobHistory);
return jhem.map(entry -> entry.withPosition(entry.getPosition()
.replace("qa", "QA"))).getList();
}

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


private static class LazyMapHelper<T, R> {

Expand Down