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
49 changes: 49 additions & 0 deletions Function/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

# User-specific stuff:
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/dictionaries

# Sensitive or high-churn files:
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.xml
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml

# Gradle:
.idea/**/gradle.xml
.idea/**/libraries

# CMake
cmake-build-debug/

# Mongo Explorer plugin:
.idea/**/mongoSettings.xml

## File-based project format:
*.iws

## Plugin-specific files:

# IntelliJ
out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Cursive Clojure plugin
.idea/replstate.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties
1 change: 1 addition & 0 deletions Function/.idea/.name

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

18 changes: 18 additions & 0 deletions Function/.idea/compiler.xml

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

13 changes: 13 additions & 0 deletions Function/.idea/misc.xml

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

8 changes: 8 additions & 0 deletions Function/.idea/modules.xml

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

22 changes: 22 additions & 0 deletions Function/Function.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: junit:junit:4.8" level="project" />
<orderEntry type="library" name="Maven: org.jetbrains:annotations:13.0" level="project" />
<orderEntry type="library" name="Maven: com.google.guava:guava:23.2-jre" level="project" />
<orderEntry type="library" name="Maven: com.google.code.findbugs:jsr305:1.3.9" level="project" />
<orderEntry type="library" name="Maven: com.google.errorprone:error_prone_annotations:2.0.18" level="project" />
<orderEntry type="library" name="Maven: com.google.j2objc:j2objc-annotations:1.1" level="project" />
<orderEntry type="library" name="Maven: org.codehaus.mojo:animal-sniffer-annotations:1.14" level="project" />
</component>
</module>
47 changes: 47 additions & 0 deletions Function/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>ru.spbau.mit.kazakov</groupId>
<artifactId>Function</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8</version>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>13.0</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>23.2-jre</version>
</dependency>
</dependencies>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>

</project>
126 changes: 126 additions & 0 deletions Function/src/main/java/ru/spbau/mit/kazakov/Function/Collections.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package ru.spbau.mit.kazakov.Function;


import com.google.common.collect.Lists;
import org.jetbrains.annotations.NotNull;

import java.util.*;

/**
* Class providing methods for working with collections using functions.
*/
public class Collections {
/**
* Applies specified function to specified container's elements and adds mapped elements in new list.
*
* @param function specified function
* @param elements specified container
* @param <T> type of elements in specified container
* @param <U> type of elements in result list
* @return result list
*/
static public <T, U> List<U> map(@NotNull final Function1<? super T, ? extends U> function, @NotNull final Iterable<T> elements) {
List<U> resultList = new ArrayList<>();

for (T element : elements) {
resultList.add(function.apply(element));
}

return resultList;
}

/**
* Creates new list composed of elements from specified container that satisfy specified predicate.
*
* @param predicate specified predicate
* @param elements specified container
* @param <T> type of elements in specified container
* @return result list
*/
static public <T> List<T> filter(@NotNull final Predicate<? super T> predicate, @NotNull final Iterable<T> elements) {
List<T> resultList = new ArrayList<>();

for (T element : elements) {
if (predicate.apply(element)) {
resultList.add(element);
}
}

return resultList;
}

/**
* Creates new list by adding elements from specified container while they satisfy specified predicate.
*
* @param predicate specified predicate
* @param elements specified container
* @param <T> type of elements in specified container
* @return result list
*/
static public <T> List<T> takeWhile(@NotNull final Predicate<? super T> predicate, @NotNull final Iterable<T> elements) {
List<T> resultList = new ArrayList<>();

for (T element : elements) {
if (!predicate.apply(element)) {
break;
}
resultList.add(element);
}

return resultList;
}

/**
* Creates new list by adding elements from specified container unless they satisfy specified predicate.
*
* @param predicate specified predicate
* @param elements specified container
* @param <T> type of elements in specified container
* @return result list
*/
static public <T> List<T> takeUnless(@NotNull final Predicate<? super T> predicate, @NotNull final Iterable<T> elements) {
return takeWhile(predicate.not(), elements);
}

/**
* Left-associative fold of specified collection. Reduces the list using specified function from left to right.
*
* @param function specified function
* @param startValue first value for function's first element
* @param elements specified collection
* @param <T> type of elements in specified collection
* @param <U> specified function's codomain
* @return result value
*/
static public <T, U> U foldl(@NotNull final Function2<? super U, ? super T, ? extends U> function,
U startValue, @NotNull final Collection<T> elements) {
U resultValue = startValue;

for (T element : elements) {
resultValue = function.apply(resultValue, element);
}

return resultValue;
}

/**
* Right-associative fold of specified collection. Reduces the list using specified function from right to left.
*
* @param function specified function
* @param startValue first value for function's second element
* @param elements specified collection
* @param <T> type of elements in specified collection
* @param <U> specified function's codomain
* @return result value
*/
static public <T, U> U foldr(@NotNull final Function2<? super T, ? super U, ? extends U> function,
U startValue, @NotNull final Collection<T> elements) {
U resultValue = startValue;

for (T element : Lists.reverse(new ArrayList<>(elements))) {
resultValue = function.apply(element, resultValue);
}

return resultValue;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package ru.spbau.mit.kazakov.Function;


import org.jetbrains.annotations.NotNull;

/**
* The interface for representing a function with one argument.
*
* @param <T> domain's type
* @param <U> codomain's type
*/
@FunctionalInterface
public interface Function1<T, U> {
/**
* Applies function to specified argument.
*
* @param argument for applying to
* @return mapped value
*/
U apply(T argument);

/**
* Creates composition of functions.
*
* @param function for composition
* @param <V> specified function's codomain's type
* @return result of function composition
*/
default <V> Function1<T, V> compose(@NotNull final Function1<? super U, ? extends V> function) {
return argument -> function.apply(apply(argument));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package ru.spbau.mit.kazakov.Function;


import org.jetbrains.annotations.NotNull;

/**
* The interface for representing a function with two arguments.
*
* @param <T> first argument's type
* @param <U> second argument's type
* @param <K> codomain's type
*/
@FunctionalInterface
public interface Function2<T, U, K> {
/**
* Applies function to specified argument.
*
* @param firstArgument for applying to
* @param secondArgument for applying to
* @return mapped value
*/
K apply(T firstArgument, U secondArgument);

/**
* Creates composition of functions.
*
* @param <R> specified function's codomain's type
* @return result of function composition
*/
default <R> Function2<T, U, R> compose(@NotNull final Function1<? super K, ? extends R> function) {
return (firstArgument, secondArgument) -> function.apply(apply(firstArgument, secondArgument));
}

/**
* Binds first argument to specified value.
*
* @param firstArgument specified value
* @return function of one argument
*/
default Function1<U, K> bind1(T firstArgument) {
return secondArgument -> apply(firstArgument, secondArgument);
}

/**
* Binds second argument to specified value.
*
* @param secondArgument specified value
* @return function of one argument
*/
default Function1<T, K> bind2(U secondArgument) {
return firstArgument -> apply(firstArgument, secondArgument);
}

/**
* Converts current function with two arguments into two functions with one argument.
*
* @return new function with one argument
*/
default Function1<T, Function1<U, K>> curry() {
return this::bind1;
}
}
Loading