SlideShare a Scribd company logo
Functional Programming
With JavaScript
Deepankar Chopra
Senior Software Engineer
Ticketmaster
» What is Functional Programming?
» Mutations
» Side-Effects
» Pure Functions
» Higher Order Functions
» Composition
» Advantages & Disadvantages of FP
» How does FP differ from Object-Oriented programming
2Overview
What is Functional Programming?
Functional programming (often abbreviated FP) is the process of
building software by
» composing pure functions,
» avoiding
» shared state,
» mutable data
» side-effects.
» Managing state flows through composition
3
Mutations
Mutable is a type of variable that can be changed after it is created.
In JavaScript, only objects and arrays are mutable, not primitive
values.
(You can make a variable name point to a new value, but the
previous value is still held in memory.)
4
Mutations 5
let a = { foo: 'bar' };
let b = a;
a.foo = 'test’;
console.log(a,b);
//{foo: "test"} {foo: "test"}
// avoiding mutation
let a = { foo: 'bar' };
let b = Object.assign({}, a);
a.foo = 'test’;
console.log(a,b);
//{foo: "test"} {foo: "bar"}
Side Effects
If a program / function modifies the state of something else
(outside its own scope), then it is producing a side effect.
This could effectively include something like "display a character on
the screen", or it could be changing the value stored in some
arbitrary RAM location, or anything similar.
6
Side Effects include
» Modifying any external variable or object property (e.g., a global
variable, or a variable in the parent function scope chain)
» Logging to the console
» Writing to the screen
» Writing to a file
» Writing to the network
» Triggering any external process
» Calling any other functions with side-effects
7
Pure Functions
» Must return a value
» Can’t produce any side-effects
» Must return the same output for a given input
8
Pure Functions Example 9
let a = 10;
function impure1(x) {
return x + a;
}
function impure2(x) {
a = 30;
return x + a;
}
console.log(impure1(10));//20
a+=10;
console.log(impure1(10));//30
console.log(impure2(10));//40
function pure(x, y) {
return x + y;
}
console.log(pure(10,10));
Composition
Function Composition is a mathematical concept, by which the
result of one function becomes the input of the next, and so on.
Composing functions together is like putting together a series of
pipes for our data to flow through.
10
Composition Example
function addOne(x) {
return x + 1;
}
function timesTwo(x) {
return x * 2;
}
console.log(addOne(timesTwo(3))); //7
11
Higher Order Functions
A higher-order function is a function that does at least one of the
following:
» takes one or more functions as arguments
» returns a function as its result.
12
Higher Order Functions Example
function greaterThan(n) {
return function(m) { return m > n; };
}
let greaterThan10 = greaterThan(10);
let greaterThan11 = greaterThan(11);
console.log(greaterThan10(11));//true
console.log(greaterThan11(11));//false
13
Advantages of FP
» Pure functions are easier to reason about
» Testing is easier, and pure functions lend themselves well to
techniques like property-based testing
» Debugging is easier
» Programs are more bulletproof
» Programs are written at a higher level, and are therefore easier
to comprehend
» Parallel/concurrent programming is easier
14
Disadvantages of FP
» Not suitable in every situation
» Needs to be clubbed with Object oriented programming in some
cases.
» Makes heavy use of recursion
15
How does FP differ from OOP 16
Functional Programming
» Primary Manipulation Unit is
“Function”
» Stateless Programming Model
» Functions with No-Side Effects
» Order of execution is less
importance
Object Oriented Programming
» Primary Manipulation Unit is
Objects(Instances of Classes)
» Stateful Programming Model
» Methods with Side Effects
» Order of execution is more
important.
Any questions?
You can mail me at
deepankar.chopra@gmail.com
17THANKS!

More Related Content

What's hot

Learning C++ - Functions in C++ 3
Learning C++ - Functions  in C++ 3Learning C++ - Functions  in C++ 3
Learning C++ - Functions in C++ 3
Ali Aminian
 
Anonymous and Inline Functions in MATLAB
Anonymous and Inline Functions in MATLABAnonymous and Inline Functions in MATLAB
Anonymous and Inline Functions in MATLAB
Shameer Ahmed Koya
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
home
 
Review functions
Review functionsReview functions
Review functions
Kgr Sushmitha
 
C++ lecture 03
C++   lecture 03C++   lecture 03
C++ lecture 03
HNDE Labuduwa Galle
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
Sachin Sharma
 
Lecture#7 Call by value and reference in c++
Lecture#7 Call by value and reference in c++Lecture#7 Call by value and reference in c++
Lecture#7 Call by value and reference in c++
NUST Stuff
 
User defined Functions in MATLAB Part 1
User defined Functions in MATLAB Part 1User defined Functions in MATLAB Part 1
User defined Functions in MATLAB Part 1
Shameer Ahmed Koya
 
C++ Function
C++ FunctionC++ Function
C++ Function
Hajar
 
Call by value or call by reference in C++
Call by value or call by reference in C++Call by value or call by reference in C++
Call by value or call by reference in C++
Sachin Yadav
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
Mohammed Sikander
 
Call by value
Call by valueCall by value
Call by value
Dharani G
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
Nikhil Pandit
 
Functions in C
Functions in CFunctions in C
Functions in C
Princy Nelson
 
C++ overloading
C++ overloadingC++ overloading
C++ overloading
sanya6900
 
Functions in c
Functions in cFunctions in c
Functions in c
Innovative
 
18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator
SAFFI Ud Din Ahmad
 
Function C++
Function C++ Function C++
Function C++
Shahzad Afridi
 
C and C++ functions
C and C++ functionsC and C++ functions
C and C++ functions
kavitha muneeshwaran
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
Pranali Chaudhari
 

What's hot (20)

Learning C++ - Functions in C++ 3
Learning C++ - Functions  in C++ 3Learning C++ - Functions  in C++ 3
Learning C++ - Functions in C++ 3
 
Anonymous and Inline Functions in MATLAB
Anonymous and Inline Functions in MATLABAnonymous and Inline Functions in MATLAB
Anonymous and Inline Functions in MATLAB
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Review functions
Review functionsReview functions
Review functions
 
C++ lecture 03
C++   lecture 03C++   lecture 03
C++ lecture 03
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Lecture#7 Call by value and reference in c++
Lecture#7 Call by value and reference in c++Lecture#7 Call by value and reference in c++
Lecture#7 Call by value and reference in c++
 
User defined Functions in MATLAB Part 1
User defined Functions in MATLAB Part 1User defined Functions in MATLAB Part 1
User defined Functions in MATLAB Part 1
 
C++ Function
C++ FunctionC++ Function
C++ Function
 
Call by value or call by reference in C++
Call by value or call by reference in C++Call by value or call by reference in C++
Call by value or call by reference in C++
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Call by value
Call by valueCall by value
Call by value
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Functions in C
Functions in CFunctions in C
Functions in C
 
C++ overloading
C++ overloadingC++ overloading
C++ overloading
 
Functions in c
Functions in cFunctions in c
Functions in c
 
18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator
 
Function C++
Function C++ Function C++
Function C++
 
C and C++ functions
C and C++ functionsC and C++ functions
C and C++ functions
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 

Similar to Functional Programming with Javascript

Functional programming 101
Functional programming 101Functional programming 101
Functional programming 101
Maneesh Chaturvedi
 
From object oriented to functional domain modeling
From object oriented to functional domain modelingFrom object oriented to functional domain modeling
From object oriented to functional domain modeling
Codemotion
 
From object oriented to functional domain modeling
From object oriented to functional domain modelingFrom object oriented to functional domain modeling
From object oriented to functional domain modeling
Mario Fusco
 
Intro to Javascript
Intro to JavascriptIntro to Javascript
Intro to Javascript
Anjan Banda
 
Generalized Functors - Realizing Command Design Pattern in C++
Generalized Functors - Realizing Command Design Pattern in C++Generalized Functors - Realizing Command Design Pattern in C++
Generalized Functors - Realizing Command Design Pattern in C++
ppd1961
 
Functional programming
Functional programmingFunctional programming
Functional programming
Prashant Kalkar
 
Functional programming
Functional programmingFunctional programming
Functional programming
Christian Hujer
 
functions
functionsfunctions
functions
Makwana Bhavesh
 
Functional programming in java
Functional programming in javaFunctional programming in java
Functional programming in java
Jason O'Regan
 
JavaScript Interview Questions 2023
JavaScript Interview Questions 2023JavaScript Interview Questions 2023
JavaScript Interview Questions 2023
Laurence Svekis ✔
 
Functional programming
Functional programmingFunctional programming
Functional programming
S M Asaduzzaman
 
Operators
OperatorsOperators
Operators
loidasacueza
 
Function
Function Function
Столпы функционального программирования для адептов ООП, Николай Мозговой
Столпы функционального программирования для адептов ООП, Николай МозговойСтолпы функционального программирования для адептов ООП, Николай Мозговой
Столпы функционального программирования для адептов ООП, Николай Мозговой
Sigma Software
 
ZIO: Powerful and Principled Functional Programming in Scala
ZIO: Powerful and Principled Functional Programming in ScalaZIO: Powerful and Principled Functional Programming in Scala
ZIO: Powerful and Principled Functional Programming in Scala
Wiem Zine Elabidine
 
Closures
ClosuresClosures
Closures
prashanthbabu07
 
SeneJug java_8_prez_122015
SeneJug java_8_prez_122015SeneJug java_8_prez_122015
SeneJug java_8_prez_122015
senejug
 
C++ Interview Question And Answer
C++ Interview Question And AnswerC++ Interview Question And Answer
C++ Interview Question And Answer
Jagan Mohan Bishoyi
 
C++ questions And Answer
C++ questions And AnswerC++ questions And Answer
C++ questions And Answer
lavparmar007
 
Fp201 unit5 1
Fp201 unit5 1Fp201 unit5 1
Fp201 unit5 1
rohassanie
 

Similar to Functional Programming with Javascript (20)

Functional programming 101
Functional programming 101Functional programming 101
Functional programming 101
 
From object oriented to functional domain modeling
From object oriented to functional domain modelingFrom object oriented to functional domain modeling
From object oriented to functional domain modeling
 
From object oriented to functional domain modeling
From object oriented to functional domain modelingFrom object oriented to functional domain modeling
From object oriented to functional domain modeling
 
Intro to Javascript
Intro to JavascriptIntro to Javascript
Intro to Javascript
 
Generalized Functors - Realizing Command Design Pattern in C++
Generalized Functors - Realizing Command Design Pattern in C++Generalized Functors - Realizing Command Design Pattern in C++
Generalized Functors - Realizing Command Design Pattern in C++
 
Functional programming
Functional programmingFunctional programming
Functional programming
 
Functional programming
Functional programmingFunctional programming
Functional programming
 
functions
functionsfunctions
functions
 
Functional programming in java
Functional programming in javaFunctional programming in java
Functional programming in java
 
JavaScript Interview Questions 2023
JavaScript Interview Questions 2023JavaScript Interview Questions 2023
JavaScript Interview Questions 2023
 
Functional programming
Functional programmingFunctional programming
Functional programming
 
Operators
OperatorsOperators
Operators
 
Function
Function Function
Function
 
Столпы функционального программирования для адептов ООП, Николай Мозговой
Столпы функционального программирования для адептов ООП, Николай МозговойСтолпы функционального программирования для адептов ООП, Николай Мозговой
Столпы функционального программирования для адептов ООП, Николай Мозговой
 
ZIO: Powerful and Principled Functional Programming in Scala
ZIO: Powerful and Principled Functional Programming in ScalaZIO: Powerful and Principled Functional Programming in Scala
ZIO: Powerful and Principled Functional Programming in Scala
 
Closures
ClosuresClosures
Closures
 
SeneJug java_8_prez_122015
SeneJug java_8_prez_122015SeneJug java_8_prez_122015
SeneJug java_8_prez_122015
 
C++ Interview Question And Answer
C++ Interview Question And AnswerC++ Interview Question And Answer
C++ Interview Question And Answer
 
C++ questions And Answer
C++ questions And AnswerC++ questions And Answer
C++ questions And Answer
 
Fp201 unit5 1
Fp201 unit5 1Fp201 unit5 1
Fp201 unit5 1
 

Recently uploaded

HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
panagenda
 
Serial Arm Control in Real Time Presentation
Serial Arm Control in Real Time PresentationSerial Arm Control in Real Time Presentation
Serial Arm Control in Real Time Presentation
tolgahangng
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
名前 です男
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
Matthew Sinclair
 
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
Neo4j
 
RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
KAMESHS29
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
Adtran
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
mikeeftimakis1
 
“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”
Claudio Di Ciccio
 
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
SOFTTECHHUB
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
Zilliz
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
Octavian Nadolu
 
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
Edge AI and Vision Alliance
 
Infrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI modelsInfrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI models
Zilliz
 
How to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For FlutterHow to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For Flutter
Daiki Mogmet Ito
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Paige Cruz
 
Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
innovationoecd
 
UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5
DianaGray10
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
Uni Systems S.M.S.A.
 

Recently uploaded (20)

HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
 
Serial Arm Control in Real Time Presentation
Serial Arm Control in Real Time PresentationSerial Arm Control in Real Time Presentation
Serial Arm Control in Real Time Presentation
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
 
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
 
RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
 
“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”
 
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
 
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
 
Infrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI modelsInfrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI models
 
How to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For FlutterHow to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For Flutter
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
 
Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
 
UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
 

Functional Programming with Javascript

  • 1. Functional Programming With JavaScript Deepankar Chopra Senior Software Engineer Ticketmaster
  • 2. » What is Functional Programming? » Mutations » Side-Effects » Pure Functions » Higher Order Functions » Composition » Advantages & Disadvantages of FP » How does FP differ from Object-Oriented programming 2Overview
  • 3. What is Functional Programming? Functional programming (often abbreviated FP) is the process of building software by » composing pure functions, » avoiding » shared state, » mutable data » side-effects. » Managing state flows through composition 3
  • 4. Mutations Mutable is a type of variable that can be changed after it is created. In JavaScript, only objects and arrays are mutable, not primitive values. (You can make a variable name point to a new value, but the previous value is still held in memory.) 4
  • 5. Mutations 5 let a = { foo: 'bar' }; let b = a; a.foo = 'test’; console.log(a,b); //{foo: "test"} {foo: "test"} // avoiding mutation let a = { foo: 'bar' }; let b = Object.assign({}, a); a.foo = 'test’; console.log(a,b); //{foo: "test"} {foo: "bar"}
  • 6. Side Effects If a program / function modifies the state of something else (outside its own scope), then it is producing a side effect. This could effectively include something like "display a character on the screen", or it could be changing the value stored in some arbitrary RAM location, or anything similar. 6
  • 7. Side Effects include » Modifying any external variable or object property (e.g., a global variable, or a variable in the parent function scope chain) » Logging to the console » Writing to the screen » Writing to a file » Writing to the network » Triggering any external process » Calling any other functions with side-effects 7
  • 8. Pure Functions » Must return a value » Can’t produce any side-effects » Must return the same output for a given input 8
  • 9. Pure Functions Example 9 let a = 10; function impure1(x) { return x + a; } function impure2(x) { a = 30; return x + a; } console.log(impure1(10));//20 a+=10; console.log(impure1(10));//30 console.log(impure2(10));//40 function pure(x, y) { return x + y; } console.log(pure(10,10));
  • 10. Composition Function Composition is a mathematical concept, by which the result of one function becomes the input of the next, and so on. Composing functions together is like putting together a series of pipes for our data to flow through. 10
  • 11. Composition Example function addOne(x) { return x + 1; } function timesTwo(x) { return x * 2; } console.log(addOne(timesTwo(3))); //7 11
  • 12. Higher Order Functions A higher-order function is a function that does at least one of the following: » takes one or more functions as arguments » returns a function as its result. 12
  • 13. Higher Order Functions Example function greaterThan(n) { return function(m) { return m > n; }; } let greaterThan10 = greaterThan(10); let greaterThan11 = greaterThan(11); console.log(greaterThan10(11));//true console.log(greaterThan11(11));//false 13
  • 14. Advantages of FP » Pure functions are easier to reason about » Testing is easier, and pure functions lend themselves well to techniques like property-based testing » Debugging is easier » Programs are more bulletproof » Programs are written at a higher level, and are therefore easier to comprehend » Parallel/concurrent programming is easier 14
  • 15. Disadvantages of FP » Not suitable in every situation » Needs to be clubbed with Object oriented programming in some cases. » Makes heavy use of recursion 15
  • 16. How does FP differ from OOP 16 Functional Programming » Primary Manipulation Unit is “Function” » Stateless Programming Model » Functions with No-Side Effects » Order of execution is less importance Object Oriented Programming » Primary Manipulation Unit is Objects(Instances of Classes) » Stateful Programming Model » Methods with Side Effects » Order of execution is more important.
  • 17. Any questions? You can mail me at deepankar.chopra@gmail.com 17THANKS!