functional programming in java
play

Functional Programming in Java Prof. Dr. Ralf Lmmel Msc. Johannes - PowerPoint PPT Presentation

Functional Programming in Java Prof. Dr. Ralf Lmmel Msc. Johannes Hrtel Msc. Marcel Heinz (C) 2018, SoftLang Team, University of Koblenz-Landau Java without functional Constructs 101Company Features OO-Model Cut Total


  1. Functional Programming in Java Prof. Dr. Ralf Lämmel Msc. Johannes Härtel Msc. Marcel Heinz (C) 2018, SoftLang Team, University of Koblenz-Landau

  2. Java without functional Constructs 101Company Features ● OO-Model ● Cut ● Total (C) 2018, SoftLang Team, University of Koblenz-Landau

  3. Java without functional Constructs 101Company Features ● OO-Model ● Cut ● Total Getters and Setters are omitted on the slide for brevity. (C) 2018, SoftLang Team, University of Koblenz-Landau

  4. Java without functional Constructs 101Company Features ● OO-Model ● Cut ● Total Getters and Setters are omitted on the slide for brevity. (C) 2018, SoftLang Team, University of Koblenz-Landau

  5. Java without functional Constructs 101Company Features ● OO-Model ● Cut ● Total Typical imperative Java-code with for-loops. (C) 2018, SoftLang Team, University of Koblenz-Landau

  6. Java without functional Constructs 101Company Features ● OO-Model ● Cut ● Total Typical imperative Java-code with for-loops. (C) 2018, SoftLang Team, University of Koblenz-Landau

  7. Java History ● Language ○ “ Lambda Expressions , a new language feature, has been introduced in this ● Java 8 release. They enable you to treat ● Java 9 functionality as a method argument, or ● Java 10 code as data. Lambda expressions let you express instances of single-method See interfaces (referred to as functional https://www.oracle.com/techn etwork/java/javase/8-whats-ne interfaces) more compactly.” w-2157071.html ○ “ Method references provide easy-to-read lambda expressions for methods that already have a name.” ○ “Improved type inference .” (C) 2018, SoftLang Team, University of Koblenz-Landau

  8. Java History ● Collections ○ “Classes in the new java.util.stream package provide a Stream API to support ● Java 8 functional-style operations on streams ● Java 9 of elements. The Stream API is ● Java 10 integrated into the Collections API, which enables bulk operations on collections, See such as sequential or parallel https://www.oracle.com/techn etwork/java/javase/8-whats-ne map-reduce transformations.” w-2157071.html (C) 2018, SoftLang Team, University of Koblenz-Landau

  9. Java History ● Tools ○ “The jshell tool provides an interactive command-line interface for evaluating ● Java 8 declarations, statements, and ● Java 9 expressions of the Java programming ● Java 10 language. It facilitates prototyping and exploration of coding options with See immediate results and feedback. The https://docs.oracle.com/javase /9/whatsnew/toc.htm#JSNEW- immediate feedback combined with the GUID-5B808B2F-E891-43CD- ability to start with expressions is useful BF6E-78787E547071 for education —whether learning the Java language or just learning a new API or language feature.” (C) 2018, SoftLang Team, University of Koblenz-Landau

  10. Java History ● Java Class Library ○ Changes in the core libraries that shall not be of interest for this course. ● Java 8 ● Java 9 ● Java 10 See https://docs.oracle.com/javase /9/whatsnew/toc.htm#JSNEW- GUID-5B808B2F-E891-43CD- BF6E-78787E547071 (C) 2018, SoftLang Team, University of Koblenz-Landau

  11. Java History ● Local Variable Type Inference ○ We will come to this later. ● Java 8 ● Java 9 ● Java 10 This course focuses on the changes introduced in Java 8. (C) 2018, SoftLang Team, University of Koblenz-Landau

  12. How can we code Functional Cut and Total in a Programming functional fashion? - in Java Thus, it needs to resemble mathematical ● ForEach ● Lambda Calculus expressions instead of ● Method Reference ● Streams imperative commands. ○ Map ○ Reduce ○ Filter ○ Concat Which functional ○ Parallel constructs exist in ○ Collect ○ Debugging Java? ● Optionals (C) 2018, SoftLang Team, University of Koblenz-Landau

  13. Functional Programming - in Java Imperative Programming ● ForEach ● Lambda Calculus ● Method Reference “Functional Programming” ● Streams ○ Map ○ Reduce ○ Filter ○ Concat ○ Parallel ○ Collect ○ Debugging ● Optionals (C) 2018, SoftLang Team, University of Koblenz-Landau

  14. Functional Programming - in Java ● ForEach ● Lambda Calculus ● Method Reference ● Streams ○ Map ○ Reduce We apply the function cut to every element of the ○ Filter department list. ○ Concat ○ Parallel ○ Collect ○ Debugging ● Optionals (C) 2018, SoftLang Team, University of Koblenz-Landau

  15. Functional Programming - in Java ● ForEach ● Lambda Calculus ● Method Reference ● Streams ○ Map ○ Reduce Java8 introduces Lambda expressions. ○ Filter The symbol “ λ” is not written. ○ Concat It looks like we’re applying an anonymous function. ○ Parallel ○ Collect ○ Debugging ● Optionals (C) 2018, SoftLang Team, University of Koblenz-Landau

  16. Type Inferred at Lambda Expression Hovering over d in Eclipse. (C) 2018, SoftLang Team, University of Koblenz-Landau

  17. Functional Programming - in Java ● ForEach ● Lambda Calculus ● Method Reference ● Streams Java8 introduces Method References. ○ Map Methods can be passed as arguments. ○ Reduce Syntax: <namespace>::<name> ○ Filter ● <Namespace> is a class for static methods ○ Concat or an object for instance methods. ○ Parallel ● <Name> is the name of a method that is part ○ Collect of the namespace. ○ Debugging ● Optionals (C) 2018, SoftLang Team, University of Koblenz-Landau

  18. Functional Programming - in Java ● ForEach ● Lambda Calculus ● Method Reference ● Streams ○ Map Our current context serves as the namespace. Our ○ Reduce cut() methods are instance methods of the class ○ Filter Cut. If they were static methods, we would write ○ Concat Cut::cut . ○ Parallel ○ Collect ○ Debugging ● Optionals (C) 2018, SoftLang Team, University of Koblenz-Landau

  19. To demonstrate what happens underneath, let us rewrite the code in a more explicit manner. Functional Programming - in Java ● ForEach Same for the Lambda way. ● Lambda Calculus ● Method Reference ● Streams ○ Map ○ Reduce ○ Filter ○ Concat ○ Parallel ○ Collect Both times, an instance of Consumer is created, which is a functional ○ Debugging interface . A functional interface is an interface with only one abstract ● Optionals method . (C) 2018, SoftLang Team, University of Koblenz-Landau

  20. We can write functions that can Changing Employees change any employee the way we want. (C) 2018, SoftLang Team, University of Koblenz-Landau

  21. Functional Programming - in Java ● ForEach (Rolls eyes) And here ● Lambda Calculus comes the magic functional ● Method Reference programmer turning this into ● Streams one line... ○ Map ○ Reduce ○ Filter ○ Concat ○ Parallel ○ Collect ○ Debugging ● Optionals (C) 2018, SoftLang Team, University of Koblenz-Landau

  22. Total ForEach ● Lambda Calculus ● Method Reference ● Functional Interface ● Okay, I don’t get the code anymore. Let the magician stream ● maintain this part. map ○ filter ○ <- Besides, your reduce TOC is gone, ○ because of the long parallelStream ● line. Optionals ● (C) 2018, SoftLang Team, University of Koblenz-Landau

  23. Functional By now, there exist a lot of style guidelines on the issue of emphasizing understandability. Programming (https://dzone.com/articles/functional-programming-patter - in Java ns-with-java-8) ● ForEach ● Lambda Calculus ● Method Reference ● Streams ○ Map ○ Reduce ○ Filter ○ Concat ○ Parallel ○ Collect ○ Debugging ● Optionals (C) 2018, SoftLang Team, University of Koblenz-Landau

  24. total :: Company -> Float Functional total c = foldr (+) 0 (map total (getDepts c)) Programming Haskell-way for comparison. - in Java ● ForEach ● Lambda Calculus ● Method Reference ● Streams ○ Map ○ Reduce ○ Filter ○ Concat ○ Parallel ○ Collect ○ Debugging ● Optionals (C) 2018, SoftLang Team, University of Koblenz-Landau

  25. Functional Programming - in Java Imperative way for comparison. ● ForEach ● Lambda Calculus ● Method Reference ● Streams ○ Map ○ Reduce ○ Filter ○ Concat ○ Parallel ○ Collect ○ Debugging ● Optionals (C) 2018, SoftLang Team, University of Koblenz-Landau

  26. Functional Programming - in Java ● ForEach ● Lambda Calculus ● Method Reference ● Streams Streams offer the capability of processing elements ○ Map in a sort of pipeline. A stream can either be linear ○ Reduce or parallel. ○ Filter ○ Concat ○ Parallel ○ Collect ○ Debugging ● Optionals (C) 2018, SoftLang Team, University of Koblenz-Landau

  27. Functional Programming - in Java ● ForEach ● Lambda Calculus ● Method Reference ● Streams After turning the list into a stream, we apply the ○ Map lambda expression to every element in the stream. ○ Reduce ○ Filter ○ Concat ○ Parallel ○ Collect ○ Debugging ● Optionals (C) 2018, SoftLang Team, University of Koblenz-Landau

Recommend


More recommend