KEMBAR78
Functional programming | PPTX
LHOUCEINE Ouhamza: Java Developer,
+10 years of IT experience.
©2020
ouhamza.web.pro@gmail.com
3
Level 1 - beginner
Lambda expressions and streams API
Imperative programming
Object-oriented programming
Functional programming
Google trends currently ranks functional programming as more popular than Object-
oriented programming
Programming perspective
Functional programming is a paradigm that allows programming using
expressions i.e. declaring functions, passing functions as arguments and
using functions as statements.
It also simplifies the creation of concurrent programs. Concurrency (or
parallel processing) is vital for improving application performance.
Functional programming
5
In functional programming, there are two very important rules:
Immutable state: It means a data object should not be changed after it is
created.
Pure function: always returns the same result for the same arguments.
Rules of functional programming
6
● Usage of functions as input to and output from other functions,
higher order functions.
● Usage of map , filter , and reduce type functions instead of looping.
● Immutable state.
● Recursion in place of looping.
● Composing functions from other functions.
● Distinguishing “pure” functions from functions with side effects.
Advantages of functional programming
7
Programming language which supports functional programming:
Programming languages
8
❏ Lambda expressions are the most talked feature of Java 8.
❏ you could think about lambda expressions as a way of supporting functional
programming in Java.
❏ To assign a lambda expression you need to have a Functional Interface.
Lambda expressions
10
Is a interface with a single abstract method
interface Welcome {
abstract void welcome(String string);
}
Functional interface
11
@FunctionalInterface annotation is optional.
Lambda expression (1)
12
Let’s say you have a list of numbers :
List<Integer> numbers = Arrays.asList(1,2,3,4,5,6,7,8,9);
If you were asked to print the numbers from the list?
for (Integer number : numbers) {
System.out.println(number);
}
Lambda expression (2)
13
We Will can use aggregate operations.
numbers.forEach(number -> System.out.println(number));
We can still improve it further using method reference
operator.
numbers.forEach(System.out::println);
Lambda expression (3)
14
Stream represents a sequence of objects from a source ( array,
collection or from database ).
With streams. You can use lambda expressions with more functions,
called aggregate operations like:
ForEach, Reduce, Filter, Map, flatMap, Collect, Count...
Streams Api
15
Intermediate operations such as filter() return a new stream on which further
processing can be done. Terminal operations, such as forEach(), mark the stream
as consumed.
A stream pipeline consists of a stream source, followed by zero or more
intermediate operations, and a terminal operation.
Method Types and Pipelines
16
When even you have a problem with null parameter think of Optional.
It’s the best solution to use Java 8 feature.
Optional class
17
18
DEMO
https://github.com/ouhamzalhss/functional-programming
19
Level 2 - advanced
Functional interface
Clean code + java 8
20
Java 8 introduced @FunctionalInterface, an interface that has
exactly one abstract method. The compiler will treat any interfaces
meeting the definition of a functional interface as a functional interface;
it means the @FunctionalInterface annotation is optional.
Functional Interface
21
In Java 8, BiFunction is a functional interface; it takes two
arguments and returns an object.
BiFunction interface
T – Type of the first argument to the function.
U – Type of the second argument to the function.
R – Type of the result of the function.
22
In Java 8, Consumer is a functional interface; it takes an argument and
returns nothing.
Consumer Interface
23
Let us see the six basic function interfaces.
Functional Interface
24
25
❏ Use Lambda Expressions: anonymous function.
❏ But you never use this: -> {.....
...}
It should be one line.
❏ Extract heavy lambda into named::methods
Clean code: Flat lambda
26
27
❏ No nullable parameters (use Optional )
❏ No optional parameters
Instead: thaOption.map(myfunction).
❏ Avoid returning null, you should throw exception or return Optional:
Clean code : parameters
28
❏ You must suffer if you work with non-runtime exceptions
❏ You can use libraries like jool,lombok : rethrow checked as
runtime exception.
Clean code: Exceptions
29
Clean code: Best practices
30
❏ Functions and type over classes.
❏ Composition over inheritance.
❏ Purity over mutability.
❏ Options over nulls.
❏ Runtime over checked exceptions.
❏ Don’t iterate use map, reduce, filter…
❏ Remove extra wrappings with .flatMap
❏ Only use a stream once.
Best practices
31
Huge thanks to you
friends
32
DEMO
https://github.com/ouhamzalhss/functional-programming-advanced

Functional programming

  • 2.
    LHOUCEINE Ouhamza: JavaDeveloper, +10 years of IT experience. ©2020 ouhamza.web.pro@gmail.com
  • 3.
    3 Level 1 -beginner Lambda expressions and streams API
  • 4.
    Imperative programming Object-oriented programming Functionalprogramming Google trends currently ranks functional programming as more popular than Object- oriented programming Programming perspective
  • 5.
    Functional programming isa paradigm that allows programming using expressions i.e. declaring functions, passing functions as arguments and using functions as statements. It also simplifies the creation of concurrent programs. Concurrency (or parallel processing) is vital for improving application performance. Functional programming 5
  • 6.
    In functional programming,there are two very important rules: Immutable state: It means a data object should not be changed after it is created. Pure function: always returns the same result for the same arguments. Rules of functional programming 6
  • 7.
    ● Usage offunctions as input to and output from other functions, higher order functions. ● Usage of map , filter , and reduce type functions instead of looping. ● Immutable state. ● Recursion in place of looping. ● Composing functions from other functions. ● Distinguishing “pure” functions from functions with side effects. Advantages of functional programming 7
  • 8.
    Programming language whichsupports functional programming: Programming languages 8
  • 10.
    ❏ Lambda expressionsare the most talked feature of Java 8. ❏ you could think about lambda expressions as a way of supporting functional programming in Java. ❏ To assign a lambda expression you need to have a Functional Interface. Lambda expressions 10
  • 11.
    Is a interfacewith a single abstract method interface Welcome { abstract void welcome(String string); } Functional interface 11 @FunctionalInterface annotation is optional.
  • 12.
  • 13.
    Let’s say youhave a list of numbers : List<Integer> numbers = Arrays.asList(1,2,3,4,5,6,7,8,9); If you were asked to print the numbers from the list? for (Integer number : numbers) { System.out.println(number); } Lambda expression (2) 13
  • 14.
    We Will canuse aggregate operations. numbers.forEach(number -> System.out.println(number)); We can still improve it further using method reference operator. numbers.forEach(System.out::println); Lambda expression (3) 14
  • 15.
    Stream represents asequence of objects from a source ( array, collection or from database ). With streams. You can use lambda expressions with more functions, called aggregate operations like: ForEach, Reduce, Filter, Map, flatMap, Collect, Count... Streams Api 15
  • 16.
    Intermediate operations suchas filter() return a new stream on which further processing can be done. Terminal operations, such as forEach(), mark the stream as consumed. A stream pipeline consists of a stream source, followed by zero or more intermediate operations, and a terminal operation. Method Types and Pipelines 16
  • 17.
    When even youhave a problem with null parameter think of Optional. It’s the best solution to use Java 8 feature. Optional class 17
  • 18.
  • 19.
    19 Level 2 -advanced Functional interface Clean code + java 8
  • 20.
    20 Java 8 introduced@FunctionalInterface, an interface that has exactly one abstract method. The compiler will treat any interfaces meeting the definition of a functional interface as a functional interface; it means the @FunctionalInterface annotation is optional. Functional Interface
  • 21.
    21 In Java 8,BiFunction is a functional interface; it takes two arguments and returns an object. BiFunction interface T – Type of the first argument to the function. U – Type of the second argument to the function. R – Type of the result of the function.
  • 22.
    22 In Java 8,Consumer is a functional interface; it takes an argument and returns nothing. Consumer Interface
  • 23.
    23 Let us seethe six basic function interfaces. Functional Interface
  • 24.
  • 25.
    25 ❏ Use LambdaExpressions: anonymous function. ❏ But you never use this: -> {..... ...} It should be one line. ❏ Extract heavy lambda into named::methods Clean code: Flat lambda
  • 26.
  • 27.
    27 ❏ No nullableparameters (use Optional ) ❏ No optional parameters Instead: thaOption.map(myfunction). ❏ Avoid returning null, you should throw exception or return Optional: Clean code : parameters
  • 28.
    28 ❏ You mustsuffer if you work with non-runtime exceptions ❏ You can use libraries like jool,lombok : rethrow checked as runtime exception. Clean code: Exceptions
  • 29.
  • 30.
    30 ❏ Functions andtype over classes. ❏ Composition over inheritance. ❏ Purity over mutability. ❏ Options over nulls. ❏ Runtime over checked exceptions. ❏ Don’t iterate use map, reduce, filter… ❏ Remove extra wrappings with .flatMap ❏ Only use a stream once. Best practices
  • 31.
    31 Huge thanks toyou friends
  • 32.

Editor's Notes

  • #3 Functional programming is a programming paradigm—a style of building the structure and elements of computer programs—that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data.
  • #7 Pure functions only: Functional code is idempotent. A function should return values only based on the arguments passed and should not affect(side-effect) or depend on global state. Such functions always produce the same result for the same arguments.
  • #10 default and static methods in Interfaces. Functional Interfaces and Lambda Expressions. Java Stream API for Bulk Data Operations on Collections. Java Time API. Optional class
  • #29 Checked exception makes your code ugly by adding of try-catch-finally block.