11 functional concepts in java
play

11. Functional Concepts in Java State (e.g. Fields) Stateless - PowerPoint PPT Presentation

Functional vs. Imperative Programming Imperative concepts Functional Concepts Executing statements Evaluating expressions 11. Functional Concepts in Java State (e.g. Fields) Stateless Mutable data types Immutable data types Functional


  1. Functional vs. Imperative Programming Imperative concepts Functional Concepts Executing statements Evaluating expressions 11. Functional Concepts in Java State (e.g. Fields) Stateless Mutable data types Immutable data types Functional programming, lambda expressions, streams, pipelines Focus on data structures Focus on streams Focus on “how” Focus on “what” 342 343 Example: Reading of Files - Imperative Example: Readong of Files - Functional try (BufferedReader br=new BufferedReader(new FileReader("data.csv"))){ try (Stream<String> stream = Files.lines(Paths.get("data.csv"))) { LinkedList<Measurement> result = new LinkedList<>(); br.readLine(); String line ; while (( line = br.readLine()) != null){ return stream.skip(1).map(Measurement::new).collect(toList()); Measurement m = new Measurement(line); result .add(m); } return result ; } } 344 345

  2. Streams Operations on Streams: Map In Java, Streams are the basis for functional programming. Sources Map : Applying functions on individual elements of the stream of streams: Mathematical computations Files Creation of new objects based on existing elements. Arrays Data structures . . . Example . . . Example map(Measurement::new) Stream<String> stream = Files.lines (...)) 346 347 Operations on Streams: Reduce Example: Search for Data - Imparative Reduce : Aggregation of individual elements of a stream to one single value. List<Measurement> data = readCsvData(); Coordinate ref = readCoordinate(); Statistical aggregation for (Measurement m : data){ Put elements in a data structure if (m.position.near(ref)){ System.out.println(m.originalLine); . . . } Example } collect (toList ()) 348 349

  3. Example: Search for Data - Functional Operations on Streams: Filter Filter : Filter individual elements of a stream. List<Measurement> data = readCsvData(); Remove illegal values Coordinate ref = readCoordinate(); Select values based on inquiries data.stream() . . . . filter (m − > ref.near(m.position)) . forEach(System.out::println); Example filter (m − > ref.near(m.position)) 350 351 Operations on Streams: Side Effects Functionality as Parameter Sideeffects : The non-functional aspect: Execution on arbitrary Operations on streams have functionality (code) as parameter, operations based on individual elements. instead of data Input/Output Possibility to pass functionality (instead of data) Update data structures code snippets . . . References on methods Example References to constructors forEach(System.out::println) How can we do this? 352 353

  4. Lambda Expressions Lambda Expressions Lambda expression Lambda expressions are basically methods without names. (double a, double b, double c) − > { return b ∗ b − 4 ∗ a ∗ c; Normal method } double discriminant(double a, double b, double c){ Without explicit type declaration of the parameters return b ∗ b − 4 ∗ a ∗ c; } (a, b, c) − > { return b ∗ b − 4 ∗ a ∗ c; Equivalent lambda expression } (double a, double b, double c) − > { return b ∗ b − 4 ∗ a ∗ c; With a single expression instead of a block } (a, b, c) − > b ∗ b − 4 ∗ a ∗ c 354 355 Lambda Expression in the Example References on Methods Example filter (m − > ref.near(m.position)) To call a method on an object, we write: object.method() The method filter expects a method as parameter that takes a Measurement as parameter and returns a boolean . To specify a reference to a method on an object, we write: m is a parameter of type Measurement � object::methode ref.near(m.position) is a single boolean expression � The variable ref from the defining context is accessible, if it is effectively constant ( final ). 356 357

  5. References on Static Methods Reference to a Method in the Example Example To call a static method, we write: forEach(System.out::println) Clazz.method() The method forEach expects a method, which doesn’t return anything and To specify a reference to a static method, we write: takes an argument of type Measurement . Clazz::method The method println on object out satisfies those properties � 358 359 References to Constructors References to a Constructor in the Example Example To call a constructor of a class, we write: map(Measurement::new) new Clazz() The method map expects a method that returns an object of a certain data types To specify a reference to a constructor of a class, we write: (it doesn’t matter which) and an argument of type String . Clazz::new The constructor of the class Measurement satisfies this property � 360 361

  6. Advantages and Disadvantages of Functional Programming Less error-prone Learn another language concept Easier to maintain Details on the execution are Allows for elegant programming unknown constructs Super-imposed on an imperative Independent on specific language architecture 362

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend