JAVA函数式编程尝试实现
接口
public interface Func { public Object invoke(Object ...o); }
MAP
import java.util.Arrays; public class Test { /** * @param args */ public static void main(String[] args) { Object[] arr = new Object[]{1, 2, 3, 4, 5}; map(arr, new Func() { @Override public Object invoke(Object ...o) { return (Integer)o[0] * 2; } }); System.out.println(Arrays.toString(arr));//output [2, 4, 6, 8, 10] } public static void map(Object[] arr, Func function) { for (int i = 0; i < arr.length; i++) { arr[i] = function.invoke(arr[i]); } } }
reduce
public class Test2 { /** * @param args */ public static void main(String[] args) { Object sum = sum(new Object[]{0, 1, 2, 3, 4}); Object str = join(new Object[]{0, 1, 2, 3, 4}); System.out.println(sum);//output 10 System.out.println(str);//output 01234 } private static Object join(Object[] objects) { return reduce(objects, "", new Func() { @Override public Object invoke(Object... o) { return o[0].toString() + o[1]; } }); } public static Object sum(Object[] arr) { return reduce(arr, 0, new Func() { @Override public Object invoke(Object ...o) { return (Integer)o[0] + (Integer)o[1]; } }); } private static Object reduce(Object[] arr, Object init, Func f) { Object sum = init; for (int i = 0; i < arr.length; i++) { sum = f.invoke(sum, arr[i]); } return sum; } }