SlideShare a Scribd company logo
FUNCTIONAL
PRINCIPLES
Introduction to Functional Programming
and Java 8
WHY ?
WHY FUNCTIONAL PROGRAMMING ? / MOOREโ€™S LAW
WHY FUNCTIONAL PROGRAMMING ? / MULTI CORE TREND
WHY FUNCTIONAL PROGRAMMING ? / SOME HISTORY
WHAT ?
โ€ข A programming paradigm where
functions are first-class entities
โ€ข The main concepts are:
1. programming with functions
2. avoid mutation
โ€ข A new way of thinking
WHAT IS FP ?
โ€ข Object Immutability
โ€ข Functions:
โ€“ as first class citizens
โ€“ no side effects (Pure functions)
โ€“ Higher Order Functions
โ€ข No loops
โ€ข Lazy evaluation
WHAT IS FP ? / FUNCTIONAL PRINCIPLES
โ€ข Easier parallelization
โ€ข Less code
โ€ข Easy testing
โ€ข Results instead of steps
โ€ข Easy to understand code
WHAT IS FP ? / WHAT YOU GET ?
HOW ?
IMMUTABILITY
An immutable object is an object
whose state cannot be modified
after it is created
IMMUTABLE OBJECTS
โ€œClasses should be immutable unless
thereโ€™s very good reason to make them
mutableโ€ฆ If a class cannot be made
immutable, limit its mutability as much
as possibleโ€
Joshua Bloch
IMMUTABLE OBJECTS
IMMUTABLE OBJECTS / JAVA
IMMUTABLE OBJECTS / DEFENSIVE COPY
IMMUTABLE OBJECTS / OTHER FUNCTIONAL LANGUAGES
IMMUTABLE OBJECTS / OTHER FUNCTIONAL LANGUAGES
IMMUTABLE OBJECTS / HOW TO CHANGE ?
IMMUTABLE OBJECTS / PARALLELISM
โ€ขare thread-safe
โ€ขare simple to construct
โ€ขeasy to test
โ€ขeasy to use
โ€ขfavors caching
IMMUTABLE OBJECTS / PROS
โ€ข Large object graphs
โ€ข Memory consumption
โ€ข Extra garbage collection cycles needed
IMMUTABLE OBJECTS / CONS
FUNCTIONS
F : X โ†’ Y
HIGHER ORDER
FUNCTIONS
(HOF)
HOF = functions that can take other
functions as arguments and / or
return other functions as result
HIGHER ORDER FUNCTIONS / DEFINITION
HIGHER ORDER FUNCTIONS / FIRST CLASS CITIZENS
HIGHER ORDER FUNCTIONS / FUNCTIONS AS PARAMS
HIGHER ORDER FUNCTIONS / RETURN FUNCTIONS
HIGHER ORDER FUNCTIONS / HOFS EXAMPLE IN F#
HIGHER ORDER FUNCTIONS / BENEFITS
โ€ขAllows easy parallelism
โ€ขEncourages abstraction
โ€ขReusing of common code
โ€ขIsolates the essential parts
โ€ขAllows easier unit testing
HIGHER ORDER FUNCTIONS
HIGHER ORDER FUNCTIONS/ HOFS IN JAVA
HIGHER ORDER FUNCTIONS / JAVA CLASSES
HIGHER ORDER FUNCTIONS / USAGE OF JAVA 8 FUNC. CLS.
HIGHER ORDER FUNCTIONS
HIGHER ORDER FUNCTIONS / LAMBDA EXPRESSIONS
โ€ข A lambda expression is an anonymous
method
โ€ข Lambdas favor HOFs
โ€ข more powerful libraries
โ€ข more expressive, more readable, less
error-prone use code
โ€ข Boosts developer productivity
โ€ข key to an accessible parallelism strategy
HIGHER ORDER FUNCTIONS / LAMBDA EXAMPLES 1
HIGHER ORDER FUNCTIONS / LAMBDA EXAMPLES 2
HIGHER ORDER FUNCTIONS / HOF IN JAVA 8
HIGHER ORDER FUNCTIONS
HIGHER ORDER FUNCTIONS / FUNC. INTERFACE EXAMPLE
HIGHER ORDER FUNCTIONS / FUNCTION REFERENCES
CHECKPOINT
FUNCTIONS
(PURE
FUNCTIONS)
A function is said to be pure if
1. it returns same set of values
for same set of inputs
2. It does not have any
observable side effects
PURE FUNCTIONS / DEFINITION
PURE FUNCTIONS / EXAMPLE 1
PURE FUNCTIONS / EXAMPLE 2
Impure functions / Side effects :
1. Alter parameters passed by ref
2. Alter members of passed
objects
3. Alter external objects
PURE FUNCTIONS / SIDE EFFECTS
PURE FUNCTIONS / EXAMPLE 2
โ€ข sin(x)
โ€ข length(a)
โ€ข random()
โ€ข println(String s)
โ€ข Insert values(x, y, z) into DB_TABLE
PURE FUNCTIONS / SAMPLE OF PURE AND IMPURE FNC
โ€ข easier to understand
โ€ข easy maintenance
โ€ข easy testing / unit-testing
โ€ข favor concurrency
PURE FUNCTIONS / PROS
PURE FUNCTIONS / BENEFITS
โ€œNo side effectsโ€ is utopic
PURE FUNCTIONS / CONS
FUNCTION
COMPOSITION
FUNCTION COMPOSITION / MATH
FUNCTION COMPOSITION / SUPPORT IN JAVA 8
FUNCTION COMPOSITION / THE POWER OF COMPOSITION
CHECKPOINT
NO LOOPS
NO LOOPS / RECURSION
NO LOOPS
(RECURSION)
RECURSION / EXAMPLE OF AN ITERATIVE FUNCTION
A recursive function is a
function that calls itself
during its execution
RECURSION / RECURSIVE VS. ITERATION
RECURSION / TAIL RECURSION
Tail recursion = a recursive function
calls itself as its last action
NO LOOPS / RECURSION - TAIL RECURSION
NO LOOPS / RECURSION - TAIL RECURSION OPTIMIZATION
NO LOOPS / RECURSION - RECURSION ENCOURAGED
NO LOOPS
(FUNCTION
CHAINING)
NO LOOPS / FUNCTION CHAINING
โ€ข Similar to unix pipes :
ps -ax | tee processes.txt | more
โ€ข Already used in java in fluent interfaces
โ€ข Eliminate the need for intermediate variables
NO LOOPS / FUNCTION CHAINING
persons.stream()
.filter(e -> e.getGender() == Person.Sex.MALE)
.forEach(e -> System.out.println(e.getName()));
for (Person p : persons) {
if (p.getGender() == Person.Sex.MALE) {
System.out.println(p.getName());
}
}
NO LOOPS / AGGREGATE OPERATIONS
โ€ข They use internal iteration
โ€ข They process elements from a stream
โ€ข They support behavior as parameters
NO LOOPS / FUNCTION CHAINING EXAMPLE
NO LOOPS / FUNCTION CHAINING
NO LOOPS / FUNCTION CHAINING
double average = persons.stream()
.filter(p -> p.getGender() == Person.Sex.MALE)
.mapToInt(Person::getAge)
.average()
.getAsDouble();
FUNCTION CHAINING / DEFAULT METHODS
FUNCTION CHAINING / DEFAULT METHODS
FUNCTIONS CHAINING / STREAMS IN JAVA 8
โ€ข Streams do not provide a means to directly access or
manipulate their elements
โ€ข are concerned with declaratively describing the
computational operations which will be performed in
aggregate on that source
โ€ข No storage: they carry values from a source through a
pipeline
โ€ข Functional in nature ( operations do not modify its
underlying data)
โ€ข Operations can be implemented lazily ( for single pass
execution & efficient implementation of short-circuit
operations)
โ€ข No bounds : streams can be infinite
FUNCTION CHAINING / STREAMS, OPTIONAL, LAZINESS
FUNCTION CHAINING / SEQUENTIAL REDUCE
FUNCTION CHAINING / SEQUENTIAL REDUCE
FUNCTION CHAINING / PARALLEL STREAMS
FUNCTION CHAINING / PARALLEL REDUCE
PERFORMANCE OF PARALLEL PROCESSING
FUNCTION CHAINING / OTHER EXAMPLES
OTHER
FUNCTIONAL
FEATURES
(OPTIONAL)
Shameless copy-paste from
www.oracle.com/technetwork/articles/java/java8-optional-2175753.html
OPTIONAL / WHY OPTIONAL ?
OPTIONAL / NULL, THE BILLION DOLLAR MISTAKE
"I call it my billion-dollar mistake. It was the invention of the
null reference in 1965. [โ€ฆ]
I couldn't resist the temptation to put in a null reference,
simply because it was so easy to implement.
This has led to innumerable errors, vulnerabilities, and system
crashes, which have probably caused a billion dollars of pain
and damage in the last forty yearsโ€œ
Tony Hoare
OPTIONAL / THE SOLUTION TO NULL
java.util.Optional<T> :
โ€ข A class that encapsulates an optional value
โ€ข A single-value container that either contains
a value or doesn't (empty)
OPTIONAL / HOW TO CREATE IT
OPTIONAL / HOW TO USE IT
OPTIONAL / THE MOST IMPORTANT METHODS
OPTIONAL / RETURN TO ORIGINAL EXAMPLE
OPTIONAL / BENEFITS
โ€ข Idiot proof / Clear intent
โ€ข Cleaner code (no more null checks)
โ€ข Encourages method chaining
โ€ข End of NullPointerException
OPTIONAL / CONS
โ€ข Performance
โ€ข Serialization - Optional is not
โ€ข Certain operations involving
parametric polymorphism become
cumbersome
CHECKPOINT
FP CHANGES
EVERYTHING
EVERYTHING CHANGES / THREADS WITH LAMBDAS
EVERYTHING CHANGES / ACTION LISTENERS W/ LAMBDAS
EVERYTHING CHANGES / COMPARATORS
EVERYTHING CHANGES / LIST ITERATION, FILTERING, ETC.
EVERYTHING CHANGES / READING FILES
EVERYTHING CHANGES / SPRING
CHECKPOINT
โ€ข Immutability
โ€ข Higher Order Functions
โ€ข Pure functions
โ€ข No loops ( recursion, function chaining)
โ€ข Lazy evaluation
โ€ข Type inference
โ€ข Parallelism
โ€ข Easy coding / understanding
RECAP / FUNCTIONAL PROGRAMMING
โ€ข Introduced FP features
โ€ข Functional interfaces
โ€ข Function/Predicate/Producer/Consumer
โ€ข Default methods
โ€ข Lambda expressions
โ€ข Streams
โ€ข Map/Reduce/Filter/Collect
โ€ข Type inference
RECAP / JAVA 8
โ€ข Easier parallelization
โ€ข Less code
โ€ข Easy testing
โ€ข Results instead of steps
โ€ข Easy to understand code
WHAT IS FP ? / WHAT YOU GET ?
QUESTIONS
THE END
โ€ข Scheme, Lisp
โ€ข ML, OCaml
โ€ข Haskell
โ€ข Erlang
โ€ข Scala
โ€ข Clojure
โ€ข F#
WHATโ€™S NEXT ? / OTHER FUNCTIONAL LANGUAGES
โ€ขRetrolambda โ€“ backport of
java 8 lambdas in Java 7,6,5
โ€ขfunctionaljava.org
โ€ขGoogle Guava
WHATโ€™S NEXT ? / FP IN JAVA BEFORE JAVA8
โ€ขReactive programming
WHATโ€™S NEXT ? / OTHER TECHNOLOGIES
WHATโ€™S/WHOโ€™S
NEXT ?

More Related Content

What's hot (20)

PDF
Java 8 features
Oleg Tsal-Tsalko
ย 
PPTX
java 8 new features
Rohit Verma
ย 
PDF
Code generating beans in Java
Stephen Colebourne
ย 
PDF
Functional programming in scala
Stratio
ย 
PPTX
10 Sets of Best Practices for Java 8
Garth Gilmour
ย 
PDF
Java 8 โ€‹and โ€‹Best Practices
Buddhini Seneviratne
ย 
PDF
Cracking OCA and OCP Java 8 Exams
Ganesh Samarthyam
ย 
PPTX
OCP Java (OCPJP) 8 Exam Quick Reference Card
Hari kiran G
ย 
PDF
Lambdas in Java 8
Tobias Coetzee
ย 
PPT
Lambdas
malliksunkara
ย 
PDF
Xtend - better java with -less- noise
Neeraj Bhusare
ย 
PDF
Refactoring to Java 8 (Devoxx UK)
Trisha Gee
ย 
PDF
Java SE 8 library design
Stephen Colebourne
ย 
PDF
Java 8 best practices - Stephen Colebourne
JAXLondon_Conference
ย 
PDF
Functional programming with Xtend
Sven Efftinge
ย 
PDF
Lambda: A Peek Under The Hood - Brian Goetz
JAX London
ย 
PDF
Java SE 8 best practices
Stephen Colebourne
ย 
PPTX
Functional programming for the Advanced Beginner
Luis Atencio
ย 
PDF
Java 8 - A step closer to Parallelism
jbugkorea
ย 
PDF
Introduction to c first week slides
luqman bawany
ย 
Java 8 features
Oleg Tsal-Tsalko
ย 
java 8 new features
Rohit Verma
ย 
Code generating beans in Java
Stephen Colebourne
ย 
Functional programming in scala
Stratio
ย 
10 Sets of Best Practices for Java 8
Garth Gilmour
ย 
Java 8 โ€‹and โ€‹Best Practices
Buddhini Seneviratne
ย 
Cracking OCA and OCP Java 8 Exams
Ganesh Samarthyam
ย 
OCP Java (OCPJP) 8 Exam Quick Reference Card
Hari kiran G
ย 
Lambdas in Java 8
Tobias Coetzee
ย 
Lambdas
malliksunkara
ย 
Xtend - better java with -less- noise
Neeraj Bhusare
ย 
Refactoring to Java 8 (Devoxx UK)
Trisha Gee
ย 
Java SE 8 library design
Stephen Colebourne
ย 
Java 8 best practices - Stephen Colebourne
JAXLondon_Conference
ย 
Functional programming with Xtend
Sven Efftinge
ย 
Lambda: A Peek Under The Hood - Brian Goetz
JAX London
ย 
Java SE 8 best practices
Stephen Colebourne
ย 
Functional programming for the Advanced Beginner
Luis Atencio
ย 
Java 8 - A step closer to Parallelism
jbugkorea
ย 
Introduction to c first week slides
luqman bawany
ย 

Viewers also liked (20)

PDF
Functional Java 8 in everyday life
Andrea Iacono
ย 
PPTX
Functional Programming Fundamentals
Shahriar Hyder
ย 
PPTX
Introduction to java
Veerabadra Badra
ย 
PDF
Functional Programming Patterns (BuildStuff '14)
Scott Wlaschin
ย 
PPTX
Features of java
Hitesh Kumar
ย 
PDF
Functional Programming in JAVA 8
Ignasi Marimon-Clos i Sunyol
ย 
PDF
Principles of Object Oriented Programming
Kasun Ranga Wijeweera
ย 
PPTX
Features of java
WILLFREDJOSE W
ย 
PPT
Java features
myrajendra
ย 
PPTX
Uses of Cupcakes For Any Occasion in Hyderabad!
bookthecake.com
ย 
PPTX
Csc1100 lecture01 ch01 pt2-paradigm
IIUM
ย 
PDF
Core FP Concepts
Diego Pacheco
ย 
PPTX
Oop project briefing sem 1 2015 2016
IIUM
ย 
PDF
Java 8 Lambda Expressions
Scott Leberknight
ย 
PPT
Understanding Computers: Today and Tomorrow, 13th Edition Chapter 13 - Progra...
yaminohime
ย 
PPTX
Good Programming Practice
Bikalpa Gyawali
ย 
PDF
Ada 95 - Structured programming
Gneuromante canalada.org
ย 
PDF
Functional Programming in Java - Code for Maintainability
Marcin Stepien
ย 
PPT
Why C is Called Structured Programming Language
Sinbad Konick
ย 
PDF
Programming languages
Eelco Visser
ย 
Functional Java 8 in everyday life
Andrea Iacono
ย 
Functional Programming Fundamentals
Shahriar Hyder
ย 
Introduction to java
Veerabadra Badra
ย 
Functional Programming Patterns (BuildStuff '14)
Scott Wlaschin
ย 
Features of java
Hitesh Kumar
ย 
Functional Programming in JAVA 8
Ignasi Marimon-Clos i Sunyol
ย 
Principles of Object Oriented Programming
Kasun Ranga Wijeweera
ย 
Features of java
WILLFREDJOSE W
ย 
Java features
myrajendra
ย 
Uses of Cupcakes For Any Occasion in Hyderabad!
bookthecake.com
ย 
Csc1100 lecture01 ch01 pt2-paradigm
IIUM
ย 
Core FP Concepts
Diego Pacheco
ย 
Oop project briefing sem 1 2015 2016
IIUM
ย 
Java 8 Lambda Expressions
Scott Leberknight
ย 
Understanding Computers: Today and Tomorrow, 13th Edition Chapter 13 - Progra...
yaminohime
ย 
Good Programming Practice
Bikalpa Gyawali
ย 
Ada 95 - Structured programming
Gneuromante canalada.org
ย 
Functional Programming in Java - Code for Maintainability
Marcin Stepien
ย 
Why C is Called Structured Programming Language
Sinbad Konick
ย 
Programming languages
Eelco Visser
ย 
Ad

Similar to Functional programming principles and Java 8 (20)

PDF
Fun with java 8
Victor Perepelitsky
ย 
PDF
2014 java functional
Demian Neidetcher
ย 
PPTX
Functional programming
Christian Hujer
ย 
PPTX
Functional programming
Prashant Kalkar
ย 
PDF
Functional Programming 101 for Java 7 Developers
Jayaram Sankaranarayanan
ย 
PPTX
Functional Programming in Java
Narendran Solai Sridharan
ย 
PDF
Intro to functional programming - Confoo
felixtrepanier
ย 
PPTX
Insight into java 1.8, OOP VS FP
Syed Awais Mazhar Bukhari
ย 
PPTX
Functional Programming in Java
Jim Bethancourt
ย 
PPTX
Functional programming
Lhouceine OUHAMZA
ย 
PDF
A Functional Approach to Java: Augmenting Object-Oriented Java Code with Func...
romergalbowx
ย 
PPTX
Introduction to Functional Programming
Dave Fancher
ย 
PDF
Java 8 - functional features
Rafal Rybacki
ย 
PPTX
Intro to java 8
John Godoi
ย 
PPT
14274730 (1).ppt
aptechaligarh
ย 
PPTX
The joy of functional programming
Steve Zhang
ย 
PDF
Booting into functional programming
Dhaval Dalal
ย 
PPTX
FUNctional Programming in Java 8
Richard Walker
ย 
PDF
Java 8 Workshop
Mario Fusco
ย 
PDF
Functional programming is the most extreme programming
samthemonad
ย 
Fun with java 8
Victor Perepelitsky
ย 
2014 java functional
Demian Neidetcher
ย 
Functional programming
Christian Hujer
ย 
Functional programming
Prashant Kalkar
ย 
Functional Programming 101 for Java 7 Developers
Jayaram Sankaranarayanan
ย 
Functional Programming in Java
Narendran Solai Sridharan
ย 
Intro to functional programming - Confoo
felixtrepanier
ย 
Insight into java 1.8, OOP VS FP
Syed Awais Mazhar Bukhari
ย 
Functional Programming in Java
Jim Bethancourt
ย 
Functional programming
Lhouceine OUHAMZA
ย 
A Functional Approach to Java: Augmenting Object-Oriented Java Code with Func...
romergalbowx
ย 
Introduction to Functional Programming
Dave Fancher
ย 
Java 8 - functional features
Rafal Rybacki
ย 
Intro to java 8
John Godoi
ย 
14274730 (1).ppt
aptechaligarh
ย 
The joy of functional programming
Steve Zhang
ย 
Booting into functional programming
Dhaval Dalal
ย 
FUNctional Programming in Java 8
Richard Walker
ย 
Java 8 Workshop
Mario Fusco
ย 
Functional programming is the most extreme programming
samthemonad
ย 
Ad

Recently uploaded (20)

PDF
How DeepSeek Beats ChatGPT: Cost Comparison and Key Differences
sumitpurohit810
ย 
PDF
Power BI vs Tableau vs Looker - Which BI Tool is Right for You?
MagnusMinds IT Solution LLP
ย 
PPTX
CONCEPT OF PROGRAMMING in language .pptx
tamim41
ย 
PDF
IDM Crack with Internet Download Manager 6.42 Build 41
utfefguu
ย 
PDF
Designing Accessible Content Blocks (1).pdf
jaclynmennie1
ย 
PDF
>Wondershare Filmora Crack Free Download 2025
utfefguu
ย 
PDF
capitulando la keynote de GrafanaCON 2025 - Madrid
Imma Valls Bernaus
ย 
PPTX
CV-Project_2024 version 01222222222.pptx
MohammadSiddiqui70
ย 
PPTX
NeuroStrata: Harnessing Neuro-Symbolic Paradigms for Improved Testability and...
Ivan Ruchkin
ย 
PDF
WholeClear Split vCard Software for Split large vCard file
markwillsonmw004
ย 
PDF
Building scalbale cloud native apps with .NET 8
GillesMathieu10
ย 
PDF
AI Software Development Process, Strategies and Challenges
Net-Craft.com
ย 
PPTX
Quality on Autopilot: Scaling Testing in Uyuni
Oscar Barrios Torrero
ย 
PPTX
For my supp to finally picking supp that work
necas19388
ย 
PPTX
IObit Uninstaller Pro 14.3.1.8 Crack Free Download 2025
sdfger qwerty
ย 
PPTX
Iobit Driver Booster Pro 12 Crack Free Download
chaudhryakashoo065
ย 
PPTX
Seamless-Image-Conversion-From-Raster-to-wrt-rtx-rtx.pptx
Quick Conversion Services
ย 
PDF
Dealing with JSON in the relational world
Andres Almiray
ย 
PDF
Automated Test Case Repair Using Language Models
Lionel Briand
ย 
PPTX
Introduction to web development | MERN Stack
JosephLiyon
ย 
How DeepSeek Beats ChatGPT: Cost Comparison and Key Differences
sumitpurohit810
ย 
Power BI vs Tableau vs Looker - Which BI Tool is Right for You?
MagnusMinds IT Solution LLP
ย 
CONCEPT OF PROGRAMMING in language .pptx
tamim41
ย 
IDM Crack with Internet Download Manager 6.42 Build 41
utfefguu
ย 
Designing Accessible Content Blocks (1).pdf
jaclynmennie1
ย 
>Wondershare Filmora Crack Free Download 2025
utfefguu
ย 
capitulando la keynote de GrafanaCON 2025 - Madrid
Imma Valls Bernaus
ย 
CV-Project_2024 version 01222222222.pptx
MohammadSiddiqui70
ย 
NeuroStrata: Harnessing Neuro-Symbolic Paradigms for Improved Testability and...
Ivan Ruchkin
ย 
WholeClear Split vCard Software for Split large vCard file
markwillsonmw004
ย 
Building scalbale cloud native apps with .NET 8
GillesMathieu10
ย 
AI Software Development Process, Strategies and Challenges
Net-Craft.com
ย 
Quality on Autopilot: Scaling Testing in Uyuni
Oscar Barrios Torrero
ย 
For my supp to finally picking supp that work
necas19388
ย 
IObit Uninstaller Pro 14.3.1.8 Crack Free Download 2025
sdfger qwerty
ย 
Iobit Driver Booster Pro 12 Crack Free Download
chaudhryakashoo065
ย 
Seamless-Image-Conversion-From-Raster-to-wrt-rtx-rtx.pptx
Quick Conversion Services
ย 
Dealing with JSON in the relational world
Andres Almiray
ย 
Automated Test Case Repair Using Language Models
Lionel Briand
ย 
Introduction to web development | MERN Stack
JosephLiyon
ย 

Functional programming principles and Java 8

Editor's Notes

  • #2: Ce ne propunem ? Prezentarea este destul de ambitioasa pentru ca ataca doua tinte: functional programming si java8 most important features. Focusul va fi pe FP. Disclaimer: - codul in F#
  • #4: The evolution of the clock speed over time. Unul din factorii care contribuiau la imbunatatirea puterii de calcul si-a oprit cresterea. Este prima data cand legea lui Moore este pusa sub semnul intrebarii.
  • #5: Hardware-ul se schimba -> software-ul treโ€™ sa se schimbe pentru a tine pasul. Articol: The free lunch is over : - processor manufacturers will focus on products that better support multithreading (such as multi-core processors) - software developers will be forced to develop massively multithreaded programs as a way to better use such processors (i.e: proasta calitate a codului, nu mai poate fi acoperita de imbunatatirea vitezei de calcul) Codul nostru va rula distribuit intre core-urile procesorului.
  • #6: Evolutia limbajelor de programare. A se nota faptul ca limbajele functionale au aparut cu mult inaintea limbajelor OOP. Principiile din limbajele functionale se mapeaza mult mai bine pe ideea de multi threading / paralelism.
  • #9: Principiile FP derivate din cele doua concepte prezentate anterior
  • #10: Easier parallelization != No work for parallelization Results instead of steps -> SQL Verbe in locul substantivelor
  • #14: Imutabilitatea nu e ceva nou. Este recomandata si-n OOP.
  • #15: No setters Final fields Final class Java examples ?
  • #16: Un exemplu mai complex. Unul din campurile clasei este mutabil. Ultima metoda returneaza o copie defensiva pentru a evita mutabilitatea.
  • #17: Exemplu de clasa imutabila in Scala.
  • #18: Exemplu de clasa imutabila in F#. Pentru a face un obiect mutabil trebuie utilizat cuvantul cheie โ€œmutableโ€
  • #19: Talk about the memory consumption and the extra work to be done by the garbage collector. Extrapolate the example above to lists, trees, etc.
  • #21: Other benefits: - don't need a copy constructor - don't need an implementation of clone - allow hashCode to use lazy initialization, and to cache its return value - don't need to be copied defensively when used as a field - make good Map keys and Set elements (these objects must not change state while in the collection) - always have "failure atomicity" : if an immutable object throws an exception, it's never left in an undesirable or indeterminate state
  • #22: In loc de obiectul rational de mai devreme sa ne gandim ca avem o lista.add Garbage collection: ok atata timp cat nu lucrezi la Twitter.
  • #25: This cannot be achieved in Java but โ€ฆ talk about the new functional interfaces ; Predicate, Filter, โ€ฆ
  • #26: HOFs can be assigned to variables HOFs can be created at runtime and used Functions are just as any other data types
  • #27: Sum accepts another function as input parameter
  • #28: higherOrderFunction returns a function as a result
  • #29: Functions as first class citizens Functions as parameters Functions as return values
  • #30: sumOfSquares -> easy abstraction โ€“> sum (f) Easy parallelism
  • #32: A new package added in java8 In java everything is a class -> functions are classes
  • #33: The most important java classes in the java.util.function package All introduced in Java8
  • #34: Cam asa am fi utilizat clasele in Java 7
  • #35: Note the lambda expression No boilerplate Explain the type inference
  • #36: Remember the anonymous classes in java
  • #37: Examples with the most important classes implemented as lambda expressions
  • #40: Cum acoperim toate situatiile ? Prin annotation : FunctionalInterface A functional interface has only one abstract method. Instances of functional interfaces can be created with lambda or method references
  • #41: This is how we use the Functional Interface annotation. Note: the lambda expression used to define an anonymous definition of a Functional Interface
  • #42: Cum utilizam metodele deja existente ? De observat referintele la metodele din clasele Math, Integer.
  • #43: HOF Lambda Function references
  • #44: Pure functions = No side effects
  • #46: Impure function - has side effects
  • #47: Impure function -> it doesnโ€™t return the same values for the same inputs
  • #50: Sin = pure Length = pure Random() = impure Println() = impure SQL Insert = impure
  • #51: Easier to maintain: devs spend less time analyzing the impact Once tested all edge conditions we can be sure that the function behaves correctly Easy concurency: see next side/example
  • #52: No side effects favorizeaza paralelismul The same input -> same output favorizeaza testarea si intelegerea
  • #53: Any write to the console is a side-effect. Database updates or file writes on disk is a side-effect. So we cannot be 100% pure but the goal is to be as pure as possible. In an input โ€“ processโ€“ output flow the goal is to keep the middle (process) functional.
  • #55: g o f (c) = #
  • #56: Metodele compose si andThen
  • #58: Pure functions Function composition HOF Lambdas Function references
  • #60: What is the problem ? Incurajeaza shared state (variabilele partajate) = nu bine pt. paralelism Mult boilerplate
  • #61: Why avoid loops ? boilerplate code Incurajeaza partajarea state-ului deci nu este prietenos cu multi-threadingul.
  • #63: In some cases ( most of them ? ) recursion is more intuitive then iteration. Functional languages have better implementations for recursion For OOP languages iteration is much faster than recursion The mantra of functional languages: CLARITY TRUMPS EFFICIENCY (preferam claritatea vs. eficienta)
  • #64: the functionโ€™s stack frame can be reused. Remember stiva de executie a unei functii.
  • #65: After the call to factorial(n-1) there is still work to be done => not a tail recursive action
  • #66: Note: Since the stack trace is optimized, when printing the stack trace one will only see the last call instead of the entire stack trace.
  • #67: Functional languages have better support for recursion (see the list.head, list.tail) This is not possible with all lists in idiomatic java.
  • #68: Function chaining = Un caz particular de compozitie
  • #69: To be discussed here: Imperative approach vs. Functional approach : For vs. map-reduce State vs. stateless
  • #70: Discussion about state / share state ( in a multi-threaded env.) : letโ€™s sum the salaries of males in a multi-threaded env. Func. Chaining can be seen as a particular case of composition.
  • #71: Discussion about state / share state ( in a multi-threaded env.) : letโ€™s sum the salaries of males in a multi-threaded env. Func. Chaining can be seen as a particular case of composition.
  • #72: Note the chaining of combinator methods : map, filter, reduce/foldLeft
  • #73: The same functionality in Java8. Imperative vs. Declarative style / Ce face functia vs. Cum face functia / The SQL Example. Note 1: the stream() method Note 2: a method added to a the list interface (DEFAULT METHODS discussion) Discussion about map/filter/reduce Weโ€™ll come back to map/reduce/filter in a few moments
  • #74: The average age of males in the persons list. When using streams we need the following components: 1 A source ( list ) 2.Zero or more intermediate operations ( filters, transformers) 3. A terminal operation ( average)
  • #75: Default Method Discussion 2. Stream interface
  • #76: The Stream interface and the map / reduce methods
  • #77: Streams cannot be reused. Please check the Stream javadoc . http://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html#StreamOps
  • #78: The stream could have been infinite I could have used only lambdas Note the findFirst method returning an optional (discussed later in this material) Re-start the map/reduce discussion
  • #79: Reduce needs to be associative (a+b)+c = a+(b+c), i.e. the order in which the additions are performed doesn't matter.
  • #80: How sequential reduce works.
  • #81: Free parallelism but this doesnโ€™t happen every time Sum() is a particular form of reduce() โ€“ a shortcut for reduce(0, (a,b) -> a+b) reduce, collect, sum = terminal methods Note: Order of filtering matters
  • #82: Note: the operation has to be associative otherwise the result will not be consistent (no compilation or runtime error) Example of a non associative operation: x*x + y*y Discussion about non-associative
  • #83: Disclaimer: the table shows the test results in ideal conditions ( no other threads were running) - in production systems you wonโ€™t get this kind of difference
  • #84: The power of collectors
  • #87: The problem with null: Depending on the context, null means โ€œno valueโ€, other times it means โ€œerrorโ€ or โ€œnothingโ€, but it can even mean โ€œsuccessโ€
  • #88: Optional is inspired from Haskel and Scala
  • #93: Idiot proof : It forces you to actively think about the absent case if you want your program to compile There are three ways to deal with the absence of a value in an Optional: to provide a substitute value, to call a function to provide a substitute value, or to throw an exception
  • #95: Functional programming = no loops Recursivitate Function chaining Function composition Streams Map / reduce Default methods Optional
  • #96: Functional programming = no loops Recursivitate Function chaining ( this is also related to composition) Streams Map / reduce
  • #97: Lambda instead of a Runnable Runnable este o interfata functionala
  • #98: Lambda instead of an Action Listener
  • #99: Lambda instead of a Comparator. Why the Users type has been specified ? - where is the type inference ?
  • #100: Method reference in a forEach method
  • #101: BufferedReader.lines() returns a Stream Files.lines() returns a Stream as well. The examples shows how to use the stream in a try-with-resources ( it implements AutoClosable).
  • #102: Spring jdbc template with lambda expressions ( instead of RowMapper.mapRow(ResultSet, int rowNum)
  • #104: Outside the scope : Advanced Laziness Monads Function currying
  • #105: Outside the scope : Java8 ------------------------------------ Type Annotations Date and time API Lambda translation to bytecode Nashorn Javascript engine
  • #106: Easier parallelization != No work for parallelization What instead of How Verbe in loc de substantive
  • #108: Some non-programming mistakes have been intentionally inserted into the presentation
  • #111: Responsive: The system responds in a timely manner Resilient: The system stays responsive even in failure Elastic: The service allocates resources as needed ( according to the workload) Message Driven: Async message passing for loose coupling, isolation, transparency