Skip to content
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

list-ops: add to track #207

Merged
merged 13 commits into from
Feb 21, 2017
Empty file added .keep
Empty file.
8 changes: 7 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@
"wordy",
"palindrome-products",
"twelve-days",
"sublist"
"sublist",
"list-ops"
],
"exercises": [
{
Expand Down Expand Up @@ -317,6 +318,11 @@
"slug": "sublist",
"difficulty": 1,
"topics": []
},
{
"slug": "list-ops",
"difficulty": 1,
"topics": []
}
],
"deprecated": [
Expand Down
18 changes: 18 additions & 0 deletions exercises/list-ops/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
apply plugin: "java"
apply plugin: "eclipse"
apply plugin: "idea"

repositories {
mavenCentral()
}

dependencies {
testCompile "junit:junit:4.12"
}

test {
testLogging {
exceptionFormat = 'full'
events = ["passed", "failed", "skipped"]
}
}
56 changes: 56 additions & 0 deletions exercises/list-ops/src/example/java/ListOps.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.BinaryOperator;
import java.util.function.Predicate;
import java.util.function.UnaryOperator;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class ListOps {

private ListOps() {
}

public static <T> int length(final List<T> list) {
return list.size();
}

public static <T> List<T> reverse(final List<T> list) {
List<T> result = new ArrayList(list);
Collections.reverse(result);
return result;
}

public static <T> List<T> map(final List<T> list,
UnaryOperator<T> mapper) {
return list.stream().map(mapper).collect(Collectors.toList());
Copy link
Contributor

Choose a reason for hiding this comment

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

hahaha!! Cheater! 😆

👍
I joke, but actually, relying on the JSL for this functionality gives me great confidence that the tests are solid. Not sure if that was your intention, but great move!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Tried to keep it simple. The example is needed only to test the test. No pun intended. :)

}

public static <T> List<T> filter(final List<T> list,
Predicate<T> predicate) {
return list.stream().filter(predicate).collect(Collectors.toList());
}

public static <U, T> U reduce(final List<T> list,
Copy link
Contributor

Choose a reason for hiding this comment

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

👍
I love how this mirrors exactly the interface for streams... what a great way to introduce this part of the API!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you. It makes sense to expose Java API here like the C# one was used in the C# track

U identity,
BiFunction<U, T, U> accumulator,
BinaryOperator<U> combiner) {
return list.stream().reduce(identity, accumulator, combiner);
}

public static <T> boolean append(final List<T> listTo,
List<T> listFrom) {
return listTo.addAll(listFrom);
}

public static <T> List<T> concat(final List<T>... lists) {
return Stream.of(lists)
.flatMap(Collection::stream)
.collect(Collectors.toList());
}

}
Empty file.
Loading