diff --git a/05-FP/.idea/compiler.xml b/05-FP/.idea/compiler.xml
new file mode 100644
index 0000000..9224652
--- /dev/null
+++ b/05-FP/.idea/compiler.xml
@@ -0,0 +1,26 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/05-FP/.idea/copyright/profiles_settings.xml b/05-FP/.idea/copyright/profiles_settings.xml
new file mode 100644
index 0000000..e7bedf3
--- /dev/null
+++ b/05-FP/.idea/copyright/profiles_settings.xml
@@ -0,0 +1,3 @@
+
+
+
\ No newline at end of file
diff --git a/05-FP/.idea/gradle.xml b/05-FP/.idea/gradle.xml
new file mode 100644
index 0000000..89fcdaf
--- /dev/null
+++ b/05-FP/.idea/gradle.xml
@@ -0,0 +1,19 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/05-FP/.idea/libraries/Gradle__junit_junit_4_11.xml b/05-FP/.idea/libraries/Gradle__junit_junit_4_11.xml
new file mode 100644
index 0000000..dc26b34
--- /dev/null
+++ b/05-FP/.idea/libraries/Gradle__junit_junit_4_11.xml
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/05-FP/.idea/libraries/Gradle__org_hamcrest_hamcrest_core_1_3.xml b/05-FP/.idea/libraries/Gradle__org_hamcrest_hamcrest_core_1_3.xml
new file mode 100644
index 0000000..8262f72
--- /dev/null
+++ b/05-FP/.idea/libraries/Gradle__org_hamcrest_hamcrest_core_1_3.xml
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/05-FP/.idea/misc.xml b/05-FP/.idea/misc.xml
new file mode 100644
index 0000000..18d93cc
--- /dev/null
+++ b/05-FP/.idea/misc.xml
@@ -0,0 +1,57 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/05-FP/.idea/modules.xml b/05-FP/.idea/modules.xml
new file mode 100644
index 0000000..c175ac9
--- /dev/null
+++ b/05-FP/.idea/modules.xml
@@ -0,0 +1,10 @@
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/05-FP/.idea/modules/05-FP.iml b/05-FP/.idea/modules/05-FP.iml
new file mode 100644
index 0000000..da422c0
--- /dev/null
+++ b/05-FP/.idea/modules/05-FP.iml
@@ -0,0 +1,12 @@
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/05-FP/.idea/modules/05-FP_main.iml b/05-FP/.idea/modules/05-FP_main.iml
new file mode 100644
index 0000000..8202b0a
--- /dev/null
+++ b/05-FP/.idea/modules/05-FP_main.iml
@@ -0,0 +1,22 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/05-FP/.idea/modules/05-FP_test.iml b/05-FP/.idea/modules/05-FP_test.iml
new file mode 100644
index 0000000..7faa4cb
--- /dev/null
+++ b/05-FP/.idea/modules/05-FP_test.iml
@@ -0,0 +1,17 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/05-FP/build.gradle b/05-FP/build.gradle
new file mode 100644
index 0000000..73ccf9f
--- /dev/null
+++ b/05-FP/build.gradle
@@ -0,0 +1,14 @@
+group 'ru.spbau.bachelor2015.veselov.hw05'
+version '1.0-SNAPSHOT'
+
+apply plugin: 'java'
+
+sourceCompatibility = 1.8
+
+repositories {
+ mavenCentral()
+}
+
+dependencies {
+ testCompile group: 'junit', name: 'junit', version: '4.11'
+}
diff --git a/05-FP/gradle/wrapper/gradle-wrapper.jar b/05-FP/gradle/wrapper/gradle-wrapper.jar
new file mode 100644
index 0000000..ca78035
Binary files /dev/null and b/05-FP/gradle/wrapper/gradle-wrapper.jar differ
diff --git a/05-FP/gradle/wrapper/gradle-wrapper.properties b/05-FP/gradle/wrapper/gradle-wrapper.properties
new file mode 100644
index 0000000..edabcec
--- /dev/null
+++ b/05-FP/gradle/wrapper/gradle-wrapper.properties
@@ -0,0 +1,6 @@
+#Fri Oct 14 23:40:16 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
diff --git a/05-FP/gradlew b/05-FP/gradlew
new file mode 100755
index 0000000..27309d9
--- /dev/null
+++ b/05-FP/gradlew
@@ -0,0 +1,164 @@
+#!/usr/bin/env bash
+
+##############################################################################
+##
+## Gradle start up script for UN*X
+##
+##############################################################################
+
+# Attempt to set APP_HOME
+# Resolve links: $0 may be a link
+PRG="$0"
+# Need this for relative symlinks.
+while [ -h "$PRG" ] ; do
+ ls=`ls -ld "$PRG"`
+ link=`expr "$ls" : '.*-> \(.*\)$'`
+ if expr "$link" : '/.*' > /dev/null; then
+ PRG="$link"
+ else
+ PRG=`dirname "$PRG"`"/$link"
+ fi
+done
+SAVED="`pwd`"
+cd "`dirname \"$PRG\"`/" >/dev/null
+APP_HOME="`pwd -P`"
+cd "$SAVED" >/dev/null
+
+APP_NAME="Gradle"
+APP_BASE_NAME=`basename "$0"`
+
+# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
+DEFAULT_JVM_OPTS=""
+
+# Use the maximum available, or set MAX_FD != -1 to use that value.
+MAX_FD="maximum"
+
+warn ( ) {
+ echo "$*"
+}
+
+die ( ) {
+ echo
+ echo "$*"
+ echo
+ exit 1
+}
+
+# OS specific support (must be 'true' or 'false').
+cygwin=false
+msys=false
+darwin=false
+nonstop=false
+case "`uname`" in
+ CYGWIN* )
+ cygwin=true
+ ;;
+ Darwin* )
+ darwin=true
+ ;;
+ MINGW* )
+ msys=true
+ ;;
+ NONSTOP* )
+ nonstop=true
+ ;;
+esac
+
+CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
+
+# Determine the Java command to use to start the JVM.
+if [ -n "$JAVA_HOME" ] ; then
+ if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
+ # IBM's JDK on AIX uses strange locations for the executables
+ JAVACMD="$JAVA_HOME/jre/sh/java"
+ else
+ JAVACMD="$JAVA_HOME/bin/java"
+ fi
+ if [ ! -x "$JAVACMD" ] ; then
+ die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
+
+Please set the JAVA_HOME variable in your environment to match the
+location of your Java installation."
+ fi
+else
+ JAVACMD="java"
+ which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
+
+Please set the JAVA_HOME variable in your environment to match the
+location of your Java installation."
+fi
+
+# Increase the maximum file descriptors if we can.
+if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
+ MAX_FD_LIMIT=`ulimit -H -n`
+ if [ $? -eq 0 ] ; then
+ if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
+ MAX_FD="$MAX_FD_LIMIT"
+ fi
+ ulimit -n $MAX_FD
+ if [ $? -ne 0 ] ; then
+ warn "Could not set maximum file descriptor limit: $MAX_FD"
+ fi
+ else
+ warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
+ fi
+fi
+
+# For Darwin, add options to specify how the application appears in the dock
+if $darwin; then
+ GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
+fi
+
+# For Cygwin, switch paths to Windows format before running java
+if $cygwin ; then
+ APP_HOME=`cygpath --path --mixed "$APP_HOME"`
+ CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
+ JAVACMD=`cygpath --unix "$JAVACMD"`
+
+ # We build the pattern for arguments to be converted via cygpath
+ ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
+ SEP=""
+ for dir in $ROOTDIRSRAW ; do
+ ROOTDIRS="$ROOTDIRS$SEP$dir"
+ SEP="|"
+ done
+ OURCYGPATTERN="(^($ROOTDIRS))"
+ # Add a user-defined pattern to the cygpath arguments
+ if [ "$GRADLE_CYGPATTERN" != "" ] ; then
+ OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
+ fi
+ # Now convert the arguments - kludge to limit ourselves to /bin/sh
+ i=0
+ for arg in "$@" ; do
+ CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
+ CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
+
+ if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
+ eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
+ else
+ eval `echo args$i`="\"$arg\""
+ fi
+ i=$((i+1))
+ done
+ case $i in
+ (0) set -- ;;
+ (1) set -- "$args0" ;;
+ (2) set -- "$args0" "$args1" ;;
+ (3) set -- "$args0" "$args1" "$args2" ;;
+ (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
+ (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
+ (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
+ (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
+ (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
+ (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
+ esac
+fi
+
+# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
+function splitJvmOpts() {
+ JVM_OPTS=("$@")
+}
+eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
+JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
+
+exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
diff --git a/05-FP/gradlew.bat b/05-FP/gradlew.bat
new file mode 100644
index 0000000..f6d5974
--- /dev/null
+++ b/05-FP/gradlew.bat
@@ -0,0 +1,90 @@
+@if "%DEBUG%" == "" @echo off
+@rem ##########################################################################
+@rem
+@rem Gradle startup script for Windows
+@rem
+@rem ##########################################################################
+
+@rem Set local scope for the variables with windows NT shell
+if "%OS%"=="Windows_NT" setlocal
+
+set DIRNAME=%~dp0
+if "%DIRNAME%" == "" set DIRNAME=.
+set APP_BASE_NAME=%~n0
+set APP_HOME=%DIRNAME%
+
+@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
+set DEFAULT_JVM_OPTS=
+
+@rem Find java.exe
+if defined JAVA_HOME goto findJavaFromJavaHome
+
+set JAVA_EXE=java.exe
+%JAVA_EXE% -version >NUL 2>&1
+if "%ERRORLEVEL%" == "0" goto init
+
+echo.
+echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
+echo.
+echo Please set the JAVA_HOME variable in your environment to match the
+echo location of your Java installation.
+
+goto fail
+
+:findJavaFromJavaHome
+set JAVA_HOME=%JAVA_HOME:"=%
+set JAVA_EXE=%JAVA_HOME%/bin/java.exe
+
+if exist "%JAVA_EXE%" goto init
+
+echo.
+echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
+echo.
+echo Please set the JAVA_HOME variable in your environment to match the
+echo location of your Java installation.
+
+goto fail
+
+:init
+@rem Get command-line arguments, handling Windows variants
+
+if not "%OS%" == "Windows_NT" goto win9xME_args
+if "%@eval[2+2]" == "4" goto 4NT_args
+
+:win9xME_args
+@rem Slurp the command line arguments.
+set CMD_LINE_ARGS=
+set _SKIP=2
+
+:win9xME_args_slurp
+if "x%~1" == "x" goto execute
+
+set CMD_LINE_ARGS=%*
+goto execute
+
+:4NT_args
+@rem Get arguments from the 4NT Shell from JP Software
+set CMD_LINE_ARGS=%$
+
+:execute
+@rem Setup the command line
+
+set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
+
+@rem Execute Gradle
+"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
+
+:end
+@rem End local scope for the variables with windows NT shell
+if "%ERRORLEVEL%"=="0" goto mainEnd
+
+:fail
+rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
+rem the _cmd.exe /c_ return code!
+if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
+exit /b 1
+
+:mainEnd
+if "%OS%"=="Windows_NT" endlocal
+
+:omega
diff --git a/05-FP/settings.gradle b/05-FP/settings.gradle
new file mode 100644
index 0000000..c5b4539
--- /dev/null
+++ b/05-FP/settings.gradle
@@ -0,0 +1,2 @@
+rootProject.name = '05-FP'
+
diff --git a/05-FP/src/main/java/ru/spbau/bachelor2015/veselov/hw05/Collections.java b/05-FP/src/main/java/ru/spbau/bachelor2015/veselov/hw05/Collections.java
new file mode 100644
index 0000000..f89cddc
--- /dev/null
+++ b/05-FP/src/main/java/ru/spbau/bachelor2015/veselov/hw05/Collections.java
@@ -0,0 +1,214 @@
+package ru.spbau.bachelor2015.veselov.hw05;
+
+import org.jetbrains.annotations.NotNull;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.NoSuchElementException;
+
+/**
+ * Class which provides functions which use Collections and Functional classes together.
+ */
+public final class Collections {
+ /**
+ * Applies given function to all elements of iterable object and returns iterable object of results of application.
+ *
+ * @param function A function which is applied to elements.
+ * @param iterable An iterable object which represents a list of elements.
+ * @param Type of function argument.
+ * @param Type of function returning value.
+ * @param Type of elements in iterable object.
+ * @return Non modifiable Iterable object which consists of results of application.
+ */
+ public static @NotNull Iterable map(
+ @NotNull Function1 function, @NotNull Iterable iterable) {
+ return () -> new Iterator() {
+ private Iterator iterator = iterable.iterator();
+
+ @Override
+ public boolean hasNext() {
+ return iterator.hasNext();
+ }
+
+ @Override
+ public Target next() {
+ return function.apply(iterator.next());
+ }
+ };
+ }
+
+ /**
+ * Filters elements of iterable object by using a given predicate.
+ *
+ * @param predicate A predicate to filter elements.
+ * @param iterable A list of elements.
+ * @param Type of predicate argument.
+ * @param Type of elements in a list.
+ * @return New non modifiable list of elements which were accepted by predicate.
+ */
+ public static @NotNull Iterable filter(@NotNull Predicate predicate,
+ @NotNull Iterable iterable) {
+ return () -> new Iterator() {
+ private Iterator iterator = iterable.iterator();
+ private T element = null;
+
+ @Override
+ public boolean hasNext() {
+ if (element != null) {
+ return true;
+ }
+
+ while (iterator.hasNext()) {
+ element = iterator.next();
+ if (predicate.apply(element)) {
+ return true;
+ }
+ }
+
+ element = null;
+ return false;
+ }
+
+ @Override
+ public T next() {
+ if (!hasNext()) {
+ throw new NoSuchElementException();
+ }
+
+ T tmp = element;
+ element = null;
+ return tmp;
+ }
+ };
+ }
+
+ /**
+ * Returns some elements from the beginning of a given list of elements up to an element which was rejected by a
+ * given predicate. Rejected element will not be included in resulting list.
+ *
+ * @param predicate A predicate which determines terminal element.
+ * @param iterable A list of elements.
+ * @param Type of predicate argument.
+ * @param Type of elements in a list.
+ * @return New non modifiable list which is a prefix of a given one.
+ */
+ public static @NotNull Iterable takeWhile(@NotNull Predicate predicate,
+ @NotNull Iterable iterable) {
+ return () -> new Iterator() {
+ Iterator iterator = iterable.iterator();
+ T element = null;
+ boolean terminated = false;
+
+ @Override
+ public boolean hasNext() {
+ if (terminated) {
+ return false;
+ }
+
+ if (element != null) {
+ return true;
+ }
+
+ if (!iterator.hasNext()) {
+ return false;
+ }
+
+ element = iterator.next();
+ if (predicate.apply(element)) {
+ return true;
+ }
+
+ terminated = true;
+ return false;
+ }
+
+ @Override
+ public T next() {
+ if (!hasNext()) {
+ throw new NoSuchElementException();
+ }
+
+ T tmp = element;
+ element = null;
+ return tmp;
+ }
+ };
+ }
+
+ /**
+ * Returns some elements from the beginning of a given list of elements up to an element which was accepted by a
+ * given predicate. Accepted element will not be included in resulting list.
+ *
+ * @param predicate A predicate which determines terminal element.
+ * @param iterable A list of elements.
+ * @param Type of predicate argument.
+ * @param Type of elements in a list.
+ * @return New non modifiable list which is a prefix of a given one.
+ */
+ public static @NotNull Iterable takeUnless(@NotNull Predicate predicate,
+ @NotNull Iterable iterable) {
+ return takeWhile(predicate.not(), iterable);
+ }
+
+ /**
+ * Folds given list of elements into one value which is computed by a given function of two arguments. Method
+ * sequentially (from the beginning of the list) applies this function to the elements of the list and accumulates
+ * result. The function should accept accumulated result and next element.
+ *
+ * @param function A function of two arguments which folds a list of elements.
+ * @param initialValue Initially accumulated result.
+ * @param iterable A list of elements.
+ * @param Type of first argument of a function.
+ * @param Type of second argument of a function.
+ * @param Type of returning value of a function.
+ * @param Type of elements in a list.
+ * @return One value - the result of folding.
+ */
+ public static @NotNull Target foldl(
+ @NotNull Function2 function,
+ Target initialValue,
+ @NotNull Iterable iterable) {
+ Target accumulatedValue = initialValue;
+ for (T element : iterable) {
+ accumulatedValue = function.apply(accumulatedValue, element);
+ }
+
+ return accumulatedValue;
+ }
+
+ /**
+ * Folds given list of elements into one value which is computed by a given function of two arguments. Method
+ * sequentially (from the end of the list) applies this function to the elements of the list and accumulates
+ * result. The function should accept accumulated result and next element.
+ *
+ * @param function A function of two arguments which folds a list of elements.
+ * @param initialValue Initially accumulated result.
+ * @param iterable A list of elements.
+ * @param Type of first argument of a function.
+ * @param Type of second argument of a function.
+ * @param Type of returning value of a function.
+ * @param Type of elements in a list.
+ * @return One value - the result of folding.
+ */
+ public static @NotNull Target foldr(
+ @NotNull Function2 function,
+ Target initialValue,
+ @NotNull Iterable iterable) {
+ return foldl(function, initialValue, reversed(iterable));
+ }
+
+ private Collections() {
+ throw new UnsupportedOperationException();
+ }
+
+ private static @NotNull Iterable reversed(@NotNull Iterable iterable) {
+ List list = new ArrayList<>();
+ for (T element : iterable) {
+ list.add(element);
+ }
+
+ java.util.Collections.reverse(list);
+ return list;
+ }
+}
diff --git a/05-FP/src/main/java/ru/spbau/bachelor2015/veselov/hw05/Function1.java b/05-FP/src/main/java/ru/spbau/bachelor2015/veselov/hw05/Function1.java
new file mode 100644
index 0000000..c659c31
--- /dev/null
+++ b/05-FP/src/main/java/ru/spbau/bachelor2015/veselov/hw05/Function1.java
@@ -0,0 +1,30 @@
+package ru.spbau.bachelor2015.veselov.hw05;
+
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * Interface that represents function which can be applied to one argument.
+ *
+ * @param Type of function argument.
+ * @param Type of returning value.
+ */
+public interface Function1 {
+ /**
+ * Applies Function to an argument.
+ *
+ * @param argument Argument of a function.
+ * @return The result of application.
+ */
+ Target apply(Source argument);
+
+ /**
+ * Composes given Function with current one.
+ *
+ * @param function A Function which will be composed with current.
+ * @param Type of returning value of method argument.
+ * @return The resulting composition.
+ */
+ default @NotNull Function1 compose(@NotNull Function1 super Target, ArgTarget> function) {
+ return argument -> function.apply(Function1.this.apply(argument));
+ }
+}
diff --git a/05-FP/src/main/java/ru/spbau/bachelor2015/veselov/hw05/Function2.java b/05-FP/src/main/java/ru/spbau/bachelor2015/veselov/hw05/Function2.java
new file mode 100644
index 0000000..5c25eb5
--- /dev/null
+++ b/05-FP/src/main/java/ru/spbau/bachelor2015/veselov/hw05/Function2.java
@@ -0,0 +1,62 @@
+package ru.spbau.bachelor2015.veselov.hw05;
+
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * Interface that represents function of two arguments which can be applied to them.
+ *
+ * @param Type of function first argument.
+ * @param Type of function second argument.
+ * @param Type of returning value.
+ */
+public interface Function2 {
+ /**
+ * Applies Function to arguments.
+ *
+ * @param argument1 First argument of a function.
+ * @param argument2 Second argument of a function.
+ * @return The result of application.
+ */
+ Target apply(Source1 argument1, Source2 argument2);
+
+ /**
+ * Composes given Function1 with current one.
+ *
+ * @param function A Function which will be composed with current.
+ * @param Type of returning value of method argument.
+ * @return The resulting composition.
+ */
+ default @NotNull Function2 compose(
+ @NotNull Function1 super Target, ArgTarget> function) {
+ return (argument1, argument2) -> function.apply(Function2.this.apply(argument1, argument2));
+ }
+
+ /**
+ * Binds function first argument to a given value.
+ *
+ * @param argument1 A value to which first argument will be bound.
+ * @return The resulting function of one argument.
+ */
+ default @NotNull Function1 bind1(Source1 argument1) {
+ return argument2 -> Function2.this.apply(argument1, argument2);
+ }
+
+ /**
+ * Binds function second argument to a given value.
+ *
+ * @param argument2 A value to which second argument will be bound.
+ * @return The resulting function of one argument.
+ */
+ default @NotNull Function1 bind2(Source2 argument2) {
+ return argument1 -> Function2.this.apply(argument1, argument2);
+ }
+
+ /**
+ * Curries function.
+ *
+ * @return Function of one argument which returns function.
+ */
+ default @NotNull Function1> curry() {
+ return Function2.this::bind1;
+ }
+}
diff --git a/05-FP/src/main/java/ru/spbau/bachelor2015/veselov/hw05/Predicate.java b/05-FP/src/main/java/ru/spbau/bachelor2015/veselov/hw05/Predicate.java
new file mode 100644
index 0000000..fb7c747
--- /dev/null
+++ b/05-FP/src/main/java/ru/spbau/bachelor2015/veselov/hw05/Predicate.java
@@ -0,0 +1,53 @@
+package ru.spbau.bachelor2015.veselov.hw05;
+
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * A function of one argument which either accepts it (returns true) or rejects it (returns false).
+ *
+ * @param Type of predicate argument.
+ */
+public interface Predicate extends Function1 {
+ /**
+ * Returns new predicate which is disjunction of current and given one.
+ *
+ * @param predicate A predicate to make a disjunction with.
+ * @param Type of second predicate argument.
+ * @return Disjunction of predicates.
+ */
+ default @NotNull Predicate or(@NotNull Predicate predicate) {
+ return argument -> Predicate.this.apply(argument) || predicate.apply(argument);
+ }
+
+ /**
+ * Returns new predicate which is conjunction of current and given one.
+ *
+ * @param predicate A predicate to make a conjunction with.
+ * @param Type of second predicate argument.
+ * @return Conjunction of predicates.
+ */
+ default @NotNull Predicate and(@NotNull Predicate predicate) {
+ return argument -> Predicate.this.apply(argument) && predicate.apply(argument);
+ }
+
+ /**
+ * @return A predicate which is the opposite of current one.
+ */
+ default @NotNull Predicate not() {
+ return argument -> !Predicate.this.apply(argument);
+ }
+
+ /**
+ * @return A predicate which is always true.
+ */
+ static @NotNull Predicate alwaysTrue() {
+ return argument -> true;
+ }
+
+ /**
+ * @return A predicate which is always false.
+ */
+ static @NotNull Predicate alwaysFalse() {
+ return argument -> false;
+ }
+}
diff --git a/05-FP/src/test/java/ru/spbau/bachelor2015/veselov/hw05/CollectionsTest.java b/05-FP/src/test/java/ru/spbau/bachelor2015/veselov/hw05/CollectionsTest.java
new file mode 100644
index 0000000..099ea68
--- /dev/null
+++ b/05-FP/src/test/java/ru/spbau/bachelor2015/veselov/hw05/CollectionsTest.java
@@ -0,0 +1,79 @@
+package ru.spbau.bachelor2015.veselov.hw05;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.List;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+public class CollectionsTest {
+ private List naturalsUpTo100 = new ArrayList<>();
+ private List naturalsUpTo100Str = new ArrayList<>();
+ private List evensUpTo100 = new ArrayList<>();
+ private List naturalsUpTo50 = new ArrayList<>();
+ private List someWords = Arrays.asList("Just", "a", "visual", "artifact");
+
+ @Before
+ public void setUp() throws Exception {
+ for (int i = 0; i <= 100; i++) {
+ naturalsUpTo100.add(i);
+ naturalsUpTo100Str.add(Integer.toString(i));
+
+ if (i % 2 == 0) {
+ evensUpTo100.add(i);
+ }
+
+ if (i <= 50) {
+ naturalsUpTo50.add(i);
+ }
+ }
+ }
+
+ @Test
+ public void testMap() throws Exception {
+ compareIterable(Collections.map(Object::toString, naturalsUpTo100), naturalsUpTo100Str);
+ }
+
+ @Test
+ public void testFilter() throws Exception {
+ compareIterable(Collections.filter(n -> n % 2 == 0, naturalsUpTo100), evensUpTo100);
+ }
+
+ @Test
+ public void testTakeWhile() throws Exception {
+ compareIterable(Collections.takeWhile(n -> n <= 50, naturalsUpTo100), naturalsUpTo50);
+ }
+
+ @Test
+ public void testTakeUnless() throws Exception {
+ compareIterable(Collections.takeUnless(n -> n > 50, naturalsUpTo100), naturalsUpTo50);
+ }
+
+ @Test
+ public void testFoldl() throws Exception {
+ assertEquals("Java", Collections.foldl((a, s) -> a + Character.toString(s.charAt(0)), "", someWords));
+ }
+
+ @Test
+ public void testFoldr() throws Exception {
+ assertEquals("DjavaJ", Collections.foldr((a, s) -> a + Character.toString(s.charAt(0)), "Dj", someWords));
+ }
+
+ private void compareIterable(Iterable iterable1, Iterable iterable2) throws Exception {
+ Iterator it1 = iterable1.iterator();
+ Iterator it2 = iterable2.iterator();
+
+ while (it1.hasNext()) {
+ assertTrue(it2.hasNext());
+ assertTrue(it1.next().equals(it2.next()));
+ }
+
+ assertFalse(it2.hasNext());
+ }
+}
\ No newline at end of file
diff --git a/05-FP/src/test/java/ru/spbau/bachelor2015/veselov/hw05/Function1Test.java b/05-FP/src/test/java/ru/spbau/bachelor2015/veselov/hw05/Function1Test.java
new file mode 100644
index 0000000..6365ee4
--- /dev/null
+++ b/05-FP/src/test/java/ru/spbau/bachelor2015/veselov/hw05/Function1Test.java
@@ -0,0 +1,54 @@
+package ru.spbau.bachelor2015.veselov.hw05;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+public class Function1Test {
+ private Function1 id;
+ private Function1 linear2;
+ private Function1 linear3Shift5;
+ private Function1 quadratic;
+
+ @Before
+ public void setUp() throws Exception {
+ id = x -> x;
+ linear2 = x -> 2 * x;
+ linear3Shift5 = x -> 3 * x + 5;
+ quadratic = x -> x * x;
+ }
+
+ @Test
+ public void testCompose1() throws Exception {
+ Function1 id2 = id.compose(id);
+ for (int i = -100; i <= 100; i++) {
+ assertEquals(new Integer(i), id2.apply(i));
+ }
+ }
+
+ @Test
+ public void testCompose2() throws Exception {
+ Function1 id2 = linear3Shift5.compose(linear2);
+ for (int i = -100; i <= 100; i++) {
+ assertEquals(new Integer(2 * (3 * i + 5)), id2.apply(i));
+ }
+ }
+
+ @Test
+ public void testCompose3() throws Exception {
+ Function1 quadratic4 = linear2.compose(quadratic);
+ for (int i = -100; i <= 100; i++) {
+ assertEquals(new Integer(4 * i * i), quadratic4.apply(i));
+ }
+ }
+
+ @Test
+ public void testTypes() throws Exception {
+ Function1 f = Object::toString;
+ Function1 g = s -> "[" + s + "]";
+ Function1 h = f.compose(g);
+
+ assertEquals("[128]", h.apply(128));
+ }
+}
\ No newline at end of file
diff --git a/05-FP/src/test/java/ru/spbau/bachelor2015/veselov/hw05/Function2Test.java b/05-FP/src/test/java/ru/spbau/bachelor2015/veselov/hw05/Function2Test.java
new file mode 100644
index 0000000..f7dccfb
--- /dev/null
+++ b/05-FP/src/test/java/ru/spbau/bachelor2015/veselov/hw05/Function2Test.java
@@ -0,0 +1,81 @@
+package ru.spbau.bachelor2015.veselov.hw05;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+public class Function2Test {
+ private Function1 linear2;
+ private Function1 quadratic;
+
+ private Function2 linearXY;
+ private Function2 cubicXXY;
+
+ @Before
+ public void setUp() throws Exception {
+ linear2 = x -> 2 * x;
+ quadratic = x -> x * x;
+
+ linearXY = (x, y) -> x + y;
+ cubicXXY = (x, y) -> x * x * y;
+ }
+
+ @Test
+ public void testCompose1() throws Exception {
+ Function2 quadraticSum = linearXY.compose(quadratic);
+
+ for (int i = -100; i <= 100; i++) {
+ for (int j = -100; j <= 100; j++) {
+ assertEquals(new Integer((i + j) * (i + j)), quadraticSum.apply(i, j));
+ }
+ }
+ }
+
+ @Test
+ public void testCompose2() throws Exception {
+ Function2 cubic2XXY = cubicXXY.compose(linear2);
+
+ for (int i = -100; i <= 100; i++) {
+ for (int j = -100; j <= 100; j++) {
+ assertEquals(new Integer(2 * i * i * j), cubic2XXY.apply(i, j));
+ }
+ }
+ }
+
+ @Test
+ public void testBind1() throws Exception {
+ TestExtensionality.test(linearXY.bind1(3), y -> 3 + y);
+ TestExtensionality.test(cubicXXY.bind1(3), y -> 3 * 3 * y);
+ }
+
+ @Test
+ public void testBind2() throws Exception {
+ TestExtensionality.test(linearXY.bind2(9), x -> x + 9);
+ TestExtensionality.test(cubicXXY.bind2(3), x -> x * x * 3);
+ }
+
+ @Test
+ public void testCurry() throws Exception {
+ TestExtensionality.test(linearXY.curry().apply(-5), y -> -5 + y);
+ TestExtensionality.test(cubicXXY.curry().apply(-5), y -> -5 * (-5) * y);
+ }
+
+ @Test
+ public void testTypes() throws Exception {
+ Function2 f = (n, s) -> s + n.toString() + s;
+ Function1 g = f.bind2("|");
+
+ assertEquals("|128|", g.apply(128));
+ }
+}
+
+class TestExtensionality {
+ public static void test(Function1 f1, Function1 f2) {
+ final int limit = 100;
+ for (int i = -limit; i <= limit; i++) {
+ assertTrue(f1.apply(i).equals(f2.apply(i)));
+ }
+ }
+}
\ No newline at end of file
diff --git a/05-FP/src/test/java/ru/spbau/bachelor2015/veselov/hw05/PredicateTest.java b/05-FP/src/test/java/ru/spbau/bachelor2015/veselov/hw05/PredicateTest.java
new file mode 100644
index 0000000..e61b79b
--- /dev/null
+++ b/05-FP/src/test/java/ru/spbau/bachelor2015/veselov/hw05/PredicateTest.java
@@ -0,0 +1,58 @@
+package ru.spbau.bachelor2015.veselov.hw05;
+
+import org.junit.Before;
+import org.junit.Test;
+
+public class PredicateTest {
+ private Predicate even;
+ private Predicate positive;
+ private Predicate prime;
+
+ private static boolean isPrime(int n) {
+ n = Math.abs(n);
+ if (n == 0 || n == 1) {
+ return false;
+ }
+
+ for (int i = 2; i < n; i++) {
+ if (n % i == 0) {
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ @Before
+ public void setUp() throws Exception {
+ even = n -> n % 2 == 0;
+ positive = n -> n > 0;
+
+ prime = PredicateTest::isPrime;
+ }
+
+ @Test
+ public void testOr() throws Exception {
+ TestExtensionality.test(even.or(prime), n -> n % 2 == 0 || isPrime(n));
+ }
+
+ @Test
+ public void testAnd() throws Exception {
+ TestExtensionality.test(positive.and(prime), n -> n > 0 && isPrime(n));
+ }
+
+ @Test
+ public void testNot() throws Exception {
+ TestExtensionality.test(even.not(), n -> n % 2 != 0);
+ }
+
+ @Test
+ public void testAlwaysTrue() throws Exception {
+ TestExtensionality.test(Predicate.alwaysTrue(), n -> true);
+ }
+
+ @Test
+ public void testAlwaysFalse() throws Exception {
+ TestExtensionality.test(Predicate.alwaysFalse(), n -> false);
+ }
+}
\ No newline at end of file