├── settings.gradle ├── img ├── 1.png ├── 2.png └── 3.png ├── gradle └── wrapper │ └── gradle-wrapper.properties ├── .gitignore ├── gradlew.bat ├── README_CN.md ├── README.md ├── gradlew ├── src ├── test │ └── java │ │ └── com │ │ └── library │ │ └── tangxiaolv │ │ └── hf │ │ └── HFuncTests.java └── main │ └── java │ └── com │ └── library │ └── tangxiaolv │ └── hf │ └── HFunc.java └── LICENSE /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'hfunc' 2 | 3 | -------------------------------------------------------------------------------- /img/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TangXiaoLv/HFunc/HEAD/img/1.png -------------------------------------------------------------------------------- /img/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TangXiaoLv/HFunc/HEAD/img/2.png -------------------------------------------------------------------------------- /img/3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TangXiaoLv/HFunc/HEAD/img/3.png -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Thu Dec 22 00:07:51 CST 2016 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.13-all.zip 7 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | 3 | # Mobile Tools for Java (J2ME) 4 | .mtj.tmp/ 5 | 6 | # Package Files # 7 | *.jar 8 | *.war 9 | *.ear 10 | 11 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 12 | hs_err_pid* 13 | .idea/ 14 | .gradle/ 15 | /build/publications/MyPublication/pom-default.xml 16 | /build/tmp/jar/MANIFEST.MF 17 | /*.iml 18 | /gradle.properties 19 | /local.properties 20 | /build 21 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /README_CN.md: -------------------------------------------------------------------------------- 1 | # HFunc 2 | [ ![Download](https://api.bintray.com/packages/tangxiaolv/maven/hfunc/images/download.svg) ](https://bintray.com/tangxiaolv/maven/hfunc/_latestVersion) 3 | 4 | 中文 | [English](https://github.com/TangXiaoLv/HFunc/blob/master/README.md) 5 | 6 | 一个快速简单轻量级高阶函数库,支持串行,并行计算。适用于Java,Android。 7 | Support 8 | --- 9 | + map 10 | + filter 11 | + reduce 12 | 13 | Gradle 14 | --- 15 | dependencies { 16 | compile 'com.library.tangxiaolv:hfunc:1.0.1' 17 | } 18 | 19 | 20 | Guide 21 | --- 22 | 示例数据集: 23 | List c = new ArrayList<>(); 24 | for (int i = 1; i < 101; i++) { 25 | c.add(i); 26 | } 27 | 28 | [**map:**](https://research.google.com/archive/mapreduce.html) 29 | 30 | 31 | 32 | ``` 33 | 串行计算: 1078ms 34 | List result = HFunc.map(c, new HFunc.Func1() { 35 | @Override 36 | public String call(Integer item) { 37 | try { 38 | Thread.sleep(10); 39 | } catch (InterruptedException e) {} 40 | return Integer.toString(item * 2); 41 | } 42 | }); 43 | 44 | 45 | 并行计算: 150ms 46 | List result = HFunc.mapParallel(c, new HFunc.Func1() { 47 | @Override 48 | public String call(Integer item) { 49 | try { 50 | Thread.sleep(10); 51 | } catch (InterruptedException e) {} 52 | return Integer.toString(item * 2); 53 | } 54 | }); 55 | ``` 56 | 57 | **filter:** 58 | 59 | 60 | 61 | ``` 62 | 串行计算: 1037ms 63 | List result = HFunc.filter(c, new HFunc.Func1() { 64 | @Override 65 | public Boolean call(Integer item) { 66 | try { 67 | Thread.sleep(10); 68 | } catch (InterruptedException e) {} 69 | return item % 2 != 0; 70 | } 71 | }); 72 | 73 | 并行计算: 159ms 74 | List result = HFunc.filterParallel(c, new HFunc.Func1() { 75 | @Override 76 | public Boolean call(Integer item) { 77 | try { 78 | Thread.sleep(10); 79 | } catch (InterruptedException e) {} 80 | return item % 2 != 0; 81 | } 82 | }); 83 | ``` 84 | 85 | [**reduce:**](https://research.google.com/archive/mapreduce.html) 86 | 87 | 88 | 89 | ``` 90 | 串行计算: 1061ms 91 | Integer result = HFunc.reduce(c, new HFunc.Func2() { 92 | @Override 93 | public Integer call(Integer merge, Integer next) { 94 | try { 95 | Thread.sleep(10); 96 | } catch (InterruptedException e) {} 97 | return merge + next; 98 | } 99 | }); 100 | 101 | 并行计算: 239ms 102 | Integer result = HFunc.reduceParallel(c, new HFunc.Func2() { 103 | @Override 104 | public Integer call(Integer merge, Integer next) { 105 | try { 106 | Thread.sleep(10); 107 | } catch (InterruptedException e) {} 108 | return merge + next; 109 | } 110 | }); 111 | ``` 112 | 113 | LICENSE 114 | --- 115 | 116 | Copyright 2016 XiaoLv Tang 117 | 118 | Licensed under the Apache License, Version 2.0 (the "License"); 119 | you may not use this file except in compliance with the License. 120 | You may obtain a copy of the License at 121 | 122 | http://www.apache.org/licenses/LICENSE-2.0 123 | 124 | Unless required by applicable law or agreed to in writing, software 125 | distributed under the License is distributed on an "AS IS" BASIS, 126 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 127 | See the License for the specific language governing permissions and 128 | limitations under the License. 129 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # HFunc 2 | [ ![Download](https://api.bintray.com/packages/tangxiaolv/maven/hfunc/images/download.svg) ](https://bintray.com/tangxiaolv/maven/hfunc/_latestVersion) 3 | 4 | English | [中文](https://github.com/TangXiaoLv/HFunc/blob/master/README_CN.md) 5 | 6 | A fast and simple Java Higher-order function lib. Support serial compute and parallel compute. 7 | Applies to Java and Android. 8 | 9 | Support 10 | --- 11 | + map 12 | + filter 13 | + reduce 14 | 15 | Gradle 16 | --- 17 | dependencies { 18 | compile 'com.library.tangxiaolv:hfunc:1.0.1' 19 | } 20 | 21 | Guide 22 | --- 23 | Example: 24 | //data collection 25 | List c = new ArrayList<>(); 26 | for (int i = 1; i < 101; i++) { 27 | c.add(i); 28 | } 29 | 30 | [**map:**](https://research.google.com/archive/mapreduce.html) 31 | 32 | 33 | 34 | ``` 35 | serial compute: 1078ms 36 | List result = HFunc.map(c, new HFunc.Func1() { 37 | @Override 38 | public String call(Integer item) { 39 | try { 40 | Thread.sleep(10); 41 | } catch (InterruptedException e) {} 42 | return Integer.toString(item * 2); 43 | } 44 | }); 45 | 46 | parallel compute: 150ms 47 | List result = HFunc.mapParallel(c, new HFunc.Func1() { 48 | @Override 49 | public String call(Integer item) { 50 | try { 51 | Thread.sleep(10); 52 | } catch (InterruptedException e) {} 53 | return Integer.toString(item * 2); 54 | } 55 | }); 56 | ``` 57 | 58 | **filter:** 59 | 60 | 61 | 62 | ``` 63 | serial compute: 1037ms 64 | List result = HFunc.filter(c, new HFunc.Func1() { 65 | @Override 66 | public Boolean call(Integer item) { 67 | try { 68 | Thread.sleep(10); 69 | } catch (InterruptedException e) {} 70 | return item % 2 != 0; 71 | } 72 | }); 73 | 74 | parallel compute: 159ms 75 | List result = HFunc.filterParallel(c, new HFunc.Func1() { 76 | @Override 77 | public Boolean call(Integer item) { 78 | try { 79 | Thread.sleep(10); 80 | } catch (InterruptedException e) {} 81 | return item % 2 != 0; 82 | } 83 | }); 84 | ``` 85 | 86 | [**reduce:**](https://research.google.com/archive/mapreduce.html) 87 | 88 | 89 | 90 | ``` 91 | serial compute: 1061ms 92 | Integer result = HFunc.reduce(c, new HFunc.Func2() { 93 | @Override 94 | public Integer call(Integer merge, Integer next) { 95 | try { 96 | Thread.sleep(10); 97 | } catch (InterruptedException e) {} 98 | return merge + next; 99 | } 100 | }); 101 | 102 | parallel compute: 239ms 103 | Integer result = HFunc.reduceParallel(c, new HFunc.Func2() { 104 | @Override 105 | public Integer call(Integer merge, Integer next) { 106 | try { 107 | Thread.sleep(10); 108 | } catch (InterruptedException e) {} 109 | return merge + next; 110 | } 111 | }); 112 | ``` 113 | 114 | LICENSE 115 | --- 116 | 117 | Copyright 2016 XiaoLv Tang 118 | 119 | Licensed under the Apache License, Version 2.0 (the "License"); 120 | you may not use this file except in compliance with the License. 121 | You may obtain a copy of the License at 122 | 123 | http://www.apache.org/licenses/LICENSE-2.0 124 | 125 | Unless required by applicable law or agreed to in writing, software 126 | distributed under the License is distributed on an "AS IS" BASIS, 127 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 128 | See the License for the specific language governing permissions and 129 | limitations under the License. 130 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn ( ) { 37 | echo "$*" 38 | } 39 | 40 | die ( ) { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 158 | function splitJvmOpts() { 159 | JVM_OPTS=("$@") 160 | } 161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 163 | 164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 165 | -------------------------------------------------------------------------------- /src/test/java/com/library/tangxiaolv/hf/HFuncTests.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014 Netflix, Inc. 3 | *

4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | *

8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | *

10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.library.tangxiaolv.hf; 17 | 18 | 19 | import org.junit.Test; 20 | 21 | import java.util.ArrayList; 22 | import java.util.List; 23 | 24 | public class HFuncTests { 25 | 26 | @Test 27 | public void map() throws Exception { 28 | List c = new ArrayList<>(); 29 | for (int i = 1; i < 101; i++) { 30 | c.add(i); 31 | } 32 | long start = System.currentTimeMillis(); 33 | List result = HFunc.map(c, new HFunc.Func1() { 34 | @Override 35 | public String call(Integer item) { 36 | try { 37 | Thread.sleep(10); 38 | } catch (InterruptedException e) { 39 | e.printStackTrace(); 40 | } 41 | return Integer.toString(item * 2); 42 | } 43 | }); 44 | long time = System.currentTimeMillis() - start; 45 | System.out.println("Before : " + c); 46 | System.out.println("==============="); 47 | System.out.println("After : " + result); 48 | System.out.println("==============="); 49 | System.out.println("Cost time : " + time + "ms"); 50 | } 51 | 52 | @Test 53 | public void mapParallel() throws Exception { 54 | List c = new ArrayList<>(); 55 | for (int i = 1; i < 101; i++) { 56 | c.add(i); 57 | } 58 | long start = System.currentTimeMillis(); 59 | List result = HFunc.mapParallel(c, new HFunc.Func1() { 60 | @Override 61 | public String call(Integer item) { 62 | try { 63 | Thread.sleep(10); 64 | } catch (InterruptedException e) { 65 | e.printStackTrace(); 66 | } 67 | return Integer.toString(item * 2); 68 | } 69 | }); 70 | long time = System.currentTimeMillis() - start; 71 | System.out.println("Before : " + c); 72 | System.out.println("==============="); 73 | System.out.println("After : " + result); 74 | System.out.println("==============="); 75 | System.out.println("Cost time : " + time + "ms"); 76 | } 77 | 78 | @Test 79 | public void filter() throws Exception { 80 | List c = new ArrayList<>(); 81 | for (int i = 1; i < 101; i++) { 82 | c.add(i); 83 | } 84 | long start = System.currentTimeMillis(); 85 | List result = HFunc.filter(c, new HFunc.Func1() { 86 | @Override 87 | public Boolean call(Integer item) { 88 | try { 89 | Thread.sleep(10); 90 | } catch (InterruptedException e) { 91 | e.printStackTrace(); 92 | } 93 | return item % 2 != 0; 94 | } 95 | }); 96 | long time = System.currentTimeMillis() - start; 97 | System.out.println("Before : " + c); 98 | System.out.println("==============="); 99 | System.out.println("After : " + result); 100 | System.out.println("==============="); 101 | System.out.println("Cost time : " + time + "ms"); 102 | } 103 | 104 | @Test 105 | public void filterParallel() throws Exception { 106 | List c = new ArrayList<>(); 107 | for (int i = 1; i < 101; i++) { 108 | c.add(i); 109 | } 110 | long start = System.currentTimeMillis(); 111 | List result = HFunc.filterParallel(c, new HFunc.Func1() { 112 | @Override 113 | public Boolean call(Integer item) { 114 | try { 115 | Thread.sleep(10); 116 | } catch (InterruptedException e) { 117 | e.printStackTrace(); 118 | } 119 | return item % 2 != 0; 120 | } 121 | }); 122 | long time = System.currentTimeMillis() - start; 123 | System.out.println("Before : " + c); 124 | System.out.println("==============="); 125 | System.out.println("After : " + result); 126 | System.out.println("==============="); 127 | System.out.println("Cost time : " + time + "ms"); 128 | } 129 | 130 | @Test 131 | public void reduce() throws Exception { 132 | List c = new ArrayList<>(); 133 | for (int i = 1; i < 101; i++) { 134 | c.add(i); 135 | } 136 | long start = System.currentTimeMillis(); 137 | Integer result = HFunc.reduce(c, new HFunc.Func2() { 138 | @Override 139 | public Integer call(Integer merge, Integer next) { 140 | try { 141 | Thread.sleep(10); 142 | } catch (InterruptedException e) { 143 | e.printStackTrace(); 144 | } 145 | return merge + next; 146 | } 147 | }); 148 | long time = System.currentTimeMillis() - start; 149 | System.out.println("Before : " + c); 150 | System.out.println("==============="); 151 | System.out.println("After : " + result); 152 | System.out.println("==============="); 153 | System.out.println("Cost time : " + time + "ms"); 154 | } 155 | 156 | @Test 157 | public void reduceParallel() throws Exception { 158 | List c = new ArrayList<>(); 159 | for (int i = 1; i < 101; i++) { 160 | c.add(i); 161 | } 162 | long start = System.currentTimeMillis(); 163 | Integer result = HFunc.reduceParallel(c, new HFunc.Func2() { 164 | @Override 165 | public Integer call(Integer merge, Integer next) { 166 | try { 167 | Thread.sleep(10); 168 | } catch (InterruptedException e) { 169 | e.printStackTrace(); 170 | } 171 | return merge + next; 172 | } 173 | }); 174 | long time = System.currentTimeMillis() - start; 175 | System.out.println("Before : " + c); 176 | System.out.println("==============="); 177 | System.out.println("After : " + result); 178 | System.out.println("==============="); 179 | System.out.println("Cost time : " + time + "ms"); 180 | } 181 | } 182 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /src/main/java/com/library/tangxiaolv/hf/HFunc.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014 XiaoLv Tang, Inc. 3 | *

4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | *

7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | *

9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | */ 13 | package com.library.tangxiaolv.hf; 14 | 15 | import java.util.ArrayList; 16 | import java.util.Arrays; 17 | import java.util.Collection; 18 | import java.util.Collections; 19 | import java.util.Iterator; 20 | import java.util.List; 21 | import java.util.concurrent.BlockingQueue; 22 | import java.util.concurrent.CountDownLatch; 23 | import java.util.concurrent.ExecutorService; 24 | import java.util.concurrent.LinkedBlockingQueue; 25 | import java.util.concurrent.ThreadPoolExecutor; 26 | import java.util.concurrent.TimeUnit; 27 | 28 | /** 29 | * A fast and simple Java Higher-order function lib. Support serial compute and parallel compute. 30 | *

31 | * Support {@link HFunc#map}{@link HFunc#filter}{@link HFunc#reduce} 32 | */ 33 | public class HFunc { 34 | 35 | /** 36 | * current available processors 37 | */ 38 | private static final int PROCESSORS = Runtime.getRuntime().availableProcessors(); 39 | 40 | /** 41 | * corePoolSize and maximumPoolSize 42 | *

43 | * see {@link ThreadPoolExecutor#ThreadPoolExecutor(int, int, long, TimeUnit, BlockingQueue)} 44 | *

45 | */ 46 | private static final int MAX_THREAD = PROCESSORS * 2; 47 | 48 | /** 49 | * the keepAliveTime for Thread 50 | *

51 | * see {@link ThreadPoolExecutor#ThreadPoolExecutor(int, int, long, TimeUnit, BlockingQueue)} 52 | *

53 | */ 54 | private static final int THREAD_TIME_OUT = 60;//SECONDS 55 | 56 | /** 57 | * used to parallel compute for Higher-order function 58 | *

59 | * see {@link ThreadPoolExecutor#ThreadPoolExecutor(int, int, long, TimeUnit, BlockingQueue)} 60 | *

61 | */ 62 | private static volatile ThreadPoolExecutor mThreadPoolExecutor; 63 | 64 | /** 65 | * Returns an list that applies a specified function to each item.This is serial compute. 66 | * 67 | * @param c collection with date item 68 | * @param func a function to apply to each item 69 | * @param collection item type 70 | * @param the result type 71 | * @return the result transformed by the specified function 72 | */ 73 | public static List map(Collection c, Func1 func) { 74 | if (c == null || c.size() == 0 || func == null) { 75 | return Collections.emptyList(); 76 | } 77 | ArrayList result = new ArrayList<>(); 78 | for (E item : c) { 79 | R r = func.call(item); 80 | result.add(r); 81 | } 82 | return result; 83 | } 84 | 85 | /** 86 | * Returns an list that applies a specified function to each item.This is parallel compute. 87 | * The concurrent quantity see{@link HFunc#MAX_THREAD} 88 | * 89 | * @param c collection with date item 90 | * @param func a function to apply to each item 91 | * @param collection item type 92 | * @param the result type 93 | * @return the result transformed by the specified function 94 | */ 95 | public static List mapParallel(Collection c, final Func1 func) { 96 | return mapParallel(null, c, func); 97 | } 98 | 99 | /** 100 | * Returns an list that applies a specified function to each item.This is parallel compute. 101 | * The concurrent quantity see{@link HFunc#MAX_THREAD} 102 | * 103 | * @param executor {@link ExecutorService} 104 | * @param c collection with date item 105 | * @param func a function to apply to each item 106 | * @param collection item type 107 | * @param the result type 108 | * @return the result transformed by the specified function 109 | */ 110 | public static List mapParallel(ExecutorService executor, Collection c, final Func1 func) { 111 | if (c == null || c.size() == 0 || func == null) { 112 | return Collections.emptyList(); 113 | } 114 | int size = c.size(); 115 | if (size == 1) { 116 | return map(c, new Func1() { 117 | @Override 118 | public R call(E item) { 119 | return func.call(item); 120 | } 121 | }); 122 | } 123 | executor = executor != null ? executor : defES(size); 124 | @SuppressWarnings("unchecked") final R[] arr = (R[]) new Object[size]; 125 | final CountDownLatch latch = new CountDownLatch(size); 126 | int order = 0; 127 | for (final E item : c) { 128 | final int index = order; 129 | executor.execute(new Runnable() { 130 | @Override 131 | public void run() { 132 | Func1Inner f = new Func1Inner<>(func, index); 133 | R r = f.call(item); 134 | arr[f.getIndex()] = r; 135 | latch.countDown(); 136 | } 137 | }); 138 | order++; 139 | } 140 | 141 | try { 142 | latch.await(); 143 | } catch (InterruptedException e) { 144 | return Collections.emptyList(); 145 | } 146 | return Arrays.asList(arr); 147 | } 148 | 149 | /** 150 | * Returns an list that applies a specified function to each item.This is serial compute. 151 | * 152 | * @param c collection with date item 153 | * @param func a function to apply to each item 154 | * @param collection item type 155 | * @return the result transformed by the specified function 156 | */ 157 | public static List filter(Collection c, Func1 func) { 158 | if (c == null || c.size() == 0 || func == null) { 159 | return Collections.emptyList(); 160 | } 161 | ArrayList result = new ArrayList<>(); 162 | for (E item : c) { 163 | if (func.call(item)) { 164 | result.add(item); 165 | } 166 | } 167 | return result; 168 | } 169 | 170 | /** 171 | * Returns an list that applies a specified function to each item.This is parallel compute. 172 | * The concurrent quantity see{@link HFunc#MAX_THREAD} 173 | * 174 | * @param c collection with date item 175 | * @param func a function to apply to each item 176 | * @param collection item type 177 | * @return the result transformed by the specified function 178 | */ 179 | public static List filterParallel(Collection c, Func1 func) { 180 | return filterParallel(null, c, func); 181 | } 182 | 183 | /** 184 | * Returns an list that applies a specified function to each item.This is parallel compute. 185 | * The concurrent quantity see{@link HFunc#MAX_THREAD} 186 | * 187 | * @param executor {@link ExecutorService} 188 | * @param c collection with date item 189 | * @param func a function to apply to each item 190 | * @param collection item type 191 | * @return the result transformed by the specified function 192 | */ 193 | public static List filterParallel(ExecutorService executor, Collection c, final Func1 func) { 194 | if (c == null || c.size() == 0 || func == null) { 195 | return Collections.emptyList(); 196 | } 197 | int size = c.size(); 198 | if (size == 1) { 199 | return filter(c, new Func1() { 200 | @Override 201 | public Boolean call(E item) { 202 | return func.call(item); 203 | } 204 | }); 205 | } 206 | executor = executor != null ? executor : defES(size); 207 | @SuppressWarnings("unchecked") final E[] arr = (E[]) new Object[size]; 208 | final CountDownLatch latch = new CountDownLatch(size); 209 | int order = 0; 210 | for (final E item : c) { 211 | final int index = order; 212 | executor.execute(new Runnable() { 213 | @Override 214 | public void run() { 215 | Func1Inner f = new Func1Inner<>(func, index); 216 | if (f.call(item)) { 217 | arr[f.getIndex()] = item; 218 | } 219 | latch.countDown(); 220 | } 221 | }); 222 | order++; 223 | } 224 | 225 | try { 226 | latch.await(); 227 | } catch (InterruptedException e) { 228 | return Collections.emptyList(); 229 | } 230 | 231 | return filter(Arrays.asList(arr), new Func1() { 232 | @Override 233 | public Boolean call(E item) { 234 | return item != null; 235 | } 236 | }); 237 | } 238 | 239 | /** 240 | * Return a merges result that applies a specified function to each item.This is serial compute. 241 | * 242 | * @param c collection with date item 243 | * @param func a function to apply to each item 244 | * @param collection item type 245 | * @return the result transformed by the specified function 246 | */ 247 | public static E reduce(Collection c, Func2 func) { 248 | if (c == null || c.size() == 0 || func == null) { 249 | return null; 250 | } 251 | Iterator it = c.iterator(); 252 | if (c.size() == 1) return it.next(); 253 | E merge = it.next(); 254 | while (it.hasNext()) { 255 | E next = it.next(); 256 | merge = func.call(merge, next); 257 | } 258 | return merge; 259 | } 260 | 261 | /** 262 | * Return a merges result that applies a specified function to each item.This is parallel compute. 263 | * The concurrent quantity see{@link HFunc#MAX_THREAD} 264 | * 265 | * @param c collection with date item 266 | * @param func a function to apply to each item 267 | * @param collection item type 268 | * @return the result transformed by the specified function 269 | */ 270 | public static E reduceParallel(Collection c, Func2 func) { 271 | return reduceParallel(null, c, func); 272 | 273 | } 274 | 275 | /** 276 | * Return a merges result that applies a specified function to each item.This is parallel compute. 277 | * The concurrent quantity see{@link HFunc#MAX_THREAD} 278 | * 279 | * @param executor {@link ExecutorService} 280 | * @param c collection with date item 281 | * @param func a function to apply to each item 282 | * @param collection item type 283 | * @return the result transformed by the specified function 284 | */ 285 | public static E reduceParallel(ExecutorService executor, Collection c, Func2 func) { 286 | if (c == null || c.size() == 0 || func == null) { 287 | return null; 288 | } 289 | int size = c.size(); 290 | if (size == 1) return c.iterator().next(); 291 | executor = executor != null ? executor : defES(size); 292 | //sub collection num 293 | int splits = mThreadPoolExecutor.getCorePoolSize(); 294 | CountDownLatch latch = new CountDownLatch(splits); 295 | List finals = Collections.synchronizedList(new ArrayList(splits)); 296 | 297 | Iterator it = c.iterator(); 298 | int elements = size / splits; 299 | boolean hasRemainder = size % splits != 0; 300 | for (int i = 0; i < splits; i++) { 301 | if (hasRemainder && i == splits - 1) elements += size % splits; 302 | ArrayList split = new ArrayList<>(); 303 | int count = 0; 304 | while (it.hasNext()) { 305 | split.add(it.next()); 306 | count++; 307 | if (count >= elements) break; 308 | } 309 | executor.submit(new Runnable() { 310 | @Override 311 | public void run() { 312 | finals.add(reduce(split, new Func2() { 313 | @Override 314 | public E call(E merge, E next) { 315 | return func.call(merge, next); 316 | } 317 | })); 318 | latch.countDown(); 319 | } 320 | }); 321 | } 322 | 323 | try { 324 | latch.await(); 325 | } catch (InterruptedException e) { 326 | return null; 327 | } 328 | 329 | //Summary 330 | return reduce(finals, new Func2() { 331 | @Override 332 | public E call(E merge, E next) { 333 | return func.call(merge, next); 334 | } 335 | }); 336 | } 337 | 338 | //Create ExecutorService 339 | private synchronized static ExecutorService defES(int size) { 340 | if (mThreadPoolExecutor != null) { 341 | return mThreadPoolExecutor; 342 | } 343 | int nThreads = size <= PROCESSORS ? size : size > MAX_THREAD ? MAX_THREAD : size; 344 | mThreadPoolExecutor = new ThreadPoolExecutor(nThreads, nThreads, THREAD_TIME_OUT, TimeUnit.SECONDS, new LinkedBlockingQueue<>()); 345 | mThreadPoolExecutor.allowCoreThreadTimeOut(true); 346 | return mThreadPoolExecutor; 347 | } 348 | 349 | /** 350 | * the function with index 351 | */ 352 | private static class Func1Inner { 353 | private Func1 func1; 354 | private int index; 355 | 356 | Func1Inner(Func1 func, int index) { 357 | this.func1 = func; 358 | this.index = index; 359 | } 360 | 361 | int getIndex() { 362 | return index; 363 | } 364 | 365 | R call(E item) { 366 | return func1.call(item); 367 | } 368 | } 369 | 370 | /** 371 | * Represents a function with one arguments. 372 | * 373 | * @param the first argument type 374 | * @param the result type 375 | */ 376 | public interface Func1 { 377 | public R call(E item); 378 | } 379 | 380 | /** 381 | * Represents a function with two arguments. 382 | * 383 | * @param the first argument type 384 | * @param the second argument type 385 | * @param the result type 386 | */ 387 | public interface Func2 { 388 | public R call(E1 merge, E2 next); 389 | } 390 | } --------------------------------------------------------------------------------