From e985f2e361f08c87d4306f9e31408c21c33d39a1 Mon Sep 17 00:00:00 2001 From: roman Date: Sun, 20 Mar 2016 20:14:40 +0300 Subject: [PATCH 1/5] task3 --- .gitignore | 4 + build.gradle | 14 ++ gradle/wrapper/gradle-wrapper.properties | 6 + gradlew | 164 +++++++++++++++++++++++ gradlew.bat | 90 +++++++++++++ settings.gradle | 2 + src/main/java/task/Collections.java | 65 +++++++++ src/main/java/task/Function1.java | 21 +++ src/main/java/task/Function2.java | 54 ++++++++ src/main/java/task/Predicate.java | 54 ++++++++ src/test/java/task/CollectionsTest.java | 54 ++++++++ src/test/java/task/Function1Test.java | 38 ++++++ src/test/java/task/Function2Test.java | 59 ++++++++ src/test/java/task/PredicateTest.java | 53 ++++++++ 14 files changed, 678 insertions(+) create mode 100644 build.gradle create mode 100644 gradle/wrapper/gradle-wrapper.properties create mode 100755 gradlew create mode 100644 gradlew.bat create mode 100644 settings.gradle create mode 100644 src/main/java/task/Collections.java create mode 100644 src/main/java/task/Function1.java create mode 100644 src/main/java/task/Function2.java create mode 100644 src/main/java/task/Predicate.java create mode 100644 src/test/java/task/CollectionsTest.java create mode 100644 src/test/java/task/Function1Test.java create mode 100644 src/test/java/task/Function2Test.java create mode 100644 src/test/java/task/PredicateTest.java 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..c33546c --- /dev/null +++ b/build.gradle @@ -0,0 +1,14 @@ +group 'org' +version '1.0-SNAPSHOT' + +apply plugin: 'java' + +repositories { + mavenCentral() +} + +dependencies { + compile 'org.jetbrains:annotations:15.0' + compile 'com.googlecode.json-simple:json-simple:1.1' + 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..f73311b --- /dev/null +++ b/src/main/java/task/Collections.java @@ -0,0 +1,65 @@ +package task; + +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; + + +public class Collections { + + public static Iterable map(Function1 f, Iterable a) { + List list = new LinkedList<>(); + for (X x : a) { + list.add(f.apply(x)); + } + return list; + } + + public static Iterable filter(Predicate p, Iterable a) { + List list = new LinkedList<>(); + for (X x : a) { + if (p.apply(x)) { + list.add(x); + } + } + return list; + } + + public static Iterable 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 Iterable takeUnless(Predicate p, Iterable a) { + List list = new LinkedList<>(); + for (X x : a) { + if (p.apply(x)) break; + list.add(x); + } + return list; + } + + public static Y foldr(Function2 f, Y base, Iterable a) { + return myFoldr(f, base, a.iterator()); + } + + 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); + } + } + + public static Y foldl(Function2 f, Y base, Iterable a) { + for (X x : a) { + base = f.apply(base, x); + } + return base; + } +} 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..8136415 --- /dev/null +++ b/src/main/java/task/Predicate.java @@ -0,0 +1,54 @@ +package task; + + +public abstract class Predicate extends Function1 { + +// public Predicate or(Predicate other) { +// return null; +// } + + 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 static Predicate ALWAYS_TRUE = new Predicate() { + @Override + public Boolean apply(Object o) { + return true; + } + }; + + public 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..1d6ce68 --- /dev/null +++ b/src/test/java/task/CollectionsTest.java @@ -0,0 +1,54 @@ +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[] arraySquared = {4, 1, 0, 1, 4}; + public static final Integer[] arrayFiltered = {0, 1, 2}; + public static final Integer[] arrayTakenWhile = {-2, -1, 0}; + public static final Integer[] arrayTakenUnless = {-2, -1}; + public static final Integer[] arrayFold = {2, 3}; + public static final Iterable list = Arrays.asList(array); + public static final Iterable listSquared = Arrays.asList(arraySquared); + public static final Iterable listFiltered = Arrays.asList(arrayFiltered); + public static final Iterable listTakenWhile = Arrays.asList(arrayTakenWhile); + public static final Iterable listTakenUnless = Arrays.asList(arrayTakenUnless); + public static final Iterable listFold = Arrays.asList(arrayFold); + + @org.junit.Test + public void testMap() throws Exception { + assertEquals(Collections.map(Function1Test.square, list), listSquared); + } + + @org.junit.Test + public void testFilter() throws Exception { + assertEquals(Collections.filter(PredicateTest.ge, list), listFiltered); + } + + @org.junit.Test + public void testTakeWhile() throws Exception { + assertEquals(Collections.takeWhile(PredicateTest.le, list), listTakenWhile); + } + + @org.junit.Test + public void testTakeUntil() throws Exception { + assertEquals(Collections.takeUnless(PredicateTest.ge, list), listTakenUnless); + } + + @org.junit.Test + public void testTakeFoldr() throws Exception { + assertEquals(Collections.foldl(Function2Test.pow, 2, listFold).intValue(), 64); + } + + @org.junit.Test + public void testTakeFoldl() throws Exception { + assertEquals(Collections.foldl(Function2Test.pow, 2, listFold).intValue(), 64); + } + + +} diff --git a/src/test/java/task/Function1Test.java b/src/test/java/task/Function1Test.java new file mode 100644 index 0000000..d900d8c --- /dev/null +++ b/src/test/java/task/Function1Test.java @@ -0,0 +1,38 @@ +package task; + +import static org.junit.Assert.*; + + +public class Function1Test { + + public static final Function1 succ = new Function1() { + @Override + public Integer apply(Integer x) { + return x + 1; + } + }; + + public static final Function1 square = new Function1() { + @Override + public Integer apply(Integer x) { + return x * 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..e876751 --- /dev/null +++ b/src/test/java/task/Function2Test.java @@ -0,0 +1,59 @@ +package task; + + +import static java.lang.Math.pow; +import static org.junit.Assert.assertEquals; + +public class Function2Test { + + public static final Function2 add = new Function2() { + @Override + public Integer apply(Integer x, Integer y) { + return x + y; + } + }; + + public static final Function2 mul = new Function2() { + @Override + public Integer apply(Integer x, Integer y) { + return x * y; + } + }; + + public static final Function2 pow = new Function2() { + @Override + public Integer apply(Integer x, Integer y) { + return (int) Math.round(Math.pow(x, 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..ffc11d4 --- /dev/null +++ b/src/test/java/task/PredicateTest.java @@ -0,0 +1,53 @@ +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 { + assertTrue(ge.or(le).apply(1)); + assertTrue(le.or(ge).apply(0)); + } + + @org.junit.Test + public void testAnd() throws Exception { + assertTrue(ge.and(le).apply(0)); + assertFalse(le.and(ge).apply(1)); + } + + @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)); + } + +} From 9d8368c3b631544cded75f0e25be57c49741d8a0 Mon Sep 17 00:00:00 2001 From: roman Date: Mon, 4 Apr 2016 22:41:40 +0300 Subject: [PATCH 2/5] fixed --- build.gradle | 2 - src/main/java/task/Collections.java | 29 ++++++-------- src/main/java/task/Predicate.java | 4 -- src/test/java/task/CollectionsTest.java | 38 +++++++++--------- src/test/java/task/Function1Test.java | 18 +++++---- src/test/java/task/Function2Test.java | 40 +++++++++++-------- src/test/java/task/PredicateTest.java | 53 ++++++++++++++++++------- 7 files changed, 103 insertions(+), 81 deletions(-) diff --git a/build.gradle b/build.gradle index c33546c..1c437fa 100644 --- a/build.gradle +++ b/build.gradle @@ -8,7 +8,5 @@ repositories { } dependencies { - compile 'org.jetbrains:annotations:15.0' - compile 'com.googlecode.json-simple:json-simple:1.1' testCompile group: 'junit', name: 'junit', version: '4.12' } diff --git a/src/main/java/task/Collections.java b/src/main/java/task/Collections.java index f73311b..fc810c5 100644 --- a/src/main/java/task/Collections.java +++ b/src/main/java/task/Collections.java @@ -7,7 +7,7 @@ public class Collections { - public static Iterable map(Function1 f, Iterable a) { + public static List map(Function1 f, Iterable a) { List list = new LinkedList<>(); for (X x : a) { list.add(f.apply(x)); @@ -15,7 +15,7 @@ public static Iterable map(Function1 f, Iterable a) { return list; } - public static Iterable filter(Predicate p, Iterable a) { + public static List filter(Predicate p, Iterable a) { List list = new LinkedList<>(); for (X x : a) { if (p.apply(x)) { @@ -25,7 +25,7 @@ public static Iterable filter(Predicate p, Iterable a) { return list; } - public static Iterable takeWhile(Predicate p, Iterable a) { + public static List takeWhile(Predicate p, Iterable a) { List list = new LinkedList<>(); for (X x : a) { if (!p.apply(x)) break; @@ -34,19 +34,21 @@ public static Iterable takeWhile(Predicate p, Iterable a) { return list; } - public static Iterable takeUnless(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; @@ -55,11 +57,4 @@ private static Y myFoldr(Function2 f, return f.apply(myFoldr(f, base, a), x); } } - - public static Y foldl(Function2 f, Y base, Iterable a) { - for (X x : a) { - base = f.apply(base, x); - } - return base; - } } diff --git a/src/main/java/task/Predicate.java b/src/main/java/task/Predicate.java index 8136415..686500e 100644 --- a/src/main/java/task/Predicate.java +++ b/src/main/java/task/Predicate.java @@ -3,10 +3,6 @@ public abstract class Predicate extends Function1 { -// public Predicate or(Predicate other) { -// return null; -// } - public Predicate or(Predicate other) { Predicate self = this; return new Predicate() { diff --git a/src/test/java/task/CollectionsTest.java b/src/test/java/task/CollectionsTest.java index 1d6ce68..6e60cda 100644 --- a/src/test/java/task/CollectionsTest.java +++ b/src/test/java/task/CollectionsTest.java @@ -7,47 +7,49 @@ public class CollectionsTest { - public static final Integer[] array = {-2, -1, 0, 1, 2}; - public static final Integer[] arraySquared = {4, 1, 0, 1, 4}; - public static final Integer[] arrayFiltered = {0, 1, 2}; - public static final Integer[] arrayTakenWhile = {-2, -1, 0}; - public static final Integer[] arrayTakenUnless = {-2, -1}; - public static final Integer[] arrayFold = {2, 3}; - public static final Iterable list = Arrays.asList(array); - public static final Iterable listSquared = Arrays.asList(arraySquared); - public static final Iterable listFiltered = Arrays.asList(arrayFiltered); - public static final Iterable listTakenWhile = Arrays.asList(arrayTakenWhile); - public static final Iterable listTakenUnless = Arrays.asList(arrayTakenUnless); - public static final Iterable listFold = Arrays.asList(arrayFold); + 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), listSquared); + assertEquals(Collections.map(Function1Test.SQUARE, LIST), LIST_SQUARED); } @org.junit.Test public void testFilter() throws Exception { - assertEquals(Collections.filter(PredicateTest.ge, list), listFiltered); + assertEquals(Collections.filter(PredicateTest.GE, LIST), LIST_FILTERED); } @org.junit.Test public void testTakeWhile() throws Exception { - assertEquals(Collections.takeWhile(PredicateTest.le, list), listTakenWhile); + assertEquals(Collections.takeWhile(PredicateTest.LE, LIST), LIST_TAKEN_WHILE); } @org.junit.Test public void testTakeUntil() throws Exception { - assertEquals(Collections.takeUnless(PredicateTest.ge, list), listTakenUnless); + assertEquals(Collections.takeUnless(PredicateTest.GE, LIST), LIST_TAKEN_UNLESS); } @org.junit.Test public void testTakeFoldr() throws Exception { - assertEquals(Collections.foldl(Function2Test.pow, 2, listFold).intValue(), 64); + Number res = Collections.foldr(Function2Test.SND, 1, LIST_FOLD); + assertEquals(res, new Integer(2)); } @org.junit.Test public void testTakeFoldl() throws Exception { - assertEquals(Collections.foldl(Function2Test.pow, 2, listFold).intValue(), 64); + 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 index d900d8c..9c9699e 100644 --- a/src/test/java/task/Function1Test.java +++ b/src/test/java/task/Function1Test.java @@ -1,18 +1,20 @@ package task; +import java.util.Objects; + import static org.junit.Assert.*; public class Function1Test { - public static final Function1 succ = new Function1() { + public static final Function1 SUCC = new Function1() { @Override - public Integer apply(Integer x) { - return x + 1; + public Integer apply(Object x) { + return (Integer) x + 1; } }; - public static final Function1 square = new Function1() { + public static final Function1 SQUARE = new Function1() { @Override public Integer apply(Integer x) { return x * x; @@ -21,14 +23,14 @@ public Integer apply(Integer x) { @org.junit.Test public void testApply() throws Exception { - assertEquals(succ.apply(1).intValue(), 2); - assertEquals(square.apply(2).intValue(), 4); + 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); + 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 index e876751..fd64bac 100644 --- a/src/test/java/task/Function2Test.java +++ b/src/test/java/task/Function2Test.java @@ -1,42 +1,48 @@ package task; -import static java.lang.Math.pow; import static org.junit.Assert.assertEquals; public class Function2Test { - public static final Function2 add = new Function2() { + public static final Function2 ADD = new Function2() { @Override - public Integer apply(Integer x, Integer y) { - return x + y; + public Integer apply(Object x, Object y) { + return (Integer) x + (Integer) y; } }; - public static final Function2 mul = new Function2() { + public static final Function2 MUL = new Function2() { @Override - public Integer apply(Integer x, Integer y) { - return x * y; + public Integer apply(Object x, Object y) { + return (Integer) x * (Integer) y; } }; - public static final Function2 pow = new Function2() { + public static final Function2 SND = new Function2() { @Override - public Integer apply(Integer x, Integer y) { - return (int) Math.round(Math.pow(x, y)); + 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); + 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); + 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); @@ -44,15 +50,15 @@ public void testCompose() throws Exception { @org.junit.Test public void testBind() throws Exception { - Function1 f = pow.bind1(2); - Function1 g = pow.bind2(2); + 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(); + 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 index ffc11d4..cf851f3 100644 --- a/src/test/java/task/PredicateTest.java +++ b/src/test/java/task/PredicateTest.java @@ -1,47 +1,70 @@ package task; + import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; public class PredicateTest { - public static final Predicate ge = new Predicate() { + public static final Predicate GE = new Predicate() { @Override - public Boolean apply(Integer x) { - return x >= 0; + public Boolean apply(Object x) { + return (Integer) x >= 0; } }; - public static final Predicate le = new Predicate() { + public static final Predicate LE = new Predicate() { @Override - public Boolean apply(Integer x) { - return x <= 0; + public Boolean apply(Object x) { + return (Integer) x <= 0; } }; + + private class Indicator extends Predicate { + + private Boolean applied = false; + + public Boolean wasApplied() { + return applied; + } + + @Override + public Boolean apply(Object o) { + applied = true; + return true; + } + } + + ; + @org.junit.Test public void testApply() throws Exception { - assertTrue(ge.apply(1)); - assertFalse(le.apply(1)); + assertTrue(GE.apply(1)); + assertFalse(LE.apply(1)); } @org.junit.Test public void testOr() throws Exception { - assertTrue(ge.or(le).apply(1)); - assertTrue(le.or(ge).apply(0)); + 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)); - assertFalse(le.and(ge).apply(1)); + 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)); + assertTrue(GE.not().apply(-1)); + assertFalse(GE.not().apply(0)); + assertFalse(GE.not().apply(1)); } @org.junit.Test From 5f3fe3eff8b28d79ca9c2c02b50a0f13578cd47b Mon Sep 17 00:00:00 2001 From: roman Date: Mon, 4 Apr 2016 22:44:15 +0300 Subject: [PATCH 3/5] small fixed --- src/test/java/task/PredicateTest.java | 33 ++++++++++++--------------- 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/src/test/java/task/PredicateTest.java b/src/test/java/task/PredicateTest.java index cf851f3..8684318 100644 --- a/src/test/java/task/PredicateTest.java +++ b/src/test/java/task/PredicateTest.java @@ -20,24 +20,6 @@ public Boolean apply(Object x) { } }; - - private class Indicator extends Predicate { - - private Boolean applied = false; - - public Boolean wasApplied() { - return applied; - } - - @Override - public Boolean apply(Object o) { - applied = true; - return true; - } - } - - ; - @org.junit.Test public void testApply() throws Exception { assertTrue(GE.apply(1)); @@ -73,4 +55,19 @@ public void testAlways() throws Exception { 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; + } + } + } From 9f5cdbd4c5012b6ae663c57055bcf585b669f646 Mon Sep 17 00:00:00 2001 From: roman Date: Tue, 5 Apr 2016 19:31:54 +0300 Subject: [PATCH 4/5] another fix --- src/main/java/task/Predicate.java | 4 ++-- src/test/java/task/Function1Test.java | 9 ++++----- src/test/java/task/PredicateTest.java | 13 +++++++------ 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/main/java/task/Predicate.java b/src/main/java/task/Predicate.java index 686500e..8ebdf92 100644 --- a/src/main/java/task/Predicate.java +++ b/src/main/java/task/Predicate.java @@ -33,14 +33,14 @@ public Boolean apply(X x) { }; } - public static Predicate ALWAYS_TRUE = new Predicate() { + public final static Predicate ALWAYS_TRUE = new Predicate() { @Override public Boolean apply(Object o) { return true; } }; - public static Predicate ALWAYS_FALSE = new Predicate() { + public final static Predicate ALWAYS_FALSE = new Predicate() { @Override public Boolean apply(Object o) { return false; diff --git a/src/test/java/task/Function1Test.java b/src/test/java/task/Function1Test.java index 9c9699e..417336c 100644 --- a/src/test/java/task/Function1Test.java +++ b/src/test/java/task/Function1Test.java @@ -1,6 +1,5 @@ package task; -import java.util.Objects; import static org.junit.Assert.*; @@ -14,10 +13,10 @@ public Integer apply(Object x) { } }; - public static final Function1 SQUARE = new Function1() { + public static final Function1 SQUARE = new Function1() { @Override - public Integer apply(Integer x) { - return x * x; + public Integer apply(Object x) { + return (Integer) x * (Integer) x; } }; @@ -30,7 +29,7 @@ public void testApply() throws Exception { @org.junit.Test public void testCompose() throws Exception { Function1 f = SUCC.compose(SQUARE); - Function1 g = SQUARE.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/PredicateTest.java b/src/test/java/task/PredicateTest.java index 8684318..a7279a0 100644 --- a/src/test/java/task/PredicateTest.java +++ b/src/test/java/task/PredicateTest.java @@ -6,20 +6,21 @@ public class PredicateTest { - public static final Predicate GE = new Predicate() { + public static final Predicate GE = new Predicate() { @Override - public Boolean apply(Object x) { - return (Integer) x >= 0; + public Boolean apply(Integer x) { + return x >= 0; } }; - public static final Predicate LE = new Predicate() { + public static final Predicate LE = new Predicate() { @Override - public Boolean apply(Object x) { - return (Integer) x <= 0; + public Boolean apply(Integer x) { + return x <= 0; } }; + @org.junit.Test public void testApply() throws Exception { assertTrue(GE.apply(1)); From f03d09beea94fb3188af9f0c3a8cc2900bf02a1b Mon Sep 17 00:00:00 2001 From: roman Date: Tue, 5 Apr 2016 20:20:40 +0300 Subject: [PATCH 5/5] another small fix --- src/main/java/task/Collections.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/task/Collections.java b/src/main/java/task/Collections.java index fc810c5..154a45b 100644 --- a/src/main/java/task/Collections.java +++ b/src/main/java/task/Collections.java @@ -38,18 +38,18 @@ public static List takeUnless(Predicate p, Iterable a) { return takeWhile(p.not(), a); } - public static Y foldr(Function2 f, Y base, Iterable 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) { + 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) { + private static Y myFoldr(Function2 f, Y base, Iterator a) { if (!a.hasNext()) { return base; } else {