lambda expressions
play

Lambda Expressions CSC 203 Christopher Siu 1 / 12 Lambda - PowerPoint PPT Presentation

Lambda Expressions CSC 203 Christopher Siu 1 / 12 Lambda Expressions A function is a mapping of inputs to outputs: input, x f Defjnition A lambda expression defjnes a function that can be passed and returned as though it were a value. Lambda


  1. Lambda Expressions CSC 203 Christopher Siu 1 / 12

  2. Lambda Expressions A function is a mapping of inputs to outputs: input, x f Defjnition A lambda expression defjnes a function that can be passed and returned as though it were a value. Lambda expressions are also called anonymous functions , because they are not bound to one particular name. Java does not allow standalone functions — every method must be contained within a class. 2 / 12 output, f ( x )

  3. Lambda Expressions public int compare(Point pt1, Point pt2) { to be passed around. A Comparator class is boilerplate that allows a single method } 10 } 9 .compareTo(pt2.getX()); 8 return ((Double)pt1.getX()) 7 6 Example { 5 implements Comparator<Point> 4 public class PointComparator 3 2 import java.util.Comparator; 1 Consider the following class: 3 / 12

  4. Functional Interfaces Defjnition A functional interface defjnes exactly one abstract method. Functional interfaces can be targets for lambda expressions. A lambda expression in Java has the form: ( PARAMETERS …) -> VALUE Parameter and return types are omitted. If the method has one parameter, the parentheses are optional. Example 1 Comparator<Point> compareByY = 2 (pt1, pt2) -> ((Double)pt1.getY()) 3 .compareTo(pt2.getY()); 4 / 12

  5. Functional Interfaces 4 interfaces for common forms of functions. The java.util.function package defjnes functional in Java, they only hide it behind interfaces and generics. Functional interfaces don’t truly solve the problem of lambdas } 5 public String stringify(Point pt); { In order to use a lambda in Java, an appropriate functional 3 public interface PointStringifier 2 @FunctionalInterface 1 Example interface must already have been defjned. 5 / 12

  6. The Function Interface } pt -> "(" + pt.getX() + ", " + pt.getY() + ")"; 2 Function<Point, String> stringifyPoint = 1 Example that takes one argument and returns a value. A java.util.function.Function encapsulates a method 5 Defjnition R apply(T t); 4 { 3 public interface Function<T, R> 2 @FunctionalInterface 1 6 / 12

  7. The Predicate Interface } pt -> pt.getX() > 0 && pt.getY() > 0; 2 Predicate<Point> inFirstQuadrant = 1 Example that takes one argument and returns true or false . A java.util.function.Predicate encapsulates a method 5 Defjnition boolean test(T t); 4 { 3 public interface Predicate<T> 2 @FunctionalInterface 1 7 / 12

  8. The Consumer Interface } pt -> System.out.println("pt.x: " + s.getX()); 2 Consumer<Point> printX = 1 Example that takes one argument and does not return anything. A java.util.function.Consumer encapsulates a method 5 Defjnition void accept(T t); 4 { 3 public interface Consumer<T> 2 @FunctionalInterface 1 8 / 12

  9. The Supplier Interface } () -> new Point(0.0, 0.0); 2 Supplier<Point> createOrigin = 1 Example that takes no arguments and returns a value. A java.util.function.Supplier encapsulates a method 5 Defjnition T get(); 4 { 3 public interface Supplier<T> 2 @FunctionalInterface 1 9 / 12

  10. Method References Example Consider the following lambda: 1 Function<Point, String> stringifyPoint = 2 pt -> pt.toString(); This lambda expression merely calls an existing method. A method reference in Java has the form: CLASS :: METHOD Example 1 Function<Point, String> stringifyPoint = 2 Point::toString; 10 / 12

  11. Composing Lambda Expressions String::toUpperCase; 2 It then converts the string representation to upper case. 1 shoutPoint fjrst stringifjes the given Point . shoutString.compose(stringifyPoint); 7 Function<Point, String> shoutPoint = 6 5 4 Some functional interfaces provide additional methods for Function<String, String> shoutString = 3 pt -> "(" + pt.getX() + ", " + pt.getY() + ")"; 2 Function<Point, String> stringifyPoint = 1 Example combining lambda expressions. 11 / 12

  12. Composing Lambda Expressions 6 2 It then breaks any ties by comparing y -coordinates. 1 compareByXthenY fjrst compares x -coordinates. compareByX.thenComparing(compareByY); 9 Comparator<Point> compareByXthenY = 8 7 .compareTo(pt2.getY()); (pt1, pt2) -> ((Double)pt1.getY()) Example 5 Comparator<Point> compareByY = 4 .compareTo(pt2.getX()); 3 (pt1, pt2) -> ((Double)pt1.getX()) 2 Comparator<Point> compareByX = 1 12 / 12

Recommend


More recommend