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
13 changes: 13 additions & 0 deletions build.gradle
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'
}
Binary file added gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
6 changes: 6 additions & 0 deletions gradle/wrapper/gradle-wrapper.properties
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
164 changes: 164 additions & 0 deletions gradlew

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

90 changes: 90 additions & 0 deletions gradlew.bat

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
rootProject.name = 'FunctionalJava'

68 changes: 68 additions & 0 deletions src/main/java/Collections.java
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;
}

Choose a reason for hiding this comment

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

обычно длинные сигнатуры функций форматируются, как отформатирована сигнатура filter -- на первой строке первый параметр, последующие параметры под ним

Copy link

Choose a reason for hiding this comment

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

остальных функций это тоже касается)
кроме того, видно, что форматирование кода не производится, Ctrl + Alt + L в помощь )


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;
}

Choose a reason for hiding this comment

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

это можно было написать проще, вспомнив про break

Copy link

Choose a reason for hiding this comment

The 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;
}
}
12 changes: 12 additions & 0 deletions src/main/java/Function1.java
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));
}
};
}
}
Loading