-
Notifications
You must be signed in to change notification settings - Fork 14
Java05. ДЗ 04, Кравцун Андрей #66
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
Open
kravtsun
wants to merge
6
commits into
java-course-au:04-functional-java
Choose a base branch
from
kravtsun:04-functional-java
base: 04-functional-java
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a676876
Homework solved.
07081f4
Unsuppress warnings on checked conversion (static ALWAYS_TRUE and ALW…
1e86bda
Removing test on private constructor for Collections.
a153e56
Fixed warnings on previous commit's unused imports left.
e29c717
Some fixes from pull request notes.
19e3471
Final changes.
kravtsun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| package ru.spbau.mit; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public final class Collections { | ||
| private Collections() {} | ||
|
|
||
| public static <T, S> List<S> map(Function1<? super T, S> f, Iterable<T> c) { | ||
| List<S> newcol = new ArrayList<>(); | ||
| for (T el : c) { | ||
| newcol.add(f.apply(el)); | ||
| } | ||
| return newcol; | ||
| } | ||
|
|
||
| public static <T> List<T> filter(Predicate<? super T> pred, Iterable<T> c) { | ||
| List<T> newcol = new ArrayList<>(); | ||
| for (T el : c) { | ||
| if (pred.apply(el)) { | ||
| newcol.add(el); | ||
| } | ||
| } | ||
| return newcol; | ||
| } | ||
|
|
||
| public static <T> List<T> takeWhile(Predicate<? super T> pred, Iterable<T> c) { | ||
| List<T> newcol = new ArrayList<>(); | ||
| for (T el : c) { | ||
| if (!pred.apply(el)) { | ||
| break; | ||
| } | ||
| newcol.add(el); | ||
| } | ||
| return newcol; | ||
| } | ||
|
|
||
| public static <T> List<T> takeUnless(Predicate<? super T> pred, Iterable<T> c) { | ||
| return takeWhile(pred.not(), c); | ||
| } | ||
|
|
||
|
|
||
| public static <I, T> I foldl(Function2<I, ? super T, I> f, Iterable<T> c, I init) { | ||
| I res = init; | ||
| for (T el : c) { | ||
| res = f.apply2(res, el); | ||
| } | ||
| return res; | ||
| } | ||
|
|
||
| public static <I, T> I foldr(Function2<? super T, I, I> f, Iterable<T> c, I init) { | ||
| Function1<I, I> resfunc = new Function1<I, I>() { | ||
| @Override | ||
| public I apply(I arg) { | ||
| return arg; | ||
| } | ||
| }; | ||
|
|
||
| for (T el : c) { | ||
| resfunc = f.bind1(el).compose(resfunc); | ||
| } | ||
| return resfunc.apply(init); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package ru.spbau.mit; | ||
|
|
||
| public abstract class Function1<T, S> { | ||
| public <G> Function1<T, G> compose(final Function1<? super S, G> g) { | ||
| return new Function1<T, G>() { | ||
| @Override | ||
| public G apply(T arg) { | ||
| return g.apply(Function1.this.apply(arg)); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| public abstract S apply(T arg); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| package ru.spbau.mit; | ||
|
|
||
| public abstract class Function2<T1, T2, S> { | ||
| public Function1<T1, Function1<T2, S>> curry() { | ||
| return new Function1<T1, Function1<T2, S>>() { | ||
| @Override | ||
| public Function1<T2, S> apply(T1 arg1) { | ||
| return Function2.this.bind1(arg1); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| // because of compose can't extend from Function1 | ||
| public <S2> Function2<T1, T2, S2> compose(final Function1<? super S, S2> g) { | ||
| return new Function2<T1, T2, S2>() { | ||
| public S2 apply2(T1 arg1, T2 arg2) { | ||
| return g.apply(Function2.this.apply2(arg1, arg2)); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| public Function1<T2, S> bind1(final T1 arg1) { | ||
| return new Function1<T2, S>() { | ||
| @Override | ||
| public S apply(T2 arg2) { | ||
| return apply2(arg1, arg2); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| public Function1<T1, S> bind2(final T2 arg2) { | ||
| return new Function1<T1, S>() { | ||
| @Override | ||
| public S apply(T1 arg1) { | ||
| return apply2(arg1, arg2); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| public abstract S apply2(T1 arg1, T2 arg2); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package ru.spbau.mit; | ||
|
|
||
| public abstract class Predicate<T> extends Function1<T, Boolean> { | ||
| public static final Predicate<Object> ALWAYS_TRUE = new Predicate<Object>() { | ||
| @Override | ||
| public Boolean apply(Object arg) { | ||
| return true; | ||
| } | ||
| }; | ||
|
|
||
| public static final Predicate<Object> ALWAYS_FALSE = new Predicate<Object>() { | ||
| @Override | ||
| public Boolean apply(Object arg) { | ||
| return false; | ||
| } | ||
| }; | ||
|
|
||
| public Predicate<T> and(final Predicate<? super T> rhs) { | ||
| return new Predicate<T>() { | ||
| @Override | ||
| public Boolean apply(T arg) { | ||
| return Predicate.this.apply(arg) && rhs.apply(arg); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| public Predicate<T> or(final Predicate<? super T> rhs) { | ||
| return new Predicate<T>() { | ||
| @Override | ||
| public Boolean apply(T arg) { | ||
| return Predicate.this.apply(arg) || rhs.apply(arg); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| public Predicate<T> not() { | ||
| return new Predicate<T>() { | ||
| @Override | ||
| public Boolean apply(T arg) { | ||
| return !Predicate.this.apply(arg); | ||
| } | ||
| }; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,179 @@ | ||
| package ru.spbau.mit; | ||
|
|
||
| import org.junit.Test; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.Objects; | ||
|
|
||
| import static java.util.Collections.emptyList; | ||
| import static java.util.Collections.singletonList; | ||
| import static org.junit.Assert.*; | ||
|
|
||
| public class CollectionsTest { | ||
| private static final Integer MAGIC_1 = 1; | ||
| private static final Integer MAGIC_2 = -4; | ||
| private static final Integer MAGIC_3 = 3; | ||
| private static final Integer MAGIC_4 = 10; | ||
| private static final Integer MAGIC_5 = -20; | ||
|
|
||
| private static final List<Integer> INT_ARR = Arrays.asList( | ||
| MAGIC_1, MAGIC_2, MAGIC_3, MAGIC_4, MAGIC_5); | ||
|
|
||
| private static final List<Integer> INT_ARR_NEGATED = Arrays.asList( | ||
| -MAGIC_1, -MAGIC_2, -MAGIC_3, -MAGIC_4, -MAGIC_5); | ||
|
|
||
| private static final List<Integer> ARR_WITH_NULL = Arrays.asList( | ||
| null, 1, null, 4, null); | ||
|
|
||
| private static final List<String> INT_ARR_STRINGED = Arrays.asList( | ||
| MAGIC_1.toString(), | ||
| MAGIC_2.toString(), | ||
| MAGIC_3.toString(), | ||
| MAGIC_4.toString(), | ||
| MAGIC_5.toString() | ||
| ); | ||
|
|
||
| private static final Predicate<Integer> IS_POSITIVE_INTEGER = new Predicate<Integer>() { | ||
| @Override | ||
| public Boolean apply(Integer arg) { | ||
| return arg > 0; | ||
| } | ||
| }; | ||
|
|
||
| private static final Predicate<Object> IS_NULL = new Predicate<Object>() { | ||
| @Override | ||
| public Boolean apply(Object arg) { | ||
| return arg == null; | ||
| } | ||
| }; | ||
|
|
||
| private static final Predicate<Object> NOT_NULL = IS_NULL.not(); | ||
|
|
||
| private static final List<Object> EMPTY_ARR = emptyList(); | ||
|
|
||
| private static final Function2<Integer, Integer, Integer> MINUS = new Function2<Integer, Integer, Integer>() { | ||
| @Override | ||
| public Integer apply2(Integer arg1, Integer arg2) { | ||
| return arg1 - arg2; | ||
| } | ||
| }; | ||
|
|
||
| private static final Function1<Object, Object> ID = new Function1<Object, Object>() { | ||
| @Override | ||
| public Object apply(Object arg) { | ||
| return arg; | ||
| } | ||
| }; | ||
|
|
||
| private static final Function1<Object, String> TO_STRING = new Function1<Object, String>() { | ||
| @Override | ||
| public String apply(Object arg) { | ||
| return arg.toString(); | ||
| } | ||
| }; | ||
|
|
||
| private static final Function2<Object, Object, Object> FAIL_FUNCTION = | ||
| new Function2<Object, Object, Object>() { | ||
| @Override | ||
| public Object apply2(Object arg1, Object arg2) { | ||
| assert false; | ||
| return null; | ||
| } | ||
| }; | ||
|
|
||
| @Test | ||
| public void map() throws Exception { | ||
| assertEquals(INT_ARR_NEGATED, Collections.map(MINUS.bind1(0), INT_ARR)); | ||
| assertEquals(EMPTY_ARR, Collections.map(ID, EMPTY_ARR)); | ||
| assertEquals(INT_ARR_STRINGED, Collections.map(TO_STRING, INT_ARR)); | ||
| assertEquals(INT_ARR, Collections.map(ID, INT_ARR)); | ||
| } | ||
|
|
||
| @Test | ||
| public void filter() throws Exception { | ||
| final List<Integer> positiveOnly = Arrays.asList(-MAGIC_2, -MAGIC_5); | ||
| assertEquals(positiveOnly, Collections.filter(IS_POSITIVE_INTEGER, INT_ARR_NEGATED)); | ||
| assertEquals(INT_ARR, Collections.filter(Predicate.ALWAYS_TRUE, INT_ARR)); | ||
| assertEquals(EMPTY_ARR, Collections.filter(Predicate.ALWAYS_FALSE, INT_ARR)); | ||
| assertEquals(EMPTY_ARR, Collections.filter(IS_NULL, INT_ARR)); | ||
| assertEquals(Arrays.asList(null, null, null), Collections.filter(IS_NULL, ARR_WITH_NULL)); | ||
| Predicate<Object> isTen = new Predicate<Object>() { | ||
| @Override | ||
| public Boolean apply(Object arg) { | ||
| return (arg).toString().equals("10"); | ||
| } | ||
| }; | ||
| final int ten = 10; | ||
| assertEquals(singletonList(ten), Collections.filter(isTen, INT_ARR)); | ||
| } | ||
|
|
||
| @Test | ||
| public void takeWhile() throws Exception { | ||
| Predicate<CharSequence> u2 = new Predicate<CharSequence>() { | ||
| @Override | ||
| public Boolean apply(CharSequence charSequence) { | ||
| return charSequence.length() < 2; | ||
| } | ||
| }; | ||
| assertEquals(singletonList("1"), Collections.takeWhile(u2, INT_ARR_STRINGED)); | ||
| assertEquals(EMPTY_ARR, Collections.takeWhile(IS_POSITIVE_INTEGER, INT_ARR_NEGATED)); | ||
| assertEquals(INT_ARR_STRINGED, Collections.takeWhile(Predicate.ALWAYS_TRUE, INT_ARR_STRINGED)); | ||
| ArrayList<Integer> nullArray = new ArrayList<>(); | ||
| nullArray.add(null); | ||
| assertEquals(nullArray, Collections.takeWhile(IS_NULL, ARR_WITH_NULL)); | ||
| assertEquals(EMPTY_ARR, Collections.takeWhile(IS_NULL, INT_ARR)); | ||
| } | ||
|
|
||
| @Test | ||
| public void takeUnless() throws Exception { | ||
| assertEquals(EMPTY_ARR, Collections.takeUnless(IS_POSITIVE_INTEGER.not(), INT_ARR_NEGATED)); | ||
| assertEquals(singletonList(-1), Collections.takeUnless(IS_POSITIVE_INTEGER, INT_ARR_NEGATED)); | ||
| assertEquals(INT_ARR, Collections.takeUnless(Predicate.ALWAYS_FALSE, INT_ARR)); | ||
| ArrayList<Integer> nullArr = new ArrayList<>(); | ||
| nullArr.add(null); | ||
| assertEquals(nullArr, Collections.takeUnless(NOT_NULL, ARR_WITH_NULL)); | ||
| assertEquals(EMPTY_ARR, Collections.takeUnless(IS_NULL, ARR_WITH_NULL)); | ||
| } | ||
|
|
||
| @Test | ||
| public void foldl() throws Exception { | ||
| final Integer result = 10; | ||
| assertEquals(result, Collections.foldl(MINUS, INT_ARR, 0)); | ||
|
|
||
| final Function2<String, Object, String> accumulateString = new Function2<String, Object, String>() { | ||
| @Override | ||
| public String apply2(String arg1, Object arg2) { | ||
| return arg1 + Objects.toString(arg2); | ||
| } | ||
| }; | ||
|
|
||
| final String initString = "Hello, "; | ||
| final String resultString = "Hello, 1-4310-20"; | ||
| assertEquals(resultString, Collections.foldl(accumulateString, INT_ARR, initString)); | ||
| assertEquals(0, Collections.foldl(FAIL_FUNCTION, EMPTY_ARR, 0)); | ||
| String expected = "null1null4null"; | ||
| assertEquals(expected, Collections.foldl(accumulateString, ARR_WITH_NULL, "")); | ||
| } | ||
|
|
||
| @Test | ||
| public void foldr() throws Exception { | ||
|
|
||
| final Function2<Object, String, String> accumulateString = new Function2<Object, String, String>() { | ||
| @Override | ||
| public String apply2(Object arg1, String arg2) { | ||
| return Objects.toString(arg1) + arg2; | ||
| } | ||
| }; | ||
|
|
||
| final String initString = "123"; | ||
| final String resultString = "-14-3-1020123"; | ||
| assertEquals(resultString, Collections.foldr(accumulateString, INT_ARR_NEGATED, initString)); | ||
|
|
||
| final Integer init = -33; | ||
| final Integer result = 11; | ||
| assertEquals(result, Collections.foldr(MINUS, INT_ARR, init)); | ||
| assertEquals(0, Collections.foldr(FAIL_FUNCTION, EMPTY_ARR, 0)); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
лучше отключить проверку на константы в тестах
и не использовать обертки без надобности
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
checkstyle.xml находится не в моем репозитории. Был бы рад отключить проверку на константы, если бы знал, что сделаю это всего один раз.