-
Notifications
You must be signed in to change notification settings - Fork 0
Java02. ДЗ 03, Гусев Андрей #4
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| version '1.0-SNAPSHOT' | ||
|
|
||
| apply plugin: 'java' | ||
|
|
||
| sourceCompatibility = 1.5 | ||
|
|
||
| repositories { | ||
| mavenCentral() | ||
| } | ||
|
|
||
| dependencies { | ||
| testCompile group: 'junit', name: 'junit', version: '4.11' | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| #Wed Oct 12 15:32:21 MSK 2016 | ||
| distributionBase=GRADLE_USER_HOME | ||
| distributionPath=wrapper/dists | ||
| zipStoreBase=GRADLE_USER_HOME | ||
| zipStorePath=wrapper/dists | ||
| distributionUrl=https\://services.gradle.org/distributions/gradle-2.13-bin.zip |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| rootProject.name = 'FunctionalJava' | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| import java.util.Iterator; | ||
| import java.util.LinkedList; | ||
| import java.util.List; | ||
|
|
||
| public class Collections { | ||
| public static <SourceType, DestType> List < DestType > map (Iterable<SourceType> iterable, | ||
| Function1<? super SourceType, DestType> function) throws Exception { | ||
| List<DestType> resList = new LinkedList<DestType>(); | ||
| for (SourceType curElement: iterable) { | ||
| resList.add(function.apply(curElement)); | ||
| } | ||
| return resList; | ||
| } | ||
|
|
||
| public static <T> List<T> filter(Iterable<T> iterable, | ||
| Predicate<? super T> predicate) throws Exception{ | ||
| List<T> trueList = new LinkedList<T>(); | ||
| for (T curElement : iterable) { | ||
| if (predicate.apply(curElement)) { | ||
| trueList.add(curElement); | ||
| } | ||
| } | ||
| return trueList; | ||
| } | ||
|
|
||
| public static <T> List<T> takeWhile(Iterable<T> iterable, | ||
| Predicate<? super T> predicate) throws Exception { | ||
| List<T> resList = new LinkedList<T>(); | ||
| Iterator<T> it = iterable.iterator(); | ||
| while (it.hasNext()) { | ||
| T curElem = it.next(); | ||
| if (!predicate.apply(curElem)) { | ||
| break; | ||
| } | ||
| resList.add(curElem); | ||
| } | ||
| return resList; | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. это можно было написать проще, вспомнив про break There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. еще короче, вообще без итератора |
||
|
|
||
| public static <T> List<T> takeUnless(Iterable<T> iterable, Predicate<? super T> | ||
| predicate) throws Exception { | ||
| return takeWhile(iterable, predicate.not()); | ||
| } | ||
|
|
||
| public static <SourceType, DestType> DestType foldl( | ||
| Function2<? super DestType,? super SourceType,DestType> function, | ||
| DestType init, Iterable<SourceType> iterable) throws Exception { | ||
| DestType res = init; | ||
| for (SourceType elem : iterable) { | ||
| res = function.apply(res, elem); | ||
| } | ||
| return res; | ||
| } | ||
|
|
||
| public static <SourceType, DestType> DestType foldr(Function2<? super SourceType, ?super DestType, DestType> function, | ||
| DestType init, Iterable<SourceType> iterable) throws Exception { | ||
| return foldrImpl(function, init, iterable.iterator()); | ||
| } | ||
|
|
||
| private static <SourceType, DestType> DestType foldrImpl(Function2<? super SourceType, ? super DestType, DestType> function, | ||
| DestType init, Iterator<SourceType> iterator) throws Exception { | ||
| if (iterator.hasNext()) { | ||
| SourceType curElem = iterator.next(); | ||
| return function.apply(curElem, foldrImpl(function, init, iterator)); | ||
| } | ||
| return init; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| public abstract class Function1 < ArgType, ValueType > { | ||
| public abstract ValueType apply(ArgType x) throws Exception; | ||
|
|
||
| public < GValue > Function1 < ArgType, GValue > compose(final Function1 < ? super ValueType, GValue > g) { | ||
| return new Function1<ArgType, GValue>() { | ||
| @Override | ||
| public GValue apply(ArgType x) throws Exception{ | ||
| return g.apply(Function1.this.apply(x)); | ||
| } | ||
| }; | ||
| } | ||
| } |
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.
обычно длинные сигнатуры функций форматируются, как отформатирована сигнатура filter -- на первой строке первый параметр, последующие параметры под ним
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.
остальных функций это тоже касается)
кроме того, видно, что форматирование кода не производится, Ctrl + Alt + L в помощь )