diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5f81826 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.idea +.gradle +build/ \ No newline at end of file diff --git a/FunctionalJava/build.gradle b/FunctionalJava/build.gradle new file mode 100644 index 0000000..8c2ffda --- /dev/null +++ b/FunctionalJava/build.gradle @@ -0,0 +1,14 @@ +group 'ru.spbau.shevchenko' +version '1.0-SNAPSHOT' + +apply plugin: 'java' + +sourceCompatibility = 1.8 + +repositories { + mavenCentral() +} + +dependencies { + testCompile group: 'junit', name: 'junit', version: '4.11' +} diff --git a/FunctionalJava/gradle/wrapper/gradle-wrapper.jar b/FunctionalJava/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..ca78035 Binary files /dev/null and b/FunctionalJava/gradle/wrapper/gradle-wrapper.jar differ diff --git a/FunctionalJava/gradle/wrapper/gradle-wrapper.properties b/FunctionalJava/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..77b2cde --- /dev/null +++ b/FunctionalJava/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +#Mon Oct 24 23:58:01 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/FunctionalJava/gradlew b/FunctionalJava/gradlew new file mode 100755 index 0000000..27309d9 --- /dev/null +++ b/FunctionalJava/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/FunctionalJava/gradlew.bat b/FunctionalJava/gradlew.bat new file mode 100644 index 0000000..f6d5974 --- /dev/null +++ b/FunctionalJava/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/FunctionalJava/settings.gradle b/FunctionalJava/settings.gradle new file mode 100644 index 0000000..21b02ff --- /dev/null +++ b/FunctionalJava/settings.gradle @@ -0,0 +1,2 @@ +rootProject.name = 'FunctionalJava' + diff --git a/FunctionalJava/src/main/java/Collections.java b/FunctionalJava/src/main/java/Collections.java new file mode 100644 index 0000000..0167c79 --- /dev/null +++ b/FunctionalJava/src/main/java/Collections.java @@ -0,0 +1,55 @@ +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +/** + * Created by ilya on 10/25/16. + */ +public class Collections { + public static List map(Function1 f, Iterable iterable){ + List result = new ArrayList<>(); + iterable.forEach(param -> result.add(f.apply(param))); + return result; + } + public static List filter(Predicate f, Iterable iterable){ + List result = new ArrayList<>(); + for(Param param : iterable){ + if (f.apply(param)){ + result.add(param); + } + } + return result; + } + public static List takeWhile(Predicate f, Iterable iterable){ + List result = new ArrayList<>(); + for(Param param : iterable){ + if (f.apply(param)){ + result.add(param); + } + else{ + break; + } + } + return result; + } + public static List takeUnless(Predicate f, Iterable iterable){ + return takeWhile(f.not(), iterable); + } + public static Return foldl(Function2 f, Return ini, Iterable iterable){ + Return result = ini; + for(Return a : iterable){ + result = f.apply(result, a); + } + return result; + } + public static Return foldr(Function2 f, Return ini, Iterable iterable){ + return foldr(f, ini, iterable.iterator()); + } + private static Return foldr(Function2 f, Return ini, Iterator iterator) { + if(!iterator.hasNext()){ + return ini; + } + Return first = iterator.next(); + return f.apply(first, foldr(f, ini, iterator)); + } +} diff --git a/FunctionalJava/src/main/java/Function1.java b/FunctionalJava/src/main/java/Function1.java new file mode 100644 index 0000000..483a8a1 --- /dev/null +++ b/FunctionalJava/src/main/java/Function1.java @@ -0,0 +1,10 @@ +/** + * Created by ilya on 10/24/16. + */ +public interface Function1 { + Return apply(Param xw); + + default Function1 compose(Function1 g) { + return param -> g.apply(apply(param)); + } +} diff --git a/FunctionalJava/src/main/java/Function2.java b/FunctionalJava/src/main/java/Function2.java new file mode 100644 index 0000000..86067c8 --- /dev/null +++ b/FunctionalJava/src/main/java/Function2.java @@ -0,0 +1,20 @@ +/** + * Created by ilya on 10/25/16. + */ +public interface Function2 { + Return apply(Param1 x, Param2 y); + + default Function2 compose(Function1 g){ + return (param1, param2) -> g.apply(apply(param1, param2)); + } + default Function1 bind1(Param1 param1){ + return param2 -> apply(param1, param2); + } + default Function1 bind2(Param2 param2){ + return param1 -> apply(param1, param2); + } + default Function1> curry(){ + return this::bind1; + } +} + diff --git a/FunctionalJava/src/main/java/Predicate.java b/FunctionalJava/src/main/java/Predicate.java new file mode 100644 index 0000000..eb8c2ad --- /dev/null +++ b/FunctionalJava/src/main/java/Predicate.java @@ -0,0 +1,17 @@ +/** + * Created by ilya on 10/25/16. + */ +public interface Predicate extends Function1{ + Predicate ALWAYS_TRUE = x -> true; + Predicate ALWAYS_FALSE = x -> false; + + default Predicate not(){ + return param -> !apply(param); + } + default Predicate or(Predicate g){ + return param -> apply(param) || g.apply(param); + } + default Predicate and(Predicate g){ + return param -> apply(param) && g.apply(param); + } +} diff --git a/FunctionalJava/src/test/java/CollectionsTest.java b/FunctionalJava/src/test/java/CollectionsTest.java new file mode 100644 index 0000000..cc1df27 --- /dev/null +++ b/FunctionalJava/src/test/java/CollectionsTest.java @@ -0,0 +1,95 @@ +import org.junit.Test; + +import java.util.*; +//import java.util.Collections; + +import static org.junit.Assert.assertEquals; + +/** + * Created by ilya on 10/25/16. + */ +public class CollectionsTest { + @Test + public void mapSimple() throws Exception { + List array = Arrays.asList(1, 2, 3, 4, 5); + List squaredArray = Collections.map(xw -> xw*xw, array); + assertEquals(Arrays.asList(1, 4, 9, 16, 25), squaredArray); + } + + @Test + public void mapEmpty() throws Exception { + List array = java.util.Collections.emptyList(); + List mappedArray = Collections.map(xw -> xw*xw, array); + assertEquals(java.util.Collections.emptyList(), mappedArray); + } + + @Test + public void filterSimple() throws Exception { + List array = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); + List filteredArray = Collections.filter(xw -> xw % 2 == 0, array); + assertEquals(Arrays.asList(2, 4, 6, 8, 10), filteredArray); + } + + @Test + public void filterEmpty() throws Exception { + List array = java.util.Collections.emptyList(); + List filteredArray = Collections.filter(xw -> xw % 2 == 0, array); + assertEquals(java.util.Collections.emptyList(), filteredArray); + } + + + @Test + public void takeWhile() throws Exception { + List array = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); + List takenArray = Collections.takeWhile(xw -> xw <= 5, array); + assertEquals(Arrays.asList(1, 2, 3, 4, 5), takenArray); + } + + @Test + public void takeWhileEmpty() throws Exception { + List array = java.util.Collections.emptyList(); + List takenArray = Collections.takeWhile(xw -> xw <= 5, array); + assertEquals(java.util.Collections.emptyList(), takenArray); + } + + + @Test + public void takeUnless() throws Exception { + List array = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); + List takenArray = Collections.takeUnless(xw -> xw > 5, array); + assertEquals(Arrays.asList(1, 2, 3, 4, 5), takenArray); + } + + @Test + public void takeUnlessEmpty() throws Exception { + List array = java.util.Collections.emptyList(); + List takenArray = Collections.takeUnless(xw -> xw > 5, array); + assertEquals(java.util.Collections.emptyList(), takenArray); + } + + @Test + public void foldlSimple() throws Exception { + List array = Arrays.asList("1", "2", "3", "4", "5"); + String result = Collections.foldl(String::concat, "0", array); + assertEquals("012345", result); + } + @Test + public void foldlEmpty() throws Exception { + List array = java.util.Collections.emptyList(); + int result = Collections.foldl((x, y) -> x + y, 0, array); + assertEquals(0, result); + } + @Test + public void foldrSimple() throws Exception { + List array = Arrays.asList("1", "2", "3", "4", "5"); + String result = Collections.foldr(String::concat, "0", array); + assertEquals("123450", result); + } + @Test + public void foldrEmpty() throws Exception { + List array = java.util.Collections.emptyList(); + int result = Collections.foldr((x, y) -> x + y, 0, array); + assertEquals(0, result); + } + +} \ No newline at end of file diff --git a/FunctionalJava/src/test/java/Function1Test.java b/FunctionalJava/src/test/java/Function1Test.java new file mode 100644 index 0000000..077d3c1 --- /dev/null +++ b/FunctionalJava/src/test/java/Function1Test.java @@ -0,0 +1,18 @@ +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +/** + * Created by ilya on 10/25/16. + */ +public class Function1Test { + @Test + public void compose() throws Exception { + Function1 square = param -> param * param; + Function1 add3 = param -> param + 3; + Function1 sqrAdd3 = square.compose(add3); + + assertEquals(Integer.valueOf(12), sqrAdd3.apply(3)); + } + +} \ No newline at end of file diff --git a/FunctionalJava/src/test/java/Function2Test.java b/FunctionalJava/src/test/java/Function2Test.java new file mode 100644 index 0000000..5daecdf --- /dev/null +++ b/FunctionalJava/src/test/java/Function2Test.java @@ -0,0 +1,39 @@ +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +/** + * Created by ilya on 10/25/16. + */ +public class Function2Test { + @Test + public void compose() throws Exception { + Function2 multiply = (param1, param2) -> param1 * param2; + Function1 add2 = param -> param + 2; + Function2 multiplyAdd3 = multiply.compose(add2); + assertEquals(Integer.valueOf(12), multiplyAdd3.apply(2,5)); + } + + @Test + public void bind1() throws Exception { + Function2 multiply = (param1, param2) -> param1 * param2; + Function1 multiplyBy2 = multiply.bind1(2); + assertEquals(Integer.valueOf(12), multiplyBy2.apply(6)); + } + + @Test + public void bind2() throws Exception { + Function2 multiply = (param1, param2) -> param1 * param2; + Function1 multiplyBy2 = multiply.bind2(2); + assertEquals(Integer.valueOf(12), multiplyBy2.apply(6)); + } + + @Test + public void curry() throws Exception { + Function2 multiply = (param1, param2) -> param1 * param2; + Function1 multiplyBy2 = multiply.curry().apply(2); + assertEquals(Integer.valueOf(12), multiplyBy2.apply(6)); + + } + +} \ No newline at end of file diff --git a/FunctionalJava/src/test/java/PredicateTest.java b/FunctionalJava/src/test/java/PredicateTest.java new file mode 100644 index 0000000..a43aee0 --- /dev/null +++ b/FunctionalJava/src/test/java/PredicateTest.java @@ -0,0 +1,86 @@ +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +/** + * Created by ilya on 10/25/16. + */ +public class PredicateTest { + private static final Predicate UNDEFINED = x -> {throw new IllegalStateException();}; + private static final Predicate IS3 = x -> x == 3; + @Test + public void testConstants(){ + assertEquals(Boolean.TRUE, Predicate.ALWAYS_TRUE.apply(new Object())); + assertEquals(Boolean.FALSE, Predicate.ALWAYS_FALSE.apply(new Object())); + } + @Test + public void not() throws Exception { + assertTrue(IS3.apply(3)); + assertFalse(IS3.apply(1)); + assertFalse(IS3.apply(2)); + assertFalse(IS3.apply(4)); + Predicate isNot3 = IS3.not(); + assertFalse(isNot3.apply(3)); + assertTrue(isNot3.apply(1)); + assertTrue(isNot3.apply(2)); + assertTrue(isNot3.apply(4)); + } + + @Test + public void or() throws Exception { + Predicate is5 = param -> param == 5; + Predicate is3or5 = IS3.or(is5); + assertTrue(is3or5.apply(3)); + assertTrue(is3or5.apply(5)); + assertFalse(is3or5.apply(2)); + assertFalse(is3or5.apply(4)); + } + + @Test + public void and() throws Exception { + Predicate less6 = param -> param < 6; + Predicate greater2 = param -> param > 2; + Predicate between3and5 = less6.and(greater2); + for (int i = 3; i <= 5; i++) { + assertTrue(between3and5.apply(i)); + } + assertFalse(between3and5.apply(1)); + assertFalse(between3and5.apply(2)); + assertFalse(between3and5.apply(100)); + assertFalse(between3and5.apply(-1)); + } + + @Test + public void orLaziness() { + Predicate is3orThrow = IS3.or(UNDEFINED); + boolean thrown = false; + try{ + is3orThrow.apply(4); + } + catch (Exception e){ + thrown = true; + } + finally { + assertTrue(thrown); + } + assertTrue(is3orThrow.apply(3)); + } + + @Test + public void andLaziness() { + Predicate isNot3orThrow = IS3.and(UNDEFINED); + boolean thrown = false; + try{ + isNot3orThrow.apply(3); + } + catch (Exception e){ + thrown = true; + } + finally { + assertTrue(thrown); + } + assertFalse(isNot3orThrow.apply(4)); + } +} \ No newline at end of file