SlideShare a Scribd company logo
A FUNCTIONAL APPROACH FOR BETTER AND
FASTER JAVA.
Jason O’Regan
OVERVIEW
 What is Functional Programming? Why Now?
 Examples of functional code in Java 8
 Is it better and faster?
WHAT IS FUNCTIONAL PROGRAMMING?
Different
Paradigm
WHAT IS FUNCTIONAL PROGRAMMING?
First Class
Citizen
WHAT IS FUNCTIONAL PROGRAMMING?
No Side Effects
FUNCTIONAL PROGRAMMING EXAMPLE
val x = 1 + 2
val y = 2 + 3
-- Side Effect
val x = { print("foo"); 1 + 2 }
val y = { print("bar"); 2 + 3 }
WHAT IS FUNCTIONAL PROGRAMMING?
Execution Order
Does not
matter
IMPERATIVE PROGRAMMING EXAMPLE
public class SideEffectClass {
private int state = 0;
public void doSomething(int arg0) {
state += arg0;
}
}
WHY FUNCTIONAL PROGRAMMING NOW?
Rise of the Machines
WHY FUNCTIONAL PROGRAMMING NOW?
WHY FUNCTIONAL PROGRAMMING NOW?
WHY FUNCTIONAL PROGRAMMING IN JAVA?
WHY FUNCTIONAL PROGRAMMING IN JAVA?
WHY FUNCTIONAL PROGRAMMING IN JAVA?
IMPERATIVE JAVA EXAMPLE
private static final int THRESHOLD = 20;
private static final double EXCHANGE_RATE = 1.5d;
final List<BigDecimal> prices = Arrays.asList(new BigDecimal("11"), new
BigDecimal("40"), new BigDecimal("17.5"), new BigDecimal("21"), new
BigDecimal("13"), new BigDecimal("22"), new BigDecimal("56"));
BigDecimal totalOfPrices = BigDecimal.ZERO;
for (BigDecimal price : prices) {
if(price.compareTo(BigDecimal.valueOf(THRESHOLD)) > 0){
totalOfPrices =
totalOfPrices.add(price.multiply(BigDecimal.valueOf(EXCHANGE_RATE)));
}
}
System.out.println("Total of fx prices: " + totalOfPrices);
FUNCTIONAL JAVA EXAMPLE
final BigDecimal totalOfPrices = prices.stream()
.filter(price -> price.compareTo(BigDecimal.valueOf(THRESHOLD)) > 0)
.map(price -> price.multiply(BigDecimal.valueOf(EXCHANGE_RATE)))
.reduce(BigDecimal.ZERO, BigDecimal::add);
System.out.println("Total of fx prices: " + totalOfPrices);
IS IT BETTER?
Clarity
IS IT BETTER?
Maintenance
IS IT BETTER?
Less Bugs
IS IT FASTER?
IS IT FASTER?
Lazy Evaluation of Streams
IS IT FASTER?
Concurrency
FUNCTIONAL JAVA PARALLEL EXAMPLE
final BigDecimal totalOfPrices = prices.stream()
.parallel()
.filter(price -> price.compareTo(BigDecimal.valueOf(THRESHOLD)) > 0)
.map(price -> price.multiply(BigDecimal.valueOf(EXCHANGE_RATE)))
.reduce(BigDecimal.ZERO, BigDecimal::add);
System.out.println("Total of fx prices: " + totalOfPrices);
IS IT FASTER?
FUNCTIONAL JAVA PRIMES EXAMPLE
500,000 random integral values, search for the maximum
value
int m = Integer.MIN_VALUE;
for (int i : myList)
if (i>m) m=i;
int m = myList.stream().reduce(Integer.MIN_VALUE,
Math::max);
ArrayList, for-loop : 6.55 ms
ArrayList, seq. stream: 8.33 ms
ArrayList, Parallel 3.35ms
IS IT FASTER?
Sequential streams are no faster
than loops.
Streams are easier to parallelize.
IS IT FASTER?
GOOD PRACTICES
GOOD PRACTICES
Avoid
GOOD PRACTICES
Prefer Expressions
GOOD PRACTICES
Design with
Higher-Order
Functions
WHAT’S IN IT FOR ME?
WHAT’S IN IT FOR ME?
Worth the Investment
RESOURCES
RESOURCES
 OO vs FP - http://blog.cleancoder.com/
 Parallel Streams and Spliterators -
https://thecannycoder.wordpress.com
 How fast are Java 8 Streams? - Angelika Langer
 Lambdas and Streams in Java 8 Libraries – Brian
Goetz

More Related Content

What's hot

POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
Aiman Hud
 
Introduction of Object Oriented JavaScript
Introduction of Object Oriented JavaScriptIntroduction of Object Oriented JavaScript
Introduction of Object Oriented JavaScript
NexThoughts Technologies
 
Inline and lambda function
Inline and lambda functionInline and lambda function
Inline and lambda function
Jawad Khan
 
C Language - Switch and For Loop
C Language - Switch and For LoopC Language - Switch and For Loop
C Language - Switch and For Loop
Sukrit Gupta
 
Factor Graph & Sum-Product Algorithm
Factor Graph & Sum-Product AlgorithmFactor Graph & Sum-Product Algorithm
Factor Graph & Sum-Product Algorithm
Xixiu Ouyang
 
Promises - Asynchronous Control Flow
Promises - Asynchronous Control FlowPromises - Asynchronous Control Flow
Promises - Asynchronous Control Flow
Henrique Barcelos
 
Data structure scope of variables
Data structure scope of variablesData structure scope of variables
Data structure scope of variables
Saurav Kumar
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
Mohammed Khan
 
Presentation on nesting of loops
Presentation on nesting of loopsPresentation on nesting of loops
Presentation on nesting of loops
bsdeol28
 
Returning Data
Returning DataReturning Data
Returning Data
primeteacher32
 
Journey of a C# developer into Javascript
Journey of a C# developer into JavascriptJourney of a C# developer into Javascript
Journey of a C# developer into Javascript
Massimo Franciosa
 
Loops in java script
Loops in java scriptLoops in java script
Loops in java script
Ravi Bhadauria
 
Cold fusion best practice
Cold fusion best practiceCold fusion best practice
Cold fusion best practice
isummation
 
Introduction to C Programming
Introduction to C Programming Introduction to C Programming
Introduction to C Programming
vampugani
 
Loop control in c++
Loop control in c++Loop control in c++
Loop control in c++
Debre Tabor University
 
Short intro to ECMAScript
Short intro to ECMAScriptShort intro to ECMAScript
Short intro to ECMAScript
Jussi Pohjolainen
 
Reloj en java
Reloj en javaReloj en java
Reloj en java
cathe26
 
Iterative control structures, looping, types of loops, loop working
Iterative control structures, looping, types of loops, loop workingIterative control structures, looping, types of loops, loop working
Iterative control structures, looping, types of loops, loop working
Neeru Mittal
 
Conditional Control in MATLAB Scripts
Conditional Control in MATLAB ScriptsConditional Control in MATLAB Scripts
Conditional Control in MATLAB Scripts
Shameer Ahmed Koya
 
Cd
CdCd

What's hot (20)

POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
Introduction of Object Oriented JavaScript
Introduction of Object Oriented JavaScriptIntroduction of Object Oriented JavaScript
Introduction of Object Oriented JavaScript
 
Inline and lambda function
Inline and lambda functionInline and lambda function
Inline and lambda function
 
C Language - Switch and For Loop
C Language - Switch and For LoopC Language - Switch and For Loop
C Language - Switch and For Loop
 
Factor Graph & Sum-Product Algorithm
Factor Graph & Sum-Product AlgorithmFactor Graph & Sum-Product Algorithm
Factor Graph & Sum-Product Algorithm
 
Promises - Asynchronous Control Flow
Promises - Asynchronous Control FlowPromises - Asynchronous Control Flow
Promises - Asynchronous Control Flow
 
Data structure scope of variables
Data structure scope of variablesData structure scope of variables
Data structure scope of variables
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 
Presentation on nesting of loops
Presentation on nesting of loopsPresentation on nesting of loops
Presentation on nesting of loops
 
Returning Data
Returning DataReturning Data
Returning Data
 
Journey of a C# developer into Javascript
Journey of a C# developer into JavascriptJourney of a C# developer into Javascript
Journey of a C# developer into Javascript
 
Loops in java script
Loops in java scriptLoops in java script
Loops in java script
 
Cold fusion best practice
Cold fusion best practiceCold fusion best practice
Cold fusion best practice
 
Introduction to C Programming
Introduction to C Programming Introduction to C Programming
Introduction to C Programming
 
Loop control in c++
Loop control in c++Loop control in c++
Loop control in c++
 
Short intro to ECMAScript
Short intro to ECMAScriptShort intro to ECMAScript
Short intro to ECMAScript
 
Reloj en java
Reloj en javaReloj en java
Reloj en java
 
Iterative control structures, looping, types of loops, loop working
Iterative control structures, looping, types of loops, loop workingIterative control structures, looping, types of loops, loop working
Iterative control structures, looping, types of loops, loop working
 
Conditional Control in MATLAB Scripts
Conditional Control in MATLAB ScriptsConditional Control in MATLAB Scripts
Conditional Control in MATLAB Scripts
 
Cd
CdCd
Cd
 

Viewers also liked

Second language teaching methods
Second language teaching methodsSecond language teaching methods
Second language teaching methods
fadilaAli
 
Silent way
Silent waySilent way
The audiolingual method and the silent way
The audiolingual method and the silent wayThe audiolingual method and the silent way
The audiolingual method and the silent way
Rene Israel Murillo Jimenez
 
The Audio-lingual method and The Silent Way
The Audio-lingual method and The Silent WayThe Audio-lingual method and The Silent Way
The Audio-lingual method and The Silent Way
bruna_fontoura
 
Silent way
Silent waySilent way
Silent way
Fabiolalenguasmz
 
Coparison of three teaching methods
Coparison of three teaching methodsCoparison of three teaching methods
Coparison of three teaching methods
mahdiehosseini
 
The silent way
The silent wayThe silent way
The silent way
Nenden Meidiana
 
The silent way
The silent wayThe silent way
The silent way
Taghreeed
 
Silent way method
Silent way methodSilent way method
Silent way method
Istianatul Khusniyah
 
SILENT WAY
SILENT WAYSILENT WAY
The silent way Approach
The silent way ApproachThe silent way Approach
The silent way Approach
Amer Najmi
 
Silent Way Teaching Method
Silent Way Teaching MethodSilent Way Teaching Method
Silent Way Teaching Method
Ahmet Ateş
 
Total Physical Response
Total Physical ResponseTotal Physical Response
Total Physical Response
ignorantdavinci
 
Suggestopedia
SuggestopediaSuggestopedia
Suggestopedia
Sylwia Drymajło
 

Viewers also liked (14)

Second language teaching methods
Second language teaching methodsSecond language teaching methods
Second language teaching methods
 
Silent way
Silent waySilent way
Silent way
 
The audiolingual method and the silent way
The audiolingual method and the silent wayThe audiolingual method and the silent way
The audiolingual method and the silent way
 
The Audio-lingual method and The Silent Way
The Audio-lingual method and The Silent WayThe Audio-lingual method and The Silent Way
The Audio-lingual method and The Silent Way
 
Silent way
Silent waySilent way
Silent way
 
Coparison of three teaching methods
Coparison of three teaching methodsCoparison of three teaching methods
Coparison of three teaching methods
 
The silent way
The silent wayThe silent way
The silent way
 
The silent way
The silent wayThe silent way
The silent way
 
Silent way method
Silent way methodSilent way method
Silent way method
 
SILENT WAY
SILENT WAYSILENT WAY
SILENT WAY
 
The silent way Approach
The silent way ApproachThe silent way Approach
The silent way Approach
 
Silent Way Teaching Method
Silent Way Teaching MethodSilent Way Teaching Method
Silent Way Teaching Method
 
Total Physical Response
Total Physical ResponseTotal Physical Response
Total Physical Response
 
Suggestopedia
SuggestopediaSuggestopedia
Suggestopedia
 

Similar to Functional programming in java

Functional Programming with Javascript
Functional Programming with JavascriptFunctional Programming with Javascript
Functional Programming with Javascript
Deepankar Chopra
 
Rx workshop
Rx workshopRx workshop
Rx workshop
Ryan Riley
 
Effective Java with Groovy & Kotlin How Languages Influence Adoption of Good ...
Effective Java with Groovy & Kotlin How Languages Influence Adoption of Good ...Effective Java with Groovy & Kotlin How Languages Influence Adoption of Good ...
Effective Java with Groovy & Kotlin How Languages Influence Adoption of Good ...
Naresha K
 
Operators
OperatorsOperators
Operators
loidasacueza
 
Beyond Java: 자바 8을 중심으로 본 자바의 혁신
Beyond Java: 자바 8을 중심으로 본 자바의 혁신Beyond Java: 자바 8을 중심으로 본 자바의 혁신
Beyond Java: 자바 8을 중심으로 본 자바의 혁신
Sungchul Park
 
No More Promises! Let's RxJS!
No More Promises! Let's RxJS!No More Promises! Let's RxJS!
No More Promises! Let's RxJS!
Ilia Idakiev
 
Reactive Programming - ReactFoo 2020 - Aziz Khambati
Reactive Programming - ReactFoo 2020 - Aziz KhambatiReactive Programming - ReactFoo 2020 - Aziz Khambati
Reactive Programming - ReactFoo 2020 - Aziz Khambati
Aziz Khambati
 
What is new in java 8 concurrency
What is new in java 8 concurrencyWhat is new in java 8 concurrency
What is new in java 8 concurrency
kshanth2101
 
The... Wonderful? World of Lambdas
The... Wonderful? World of LambdasThe... Wonderful? World of Lambdas
The... Wonderful? World of Lambdas
Esther Lozano
 
What you need to know about Lambdas - Jamie Allen
What you need to know about Lambdas - Jamie AllenWhat you need to know about Lambdas - Jamie Allen
What you need to know about Lambdas - Jamie Allen
jaxconf
 
379008-rc217-functionalprogramming
379008-rc217-functionalprogramming379008-rc217-functionalprogramming
379008-rc217-functionalprogramming
Luis Atencio
 
Cycle.js - Functional reactive UI framework (Nikos Kalogridis)
Cycle.js - Functional reactive UI framework (Nikos Kalogridis)Cycle.js - Functional reactive UI framework (Nikos Kalogridis)
Cycle.js - Functional reactive UI framework (Nikos Kalogridis)
GreeceJS
 
Cycle.js - A functional reactive UI framework
Cycle.js - A functional reactive UI frameworkCycle.js - A functional reactive UI framework
Cycle.js - A functional reactive UI framework
Nikos Kalogridis
 
FP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyondFP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyond
Mario Fusco
 
Functional programming in javascript
Functional programming in javascriptFunctional programming in javascript
Functional programming in javascript
Boris Burdiliak
 
Functional programming in Javascript
Functional programming in JavascriptFunctional programming in Javascript
Functional programming in Javascript
Knoldus Inc.
 
Twins: Object Oriented Programming and Functional Programming
Twins: Object Oriented Programming and Functional ProgrammingTwins: Object Oriented Programming and Functional Programming
Twins: Object Oriented Programming and Functional Programming
RichardWarburton
 
Intro to Javascript
Intro to JavascriptIntro to Javascript
Intro to Javascript
Anjan Banda
 
No more promises lets RxJS 2 Edit
No more promises lets RxJS 2 EditNo more promises lets RxJS 2 Edit
No more promises lets RxJS 2 Edit
Ilia Idakiev
 
Java 8 Workshop
Java 8 WorkshopJava 8 Workshop
Java 8 Workshop
Mario Fusco
 

Similar to Functional programming in java (20)

Functional Programming with Javascript
Functional Programming with JavascriptFunctional Programming with Javascript
Functional Programming with Javascript
 
Rx workshop
Rx workshopRx workshop
Rx workshop
 
Effective Java with Groovy & Kotlin How Languages Influence Adoption of Good ...
Effective Java with Groovy & Kotlin How Languages Influence Adoption of Good ...Effective Java with Groovy & Kotlin How Languages Influence Adoption of Good ...
Effective Java with Groovy & Kotlin How Languages Influence Adoption of Good ...
 
Operators
OperatorsOperators
Operators
 
Beyond Java: 자바 8을 중심으로 본 자바의 혁신
Beyond Java: 자바 8을 중심으로 본 자바의 혁신Beyond Java: 자바 8을 중심으로 본 자바의 혁신
Beyond Java: 자바 8을 중심으로 본 자바의 혁신
 
No More Promises! Let's RxJS!
No More Promises! Let's RxJS!No More Promises! Let's RxJS!
No More Promises! Let's RxJS!
 
Reactive Programming - ReactFoo 2020 - Aziz Khambati
Reactive Programming - ReactFoo 2020 - Aziz KhambatiReactive Programming - ReactFoo 2020 - Aziz Khambati
Reactive Programming - ReactFoo 2020 - Aziz Khambati
 
What is new in java 8 concurrency
What is new in java 8 concurrencyWhat is new in java 8 concurrency
What is new in java 8 concurrency
 
The... Wonderful? World of Lambdas
The... Wonderful? World of LambdasThe... Wonderful? World of Lambdas
The... Wonderful? World of Lambdas
 
What you need to know about Lambdas - Jamie Allen
What you need to know about Lambdas - Jamie AllenWhat you need to know about Lambdas - Jamie Allen
What you need to know about Lambdas - Jamie Allen
 
379008-rc217-functionalprogramming
379008-rc217-functionalprogramming379008-rc217-functionalprogramming
379008-rc217-functionalprogramming
 
Cycle.js - Functional reactive UI framework (Nikos Kalogridis)
Cycle.js - Functional reactive UI framework (Nikos Kalogridis)Cycle.js - Functional reactive UI framework (Nikos Kalogridis)
Cycle.js - Functional reactive UI framework (Nikos Kalogridis)
 
Cycle.js - A functional reactive UI framework
Cycle.js - A functional reactive UI frameworkCycle.js - A functional reactive UI framework
Cycle.js - A functional reactive UI framework
 
FP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyondFP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyond
 
Functional programming in javascript
Functional programming in javascriptFunctional programming in javascript
Functional programming in javascript
 
Functional programming in Javascript
Functional programming in JavascriptFunctional programming in Javascript
Functional programming in Javascript
 
Twins: Object Oriented Programming and Functional Programming
Twins: Object Oriented Programming and Functional ProgrammingTwins: Object Oriented Programming and Functional Programming
Twins: Object Oriented Programming and Functional Programming
 
Intro to Javascript
Intro to JavascriptIntro to Javascript
Intro to Javascript
 
No more promises lets RxJS 2 Edit
No more promises lets RxJS 2 EditNo more promises lets RxJS 2 Edit
No more promises lets RxJS 2 Edit
 
Java 8 Workshop
Java 8 WorkshopJava 8 Workshop
Java 8 Workshop
 

Recently uploaded

KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CDKuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
rodomar2
 
Unveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdfUnveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdf
brainerhub1
 
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
dakas1
 
The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...
The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...
The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...
kalichargn70th171
 
Fundamentals of Programming and Language Processors
Fundamentals of Programming and Language ProcessorsFundamentals of Programming and Language Processors
Fundamentals of Programming and Language Processors
Rakesh Kumar R
 
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
safelyiotech
 
SQL Accounting Software Brochure Malaysia
SQL Accounting Software Brochure MalaysiaSQL Accounting Software Brochure Malaysia
SQL Accounting Software Brochure Malaysia
GohKiangHock
 
Hand Rolled Applicative User Validation Code Kata
Hand Rolled Applicative User ValidationCode KataHand Rolled Applicative User ValidationCode Kata
Hand Rolled Applicative User Validation Code Kata
Philip Schwarz
 
WWDC 2024 Keynote Review: For CocoaCoders Austin
WWDC 2024 Keynote Review: For CocoaCoders AustinWWDC 2024 Keynote Review: For CocoaCoders Austin
WWDC 2024 Keynote Review: For CocoaCoders Austin
Patrick Weigel
 
14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision
ShulagnaSarkar2
 
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
XfilesPro
 
Energy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina JonuziEnergy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina Jonuzi
Green Software Development
 
How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?
ToXSL Technologies
 
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, FactsALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
Green Software Development
 
YAML crash COURSE how to write yaml file for adding configuring details
YAML crash COURSE how to write yaml file for adding configuring detailsYAML crash COURSE how to write yaml file for adding configuring details
YAML crash COURSE how to write yaml file for adding configuring details
NishanthaBulumulla1
 
All you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVMAll you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVM
Alina Yurenko
 
GreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-JurisicGreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-Jurisic
Green Software Development
 
Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)
Julian Hyde
 
How to write a program in any programming language
How to write a program in any programming languageHow to write a program in any programming language
How to write a program in any programming language
Rakesh Kumar R
 
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
mz5nrf0n
 

Recently uploaded (20)

KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CDKuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
 
Unveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdfUnveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdf
 
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
 
The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...
The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...
The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...
 
Fundamentals of Programming and Language Processors
Fundamentals of Programming and Language ProcessorsFundamentals of Programming and Language Processors
Fundamentals of Programming and Language Processors
 
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
 
SQL Accounting Software Brochure Malaysia
SQL Accounting Software Brochure MalaysiaSQL Accounting Software Brochure Malaysia
SQL Accounting Software Brochure Malaysia
 
Hand Rolled Applicative User Validation Code Kata
Hand Rolled Applicative User ValidationCode KataHand Rolled Applicative User ValidationCode Kata
Hand Rolled Applicative User Validation Code Kata
 
WWDC 2024 Keynote Review: For CocoaCoders Austin
WWDC 2024 Keynote Review: For CocoaCoders AustinWWDC 2024 Keynote Review: For CocoaCoders Austin
WWDC 2024 Keynote Review: For CocoaCoders Austin
 
14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision
 
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
 
Energy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina JonuziEnergy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina Jonuzi
 
How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?
 
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, FactsALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
 
YAML crash COURSE how to write yaml file for adding configuring details
YAML crash COURSE how to write yaml file for adding configuring detailsYAML crash COURSE how to write yaml file for adding configuring details
YAML crash COURSE how to write yaml file for adding configuring details
 
All you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVMAll you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVM
 
GreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-JurisicGreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-Jurisic
 
Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)
 
How to write a program in any programming language
How to write a program in any programming languageHow to write a program in any programming language
How to write a program in any programming language
 
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
 

Functional programming in java

Editor's Notes

  1. Check everyone can hear. Quick introduction on myself Java developer for 15 years experience
  2. This slide tells what is going to be covered in the presentation itself Brief overview of Functional Programming and how it is different to Java. A simple java code example in imperative style and then functional. Examining the pros + cons of doing it this way.
  3. FP is just a different programming paradigm Have we got many functional programmers here today? Java 8, Scala, Clojure Roots in mathematics What are the facets of FP?
  4. 1. Nothing to do with civil rights movement 2. Means the language supports passing functions as arguments to other functions.
  5. Purely functional functions have no side effects. Basically no state modification – e.g. Of arguments or global state – Immutable state. Data is transformed rather than modified. Order of execution
  6. Side effects example in scala With No side effects – we can evaluate the expressions in any order In example 2 we cannot reverse the order of evaluation
  7. Execution Order does not matter as there are no side effects in functions. This has an implication with regard to multi-threading
  8. Java is an imperative language This means it uses statements to reach a certain goal – It change the programming state. Each statement is executed in sequence
  9. Has side effects Result will be different dependent on the input order called if called multiple times.
  10. Functional programming has been around for a long time. Lisp was around in late 1950s Bigger, faster machines, multicore platforms – make use of the hardware – parallel processing
  11. Facilitates the ability to pass functions remotely to operate on data locally. By removing side-effects and mutability – facilitates code distribution over several CPUs – makes concurrent programming easier.
  12. New languages – make it easier to adopt. It have becomes more mainstream – Twitter, linked In etc.
  13. Is it just Java jumping on the bandwagon? C# has lambdas, Scala etc – shouldn’t we do something?
  14. Let give them some nicer syntax like other languages?
  15. Big question for me as a java developer – is what is in it for me – if I learn this new stuff Let’s See
  16. Going to give you a simple example written imperatively and then rewrite it in functional style. A Simple example – given a list of prices – convert with the exchange rate and add the ones above a certain threshold. Starts with the mutable variable and loops through the prices and any that pass the threshold – multiplies by the exchange rate and add those to the total. It prints: Total of fx prices: 208.5
  17. 1. This example uses stream and lambdas from java 8. 2. There is a higher level of abstraction here – which is closer to the requirement and easier to understand 3. Read aloud – filter the prices greater than threshold, map the prices to the exchange rate and add them up. 4. Instead of iterating over prices we invoked stream() method on list which gives access to a special iterator and functions like filter() and map(). 5. Stream: Represents a sequence of values and exposes a set of aggregate operations that allow us to express common manipulations on these values easily and clearly 6. Libraries have been updated to provide convenient ways to obtain stream view. Stream operations chain together into pipelines 6. These functions take a lambda expression(anonymous function)
  18. More concise and clear –but more succinct. Matches requirements better
  19. less code to write, read, maintain. Free of low level stuff. - libraries take care of iteration.
  20. Free of low level stuff. - libraries take care of iteration. Avoided mutation – often a cause of bugs. Personally I have seen a lot of instances of mutable state in Domain objects being a big cause of bugs.
  21. Less code, faster to develop for coder once you are used to it Less time fixing bugs and maintaining
  22. Streams use lazy evaluation Intermediate operations like filter /mapping can be thought of as lazy. Facilitates single pass execution on entire pipeline In example you fuse filter + map+ reduce - lazy operations are being applied to aggregates. Each stage takes a stream of input values, performs some transformation on it and passes the values to the next stage in the pipeline. Value producing operations can be thought of as eager Methods like anyMatch or findfirst while eager can use short-circuiting to stop processing once they can determine a final result. Processing streams lazily enables the Java compiler and runtime to optimize how they process streams Good article in Dr Dobbs from Brian Goetz on Lambdas and streams.
  23. Easier to add concurrency
  24. 1. The same example – multithreaded. Just added Parallel(). 2. Very fast way of Parallelizing. 3. Brian Goetz – “Our goal is explicit but unobtrusive parallelism” - make it easier but not invisible
  25. Well 2 articles shine some light on this – cannycoder and how fast are java 8 streams. Neither are conclusive and it all depends on circumstances.
  26. 1. Example taken from “How fast are Java 8 streams?” Angelika Langer 2. Setup – Some outdated hardware – dualcore with proper warmup. 3. So JIT compiler has had a lot of time to optimise for loops and streams are new, so compiler does not yet perform sophisticated optimizations yet. 4. Also only comparing integers – if the functionality applied to each element is cpu intensive – the difference is likely to be insignificant. 5.
  27. Streams are sometimes slower than loops – but they can be equally fast, it depends on the circumstances. But sequential streams are no faster than loops. Conclusion : For sequential streams you are going because you like the style and it is easier to maintain Streams are easy to add parallel()
  28. CannyCoder - Parallel – is faster then non multi-threaded - but custom multi-threading is faster - a lot more error prone though You might also conclude that parallel stream is quicker than sequential. – depends on splitability of the stream source, Another aspect is statefulness of stream – e.g. Distinct() operation. In order to decide if distinct need to compare to each element. Could be significantly slower than sequential. Performance model of streams is not trivial, so you can’t generalize . You need to be informed on how streams work. You need to benchmark your solution.
  29. A lot of these practices are good practices anyway. Avoid mutability where possible – avoid setters – builders, defensive copies. – see Effective java
  30. Functions that have side effects impose ordering. Make program more stateless where possible.
  31. Statements perform actions but don’t return anything, whereas expressions perform actions and return a result Again minimizing statefulness.
  32. Pass functions as arguments More concise and in line with business requirements. lightweight approach to separating concerns Recommend reading – chapter 4 on designing with Lambdas expressions. Also good book Functional programming patterns in Scala and Clojure
  33. Writing more concise and expressive code. Code that is easier to maintain. Easier Concurrency. Another tool in the toolbox.
  34. Invest some time in yourself – it is worth the investment. Will make you a better programmer