Java 8 Lambda Expressions: Difference between revisions
Line 15: | Line 15: | ||
A lambda expression is a representation of an ''anonymous [[Functional_Programming#Function|function]]'': it does not have a name, but it has a list of parameters, a body, a return type, and possibly a list of exceptions that can be thrown. | A lambda expression is a representation of an ''anonymous [[Functional_Programming#Function|function]]'': it does not have a name, but it has a list of parameters, a body, a return type, and possibly a list of exceptions that can be thrown. | ||
Lambda expressions can be stored as values, in variables, and passed as arguments to methods and constructors. | |||
Lambda expressions are a convenient way of increasing the code clarity by in-lining logic. However, if the lambda's body exceeds a few lines in length, so that its behavior isn't instantly clear, the logic should be encapsulated in a method and a [[#Method_Reference|method reference]] should be used instead of the lambda expression. | Lambda expressions are a convenient way of increasing the code clarity by in-lining logic. However, if the lambda's body exceeds a few lines in length, so that its behavior isn't instantly clear, the logic should be encapsulated in a method and a [[#Method_Reference|method reference]] should be used instead of the lambda expression. |
Revision as of 17:45, 23 March 2018
External
- Lambda Expressions Tutorial https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html
Internal
Overview
Java 8 introduces functional programming features, in the form of lambda expressions. Lambda expressions allow behavior parameterization - functions can be now assigned to variables, as values, and passed around, which essentially means passing code around. Functions come in form of lambda expressions (anonymous functions) or method references.
Lambda Expression
A lambda expression is a representation of an anonymous function: it does not have a name, but it has a list of parameters, a body, a return type, and possibly a list of exceptions that can be thrown.
Lambda expressions can be stored as values, in variables, and passed as arguments to methods and constructors.
Lambda expressions are a convenient way of increasing the code clarity by in-lining logic. However, if the lambda's body exceeds a few lines in length, so that its behavior isn't instantly clear, the logic should be encapsulated in a method and a method reference should be used instead of the lambda expression.
Syntax
Statement Lambda
TODO
Expression Lambda
TODO
Method Reference
The semantics behind the method reference syntax is use this method as a value.
The <Class-Name>::<method-name> syntax creates a method reference, which then can be passed around as a value.
Relationship between Lambda Expressions and Method References
Functional Interface
@FunctionalInterface
- java.lang.Runnable https://docs.oracle.com/javase/8/docs/api/java/lang/Runnable.html
- java.util.concurrent.Callable
- java.util.Comparator https://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html
- java.util.function.Predicate https://docs.oracle.com/javase/8/docs/api/java/util/function/Predicate.html
Predicate
A predicate is a function that evaluates an argument and returns a boolean.
To Process
A lambda expression can be thought of as an anonymous methods. As shown in the "Syntax" section below, a lambda expression looks a lot like a method declaration.
Playground Example
Functional Interface
An interface with only one method is named functional interface
Apparently this is the type of a lambda expression, it describe the function signature.
The Type of a Lambda Expression
Elaborate here.
Syntax
A lambda expression consists in a comma-separated list of formal parameters, enclosed in parentheses, followed by the arrow token "->" followed by a body, which may be a single expression or a statement block.
(comma_separated_formal_parameter_list) -> body
Formal Parameters
The type of the parameters may be omitted.
If there is a single parameter, the enclosed parentheses may be omitted.
Body
The body may be a single expression, or a statement block.
Referencing a Lambda Expression
Method References
Example:
public ZipHandler getZipHandler() { return ZipUtil::getTopLevelDirectoryName; }
Variables
The variables used in the lambda expressions must be final.
TODO
- Lambda Expressions Tutorial https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html
- java.util.function https://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html