Created
April 29, 2020 05:26
-
-
Save leoxlin/5bc0d4149f960bc1333bf112f63d481d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
import java.util.List; | |
import java.util.function.Function; | |
public class JavaLambdaCompilerExample { | |
interface ImplicitLambda1 { | |
String whatever(); | |
} | |
interface ImplicitLambda2 { | |
String whatever(Integer x); | |
} | |
interface ImplicitLambda3 { | |
String whatever(Integer x, Double y); | |
} | |
interface ImplicitLambda4 { | |
void whatever(Integer x); | |
} | |
interface ComplexLambda extends Function<List<Integer>, Function<Integer, Function<String, String>>> {} | |
public static void main(String[] args) { | |
ImplicitLambda1 implicitLambda1 = () -> "out"; | |
ImplicitLambda2 implicitLambda2 = x -> x + " out"; | |
ImplicitLambda3 implicitLambda3 = (x, y) -> x + y + " out"; | |
ImplicitLambda4 implicitLambda4 = x -> { | |
System.out.println("I ate x: " + x); | |
}; | |
System.out.println(implicitLambda1.whatever()); | |
System.out.println(implicitLambda2.whatever(10)); | |
System.out.println(implicitLambda3.whatever(10, 33.33)); | |
implicitLambda4.whatever(30); | |
ComplexLambda definition = list -> otherIntInput -> postfix -> { | |
int listSum = list.stream().reduce(Integer::sum).orElse(0); | |
return listSum + otherIntInput + postfix; | |
}; | |
String complexLambdaResult = definition | |
.apply(Arrays.asList(10, 10, 10, 10, 10)) | |
.apply(5) | |
.apply(" was the result"); | |
System.out.println(complexLambdaResult); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment