diff --git a/.gitignore b/.gitignore index 32858aa..162f98c 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,10 @@ # Mobile Tools for Java (J2ME) .mtj.tmp/ +*.iml +build/* +.idea/* + # Package Files # *.jar *.war diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..1c437fa --- /dev/null +++ b/build.gradle @@ -0,0 +1,12 @@ +group 'org' +version '1.0-SNAPSHOT' + +apply plugin: 'java' + +repositories { + mavenCentral() +} + +dependencies { + testCompile group: 'junit', name: 'junit', version: '4.12' +} diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..0c19db4 --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +#Sun Feb 14 15:03: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.5-bin.zip diff --git a/gradlew b/gradlew new file mode 100755 index 0000000..91a7e26 --- /dev/null +++ b/gradlew @@ -0,0 +1,164 @@ +#!/usr/bin/env bash + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS="" + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# 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 +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; +esac + +# For Cygwin, ensure paths are in UNIX format before anything is touched. +if $cygwin ; then + [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` +fi + +# 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\"`/" >&- +APP_HOME="`pwd -P`" +cd "$SAVED" >&- + +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" ] ; 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"` + + # 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/gradlew.bat b/gradlew.bat new file mode 100644 index 0000000..aec9973 --- /dev/null +++ b/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 + +@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= + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@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 Windowz 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/settings.gradle b/settings.gradle new file mode 100644 index 0000000..99a2019 --- /dev/null +++ b/settings.gradle @@ -0,0 +1,2 @@ +rootProject.name = 'task' + diff --git a/src/main/java/task/Collections.java b/src/main/java/task/Collections.java new file mode 100644 index 0000000..154a45b --- /dev/null +++ b/src/main/java/task/Collections.java @@ -0,0 +1,60 @@ +package task; + +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; + + +public class Collections { + + public static List map(Function1 f, Iterable a) { + List list = new LinkedList<>(); + for (X x : a) { + list.add(f.apply(x)); + } + return list; + } + + public static List filter(Predicate p, Iterable a) { + List list = new LinkedList<>(); + for (X x : a) { + if (p.apply(x)) { + list.add(x); + } + } + return list; + } + + public static List takeWhile(Predicate p, Iterable a) { + List list = new LinkedList<>(); + for (X x : a) { + if (!p.apply(x)) break; + list.add(x); + } + return list; + } + + public static List takeUnless(Predicate p, Iterable a) { + return takeWhile(p.not(), a); + } + + public static Y foldr(Function2 f, Y base, Iterable a) { + return myFoldr(f, base, a.iterator()); + } + + public static Y foldl(Function2 f, Y base, Iterable a) { + for (X x : a) { + base = f.apply(base, x); + } + return base; + } + + private static Y myFoldr(Function2 f, Y base, Iterator a) { + if (!a.hasNext()) { + return base; + } else { + X x = a.next(); + return f.apply(myFoldr(f, base, a), x); + } + } +} diff --git a/src/main/java/task/Function1.java b/src/main/java/task/Function1.java new file mode 100644 index 0000000..177a9a8 --- /dev/null +++ b/src/main/java/task/Function1.java @@ -0,0 +1,21 @@ +package task; + + +public abstract class Function1 { + public abstract Y apply(X x); + + public Function1 compose(Function1 other) { + Function1 self = this; + return new Function1() { + private final Function1 f = self; + private final Function1 g = other; + + @Override + public Z apply(X x) { + return g.apply(f.apply(x)); + } + }; + } + + +} diff --git a/src/main/java/task/Function2.java b/src/main/java/task/Function2.java new file mode 100644 index 0000000..bbf1ccc --- /dev/null +++ b/src/main/java/task/Function2.java @@ -0,0 +1,54 @@ +package task; + + +public abstract class Function2 { + public abstract Z apply(X x, Y y); + + public Function2 compose(Function1 other) { + Function2 self = this; + return new Function2() { + private Function1 f = other; + private Function2 g = self; + + @Override + public Z1 apply(X x, Y y) { + return f.apply(g.apply(x, y)); + } + }; + } + + public Function1 bind1(X x) { + Function2 self = this; + return new Function1() { + @Override + public Z apply(Y y) { + return self.apply(x, y); + } + }; + } + + public Function1 bind2(Y y) { + Function2 self = this; + return new Function1() { + @Override + public Z apply(X x) { + return self.apply(x, y); + } + }; + } + + public Function1> carry() { + Function2 self = this; + return new Function1>() { + @Override + public Function1 apply(X x) { + return new Function1() { + @Override + public Z apply(Y y) { + return self.apply(x, y); + } + }; + } + }; + } +} diff --git a/src/main/java/task/Predicate.java b/src/main/java/task/Predicate.java new file mode 100644 index 0000000..8ebdf92 --- /dev/null +++ b/src/main/java/task/Predicate.java @@ -0,0 +1,50 @@ +package task; + + +public abstract class Predicate extends Function1 { + + public Predicate or(Predicate other) { + Predicate self = this; + return new Predicate() { + @Override + public Boolean apply(X x) { + return self.apply(x) || other.apply(x); + } + }; + } + + public Predicate and(Predicate other) { + Predicate self = this; + return new Predicate() { + @Override + public Boolean apply(X x) { + return self.apply(x) && other.apply(x); + } + }; + } + + public Predicate not() { + Predicate self = this; + return new Predicate() { + @Override + public Boolean apply(X x) { + return !self.apply(x); + } + }; + } + + public final static Predicate ALWAYS_TRUE = new Predicate() { + @Override + public Boolean apply(Object o) { + return true; + } + }; + + public final static Predicate ALWAYS_FALSE = new Predicate() { + @Override + public Boolean apply(Object o) { + return false; + } + }; + +} diff --git a/src/test/java/task/CollectionsTest.java b/src/test/java/task/CollectionsTest.java new file mode 100644 index 0000000..6e60cda --- /dev/null +++ b/src/test/java/task/CollectionsTest.java @@ -0,0 +1,56 @@ +package task; + +import java.util.Arrays; + +import static org.junit.Assert.assertEquals; + + +public class CollectionsTest { + + public static final Integer[] ARRAY = {-2, -1, 0, 1, 2}; + public static final Integer[] ARRAY_SQUARED = {4, 1, 0, 1, 4}; + public static final Integer[] ARRAY_FILTERED = {0, 1, 2}; + public static final Integer[] ARRAY_TAKEN_WHILE = {-2, -1, 0}; + public static final Integer[] ARRAY_TAKEN_UNLESS = {-2, -1}; + public static final Integer[] ARRAY_FOLD = {2, 3}; + public static final Iterable LIST = Arrays.asList(ARRAY); + public static final Iterable LIST_SQUARED = Arrays.asList(ARRAY_SQUARED); + public static final Iterable LIST_FILTERED = Arrays.asList(ARRAY_FILTERED); + public static final Iterable LIST_TAKEN_WHILE = Arrays.asList(ARRAY_TAKEN_WHILE); + public static final Iterable LIST_TAKEN_UNLESS = Arrays.asList(ARRAY_TAKEN_UNLESS); + public static final Iterable LIST_FOLD = Arrays.asList(ARRAY_FOLD); + + @org.junit.Test + public void testMap() throws Exception { + assertEquals(Collections.map(Function1Test.SQUARE, LIST), LIST_SQUARED); + } + + @org.junit.Test + public void testFilter() throws Exception { + assertEquals(Collections.filter(PredicateTest.GE, LIST), LIST_FILTERED); + } + + @org.junit.Test + public void testTakeWhile() throws Exception { + assertEquals(Collections.takeWhile(PredicateTest.LE, LIST), LIST_TAKEN_WHILE); + } + + @org.junit.Test + public void testTakeUntil() throws Exception { + assertEquals(Collections.takeUnless(PredicateTest.GE, LIST), LIST_TAKEN_UNLESS); + } + + @org.junit.Test + public void testTakeFoldr() throws Exception { + Number res = Collections.foldr(Function2Test.SND, 1, LIST_FOLD); + assertEquals(res, new Integer(2)); + } + + @org.junit.Test + public void testTakeFoldl() throws Exception { + Number res = Collections.foldl(Function2Test.SND, 1, LIST_FOLD); + assertEquals(res, new Integer(3)); + } + + +} diff --git a/src/test/java/task/Function1Test.java b/src/test/java/task/Function1Test.java new file mode 100644 index 0000000..417336c --- /dev/null +++ b/src/test/java/task/Function1Test.java @@ -0,0 +1,39 @@ +package task; + + +import static org.junit.Assert.*; + + +public class Function1Test { + + public static final Function1 SUCC = new Function1() { + @Override + public Integer apply(Object x) { + return (Integer) x + 1; + } + }; + + public static final Function1 SQUARE = new Function1() { + @Override + public Integer apply(Object x) { + return (Integer) x * (Integer) x; + } + }; + + @org.junit.Test + public void testApply() throws Exception { + assertEquals(SUCC.apply(1).intValue(), 2); + assertEquals(SQUARE.apply(2).intValue(), 4); + } + + @org.junit.Test + public void testCompose() throws Exception { + Function1 f = SUCC.compose(SQUARE); + Function1 g = SQUARE.compose(SQUARE); + assertEquals(f.apply(1).intValue(), 4); + assertEquals(f.apply(2).intValue(), 9); + assertEquals(g.apply(2).intValue(), 16); + } + + +} diff --git a/src/test/java/task/Function2Test.java b/src/test/java/task/Function2Test.java new file mode 100644 index 0000000..fd64bac --- /dev/null +++ b/src/test/java/task/Function2Test.java @@ -0,0 +1,65 @@ +package task; + + +import static org.junit.Assert.assertEquals; + +public class Function2Test { + + public static final Function2 ADD = new Function2() { + @Override + public Integer apply(Object x, Object y) { + return (Integer) x + (Integer) y; + } + }; + + public static final Function2 MUL = new Function2() { + @Override + public Integer apply(Object x, Object y) { + return (Integer) x * (Integer) y; + } + }; + + public static final Function2 SND = new Function2() { + @Override + public Integer apply(Object x, Object y) { + return (Integer) y; + } + }; + + public static final Function2 POW = new Function2() { + @Override + public Integer apply(Object x, Object y) { + return (int) Math.round(Math.pow((Integer)x, (Integer)y)); + } + }; + + @org.junit.Test + public void testApply() throws Exception { + assertEquals(ADD.apply(1, 3).intValue(), 4); + assertEquals(MUL.apply(2, 5).intValue(), 10); + } + + @org.junit.Test + public void testCompose() throws Exception { + Function2 f = ADD.compose(Function1Test.SQUARE); + Function2 g = MUL.compose(Function1Test.SUCC); + assertEquals(f.apply(1, 2).intValue(), 9); + assertEquals(f.apply(2, 3).intValue(), 25); + assertEquals(g.apply(2, 4).intValue(), 9); + } + + @org.junit.Test + public void testBind() throws Exception { + Function1 f = POW.bind1(2); + Function1 g = POW.bind2(2); + assertEquals(f.apply(3).intValue(), 8); + assertEquals(g.apply(3).intValue(), 9); + } + + @org.junit.Test + public void testCarry() throws Exception { + Function1> carried = POW.carry(); + assertEquals(carried.apply(2).apply(3).intValue(), 8); + assertEquals(carried.apply(3).apply(2).intValue(), 9); + } +} diff --git a/src/test/java/task/PredicateTest.java b/src/test/java/task/PredicateTest.java new file mode 100644 index 0000000..a7279a0 --- /dev/null +++ b/src/test/java/task/PredicateTest.java @@ -0,0 +1,74 @@ +package task; + + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class PredicateTest { + + public static final Predicate GE = new Predicate() { + @Override + public Boolean apply(Integer x) { + return x >= 0; + } + }; + + public static final Predicate LE = new Predicate() { + @Override + public Boolean apply(Integer x) { + return x <= 0; + } + }; + + + @org.junit.Test + public void testApply() throws Exception { + assertTrue(GE.apply(1)); + assertFalse(LE.apply(1)); + } + + @org.junit.Test + public void testOr() throws Exception { + Indicator indicator = new Indicator(); + assertTrue(GE.or(indicator).apply(1)); + assertFalse(indicator.wasApplied()); + assertTrue(LE.or(GE).apply(0)); + } + + @org.junit.Test + public void testAnd() throws Exception { + assertTrue(GE.and(LE).apply(0)); + Indicator indicator = new Indicator(); + assertFalse(LE.and(indicator).apply(1)); + assertFalse(indicator.wasApplied()); + } + + @org.junit.Test + public void testNot() throws Exception { + assertTrue(GE.not().apply(-1)); + assertFalse(GE.not().apply(0)); + assertFalse(GE.not().apply(1)); + } + + @org.junit.Test + public void testAlways() throws Exception { + assertTrue(Predicate.ALWAYS_TRUE.apply(1)); + assertFalse(Predicate.ALWAYS_FALSE.apply(true)); + } + + private class Indicator extends Predicate { + + private Boolean applied = false; + + public Boolean wasApplied() { + return applied; + } + + @Override + public Boolean apply(Object o) { + applied = true; + return true; + } + } + +}