www.heteroclinic.net
package net.heteroclinic.experiment.lambdatest; import java.util.ArrayList; import java.util.List; /** * * @author zhikai * * Basic lambda test and pointer guard, need Java 8. */ public class Test1Simple { public interface ConcatEEs { String op(String a, String b); } public static int howManyArgs (Object ... ol) { return ol.length; } public static void trySomeCooldon (List> l) { for (Object o: l==null?new ArrayList(): l ) ;//O lala. } public static int weMeasureElipse (String ... s) { return weTakeElipse(s); } public static int weTakeElipse (String [] s) { return s.length; } public static void main(String[] args) { ConcatEEs concat2e = (a, b) -> a + b; System.out.println(concat2e.op("a", "b")); System.out.println( howManyArgs (1,2,3,4,5)); trySomeCooldon (null); System.out.println(weMeasureElipse("a","b","c")); //System.out.println(weTakeElipse("a","b","c")); } }
package net.heteroclinic.experiment.lambdatest; import java.util.Arrays; /** * * @author zhikai * * Try some stuff of regular language and recursion. * */ public class Test2 { public interface StringPlusString { String op(String a, String b); } public interface PlusArray { String op( PlusArray a,String[] b); } public static void main(String[] args) { System.out.println(concatStringsElise( (a,b) -> b.length<=1? b[0] : a.op(a, Arrays.copyOfRange(b, 1, b.length).clone()) , (a,b) -> a+b, "a", "b")); System.out.println(concatStringsElise( (a,b) -> b.length<=1? b[0] : b[0] + a.op(a, Arrays.copyOfRange(b, 1, b.length).clone()) , (a,b) -> a+b, "a", "b","c")); System.out.println(concatStringsElise( (a,b) -> b.length<=1? b[0] : b[0] + a.op(a, Arrays.copyOfRange(b, 1, b.length).clone()) , (a,b) -> a+b, "a", "b","c","d")); // String [] strings = {"a","b","c","d"}; // for (String s: Arrays.copyOfRange(strings, 1, strings.length).clone()) // System.out.println(s ); // b // c // d } public static String concatStringArray (PlusArray spa, StringPlusString sps, String [] strings) { String result = ""; if (strings.length <= 1) { return strings[0]; } else if (strings.length == 2 ) { return sps.op(strings[0],strings[1]); } else if (strings.length >= 3) { return strings[0] + spa.op(spa, Arrays.copyOfRange(strings, 1, strings.length).clone()); //return fe.op(ee2, Arrays.copyOfRange(strings, 0, strings.length-1).clone()); } return result; } public static String concatStringsElise (PlusArray spa, StringPlusString sps, String ... strings) { return concatStringArray(spa, sps, strings.clone()); } }
package net.heteroclinic.experiment.lambdatest; /** * * @author zhikai * * Use lambda to compute composite functions */ public class Test3 { public interface FunctionOfX { public double f (double x); } public static double compositeFunction (FunctionOfX f, FunctionOfX g, double x) { return f.f(g.f(x)); } public static void main(String[] args) { System.out.println ( compositeFunction ( x-> x*x , x->x+x, 12.3d) ); System.out.println( (2.0d*12.3d) * (2.0d*12.3d) ); } } //605.1600000000001 //605.1600000000001
package net.heteroclinic.experiment.lambdatest; import java.util.ArrayList; import java.util.Arrays; /** * * @author zhikai * * Passing arbitary number of functors * */ public class Test4 { public interface FunctionOfX { public double f (double x); } public static double compositeFunctions (double x, FunctionOfX ... functors) { if (functors==null) throw new NullPointerException(); double result = 0.0; int i = 0; for (FunctionOfX f : Arrays.asList( functors) ) { if (i == 0) result = f.f(x); else result = f.f(result); } return result; } public static void main(String[] args) { System.out.println( compositeFunctions( 1.123d, x-> x + 2.0d, x-> x * x * x, x -> 3.0d * Math.exp(x) + Math.sin(x) ) ); } }