SlideShare a Scribd company logo
Functional programming
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
Functional programming
❏ 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

More Related Content

What's hot (20)

PDF
Java 8 Lambda Expressions
Scott Leberknight
 
PDF
07 java collection
Abhishek Khune
 
PDF
Spring annotation
Rajiv Srivastava
 
PPTX
Hibernate jpa
Lhouceine OUHAMZA
 
PPTX
Interfaces in java
Abishek Purushothaman
 
PDF
Collections In Java
Binoj T E
 
PPTX
Java 8 Lambda and Streams
Venkata Naga Ravi
 
PPTX
Spring data jpa
Jeevesh Pandey
 
PDF
Java8 features
Elias Hasnat
 
PPT
Formation jpa-hibernate-spring-data
Lhouceine OUHAMZA
 
PDF
Generics and collections in Java
Gurpreet singh
 
PDF
Monadic Java
Mario Fusco
 
PDF
TypeScript - An Introduction
NexThoughts Technologies
 
PDF
Java 8 Workshop
Mario Fusco
 
PDF
Chapitre 11: Expression Lambda et Référence de méthode en Java
Aziz Darouichi
 
PDF
Chap 6 : classes et interfaces
Aziz Darouichi
 
PPTX
Spring boot Introduction
Jeevesh Pandey
 
PPTX
Optional in Java 8
Richard Walker
 
PPTX
Workshop Spring - Session 1 - L'offre Spring et les bases
Antoine Rey
 
PPT
Interface in java By Dheeraj Kumar Singh
dheeraj_cse
 
Java 8 Lambda Expressions
Scott Leberknight
 
07 java collection
Abhishek Khune
 
Spring annotation
Rajiv Srivastava
 
Hibernate jpa
Lhouceine OUHAMZA
 
Interfaces in java
Abishek Purushothaman
 
Collections In Java
Binoj T E
 
Java 8 Lambda and Streams
Venkata Naga Ravi
 
Spring data jpa
Jeevesh Pandey
 
Java8 features
Elias Hasnat
 
Formation jpa-hibernate-spring-data
Lhouceine OUHAMZA
 
Generics and collections in Java
Gurpreet singh
 
Monadic Java
Mario Fusco
 
TypeScript - An Introduction
NexThoughts Technologies
 
Java 8 Workshop
Mario Fusco
 
Chapitre 11: Expression Lambda et Référence de méthode en Java
Aziz Darouichi
 
Chap 6 : classes et interfaces
Aziz Darouichi
 
Spring boot Introduction
Jeevesh Pandey
 
Optional in Java 8
Richard Walker
 
Workshop Spring - Session 1 - L'offre Spring et les bases
Antoine Rey
 
Interface in java By Dheeraj Kumar Singh
dheeraj_cse
 

Similar to Functional programming (20)

PPTX
Java 8
vpulec
 
PPTX
Java8 training - Class 1
Marut Singh
 
PPTX
Insight into java 1.8, OOP VS FP
Syed Awais Mazhar Bukhari
 
PPTX
Functional Programming in Java
Jim Bethancourt
 
PPTX
Lambdas : Beyond The Basics
Simon Ritter
 
PPTX
java8
Arik Abulafya
 
PPT
14274730 (1).ppt
aptechaligarh
 
PPTX
Intro to java 8
John Godoi
 
PPTX
Java 8 - An Overview
Indrajit Das
 
PPTX
Java 8 Intro - Core Features
GlobalLogic Ukraine
 
PDF
Java 8 - functional features
Rafal Rybacki
 
PPTX
A Brief Conceptual Introduction to Functional Java 8 and its API
Jörn Guy Süß JGS
 
PPTX
Introduction to functional programming with java 8
JavaBrahman
 
PPTX
Week-1..................................
kmjanani05
 
PPTX
Java 8 new features
Aniket Thakur
 
PPTX
Lambda Expressions in Java 8
icarter09
 
PPTX
Functional programming with Java 8
LivePerson
 
PDF
Fun with java 8
Victor Perepelitsky
 
PDF
Apouc 2014-java-8-create-the-future
OUGTH Oracle User Group in Thailand
 
PPTX
SoCal Code Camp 2015: An introduction to Java 8
Chaitanya Ganoo
 
Java 8
vpulec
 
Java8 training - Class 1
Marut Singh
 
Insight into java 1.8, OOP VS FP
Syed Awais Mazhar Bukhari
 
Functional Programming in Java
Jim Bethancourt
 
Lambdas : Beyond The Basics
Simon Ritter
 
14274730 (1).ppt
aptechaligarh
 
Intro to java 8
John Godoi
 
Java 8 - An Overview
Indrajit Das
 
Java 8 Intro - Core Features
GlobalLogic Ukraine
 
Java 8 - functional features
Rafal Rybacki
 
A Brief Conceptual Introduction to Functional Java 8 and its API
Jörn Guy Süß JGS
 
Introduction to functional programming with java 8
JavaBrahman
 
Week-1..................................
kmjanani05
 
Java 8 new features
Aniket Thakur
 
Lambda Expressions in Java 8
icarter09
 
Functional programming with Java 8
LivePerson
 
Fun with java 8
Victor Perepelitsky
 
Apouc 2014-java-8-create-the-future
OUGTH Oracle User Group in Thailand
 
SoCal Code Camp 2015: An introduction to Java 8
Chaitanya Ganoo
 
Ad

More from Lhouceine OUHAMZA (13)

PPTX
Présentation sur internet.pptx
Lhouceine OUHAMZA
 
PPTX
WEB SERVICE SOAP, JAVA, XML, JAXWS
Lhouceine OUHAMZA
 
PDF
Complete Java Course
Lhouceine OUHAMZA
 
PPTX
Prometheus and Grafana
Lhouceine OUHAMZA
 
PPTX
Kubernetes
Lhouceine OUHAMZA
 
PPTX
Scrum course
Lhouceine OUHAMZA
 
PPTX
Jenkins
Lhouceine OUHAMZA
 
PPT
Spring AOP
Lhouceine OUHAMZA
 
PPT
Extreme Programming (XP)
Lhouceine OUHAMZA
 
PPTX
Systemes authentification
Lhouceine OUHAMZA
 
PPT
Spring mvc
Lhouceine OUHAMZA
 
PPTX
Spring ioc
Lhouceine OUHAMZA
 
PPTX
Presentation of framework Angular
Lhouceine OUHAMZA
 
Présentation sur internet.pptx
Lhouceine OUHAMZA
 
WEB SERVICE SOAP, JAVA, XML, JAXWS
Lhouceine OUHAMZA
 
Complete Java Course
Lhouceine OUHAMZA
 
Prometheus and Grafana
Lhouceine OUHAMZA
 
Kubernetes
Lhouceine OUHAMZA
 
Scrum course
Lhouceine OUHAMZA
 
Spring AOP
Lhouceine OUHAMZA
 
Extreme Programming (XP)
Lhouceine OUHAMZA
 
Systemes authentification
Lhouceine OUHAMZA
 
Spring mvc
Lhouceine OUHAMZA
 
Spring ioc
Lhouceine OUHAMZA
 
Presentation of framework Angular
Lhouceine OUHAMZA
 
Ad

Recently uploaded (20)

PDF
Electrical Engineer operation Supervisor
ssaruntatapower143
 
DOC
MRRS Strength and Durability of Concrete
CivilMythili
 
PDF
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
PPT
Carmon_Remote Sensing GIS by Mahesh kumar
DhananjayM6
 
PDF
MAD Unit - 1 Introduction of Android IT Department
JappanMavani
 
PDF
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
PPTX
Lecture 1 Shell and Tube Heat exchanger-1.pptx
mailforillegalwork
 
PPTX
Element 11. ELECTRICITY safety and hazards
merrandomohandas
 
PDF
International Journal of Information Technology Convergence and services (IJI...
ijitcsjournal4
 
PPTX
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
PPTX
The Role of Information Technology in Environmental Protectio....pptx
nallamillisriram
 
PPTX
VITEEE 2026 Exam Details , Important Dates
SonaliSingh127098
 
PDF
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
PPTX
What is Shot Peening | Shot Peening is a Surface Treatment Process
Vibra Finish
 
PDF
Water Industry Process Automation & Control Monthly July 2025
Water Industry Process Automation & Control
 
PPTX
Damage of stability of a ship and how its change .pptx
ehamadulhaque
 
PPTX
Presentation 2.pptx AI-powered home security systems Secure-by-design IoT fr...
SoundaryaBC2
 
PDF
Halide Perovskites’ Multifunctional Properties: Coordination Engineering, Coo...
TaameBerhe2
 
PPTX
265587293-NFPA 101 Life safety code-PPT-1.pptx
chandermwason
 
PDF
Zilliz Cloud Demo for performance and scale
Zilliz
 
Electrical Engineer operation Supervisor
ssaruntatapower143
 
MRRS Strength and Durability of Concrete
CivilMythili
 
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
Carmon_Remote Sensing GIS by Mahesh kumar
DhananjayM6
 
MAD Unit - 1 Introduction of Android IT Department
JappanMavani
 
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
Lecture 1 Shell and Tube Heat exchanger-1.pptx
mailforillegalwork
 
Element 11. ELECTRICITY safety and hazards
merrandomohandas
 
International Journal of Information Technology Convergence and services (IJI...
ijitcsjournal4
 
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
The Role of Information Technology in Environmental Protectio....pptx
nallamillisriram
 
VITEEE 2026 Exam Details , Important Dates
SonaliSingh127098
 
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
What is Shot Peening | Shot Peening is a Surface Treatment Process
Vibra Finish
 
Water Industry Process Automation & Control Monthly July 2025
Water Industry Process Automation & Control
 
Damage of stability of a ship and how its change .pptx
ehamadulhaque
 
Presentation 2.pptx AI-powered home security systems Secure-by-design IoT fr...
SoundaryaBC2
 
Halide Perovskites’ Multifunctional Properties: Coordination Engineering, Coo...
TaameBerhe2
 
265587293-NFPA 101 Life safety code-PPT-1.pptx
chandermwason
 
Zilliz Cloud Demo for performance and scale
Zilliz
 

Functional programming

  • 2. LHOUCEINE Ouhamza: Java Developer, +10 years of IT experience. ©2020 [email protected]
  • 3. 3 Level 1 - beginner Lambda expressions and streams API
  • 4. Imperative programming Object-oriented programming Functional programming Google trends currently ranks functional programming as more popular than Object- oriented programming Programming perspective
  • 5. 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
  • 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 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
  • 8. Programming language which supports functional programming: Programming languages 8
  • 10. ❏ 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
  • 11. Is a interface with a single abstract method interface Welcome { abstract void welcome(String string); } Functional interface 11 @FunctionalInterface annotation is optional.
  • 13. 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
  • 14. 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
  • 15. 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
  • 16. 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
  • 17. 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
  • 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 see the six basic function interfaces. Functional Interface
  • 24. 24
  • 25. 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. 26
  • 27. 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. 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. 29 Clean code: Best practices
  • 30. 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. 31 Huge thanks to you friends

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.