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 (20)

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
 
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
 
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
 
Call by value
Call by valueCall by value
Call by value
Dharani G
 
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
 

Similar to Functional Programming with Javascript (20)

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
Function
Kathmandu University
 
Столпы функционального программирования для адептов ООП, Николай Мозговой
Столпы функционального программирования для адептов ООП, Николай МозговойСтолпы функционального программирования для адептов ООП, Николай Мозговой
Столпы функционального программирования для адептов ООП, Николай Мозговой
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
 
22 scheme OOPs with C++ BCS306B_module3.pdf
22 scheme  OOPs with C++ BCS306B_module3.pdf22 scheme  OOPs with C++ BCS306B_module3.pdf
22 scheme OOPs with C++ BCS306B_module3.pdf
sindhus795217
 
SeneJug java_8_prez_122015
SeneJug java_8_prez_122015SeneJug java_8_prez_122015
SeneJug java_8_prez_122015
senejug
 
C++ questions And Answer
C++ questions And AnswerC++ questions And Answer
C++ questions And Answer
lavparmar007
 
C++ Interview Question And Answer
C++ Interview Question And AnswerC++ Interview Question And Answer
C++ Interview Question And Answer
Jagan Mohan Bishoyi
 
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 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 ✔
 
Столпы функционального программирования для адептов ООП, Николай Мозговой
Столпы функционального программирования для адептов ООП, Николай МозговойСтолпы функционального программирования для адептов ООП, Николай Мозговой
Столпы функционального программирования для адептов ООП, Николай Мозговой
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
 
22 scheme OOPs with C++ BCS306B_module3.pdf
22 scheme  OOPs with C++ BCS306B_module3.pdf22 scheme  OOPs with C++ BCS306B_module3.pdf
22 scheme OOPs with C++ BCS306B_module3.pdf
sindhus795217
 
SeneJug java_8_prez_122015
SeneJug java_8_prez_122015SeneJug java_8_prez_122015
SeneJug java_8_prez_122015
senejug
 
C++ questions And Answer
C++ questions And AnswerC++ questions And Answer
C++ questions And Answer
lavparmar007
 
C++ Interview Question And Answer
C++ Interview Question And AnswerC++ Interview Question And Answer
C++ Interview Question And Answer
Jagan Mohan Bishoyi
 
Ad

Recently uploaded (20)

Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...
Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...
Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...
Precisely
 
Introduction to Internet of things .ppt.
Introduction to Internet of things .ppt.Introduction to Internet of things .ppt.
Introduction to Internet of things .ppt.
hok12341073
 
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdfBoosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Alkin Tezuysal
 
Your startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean accountYour startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean account
angelo60207
 
Oracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI ProfessionalOracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI Professional
VICTOR MAESTRE RAMIREZ
 
PyData - Graph Theory for Multi-Agent Integration
PyData - Graph Theory for Multi-Agent IntegrationPyData - Graph Theory for Multi-Agent Integration
PyData - Graph Theory for Multi-Agent Integration
barqawicloud
 
How Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdf
How Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdfHow Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdf
How Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdf
Rejig Digital
 
AI Agents in Logistics and Supply Chain Applications Benefits and Implementation
AI Agents in Logistics and Supply Chain Applications Benefits and ImplementationAI Agents in Logistics and Supply Chain Applications Benefits and Implementation
AI Agents in Logistics and Supply Chain Applications Benefits and Implementation
Christine Shepherd
 
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdfcnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
AmirStern2
 
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc Webinar - 2025 Global Privacy SurveyTrustArc Webinar - 2025 Global Privacy Survey
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc
 
Introduction to Typescript - GDG On Campus EUE
Introduction to Typescript - GDG On Campus EUEIntroduction to Typescript - GDG On Campus EUE
Introduction to Typescript - GDG On Campus EUE
Google Developer Group On Campus European Universities in Egypt
 
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven InfrastructureNo-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
Safe Software
 
Developing Schemas with FME and Excel - Peak of Data & AI 2025
Developing Schemas with FME and Excel - Peak of Data & AI 2025Developing Schemas with FME and Excel - Peak of Data & AI 2025
Developing Schemas with FME and Excel - Peak of Data & AI 2025
Safe Software
 
Crypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdfCrypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdf
Stephen Perrenod
 
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Shashikant Jagtap
 
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Impelsys Inc.
 
If You Use Databricks, You Definitely Need FME
If You Use Databricks, You Definitely Need FMEIf You Use Databricks, You Definitely Need FME
If You Use Databricks, You Definitely Need FME
Safe Software
 
Your startup on AWS - How to architect and maintain a Lean and Mean account J...
Your startup on AWS - How to architect and maintain a Lean and Mean account J...Your startup on AWS - How to architect and maintain a Lean and Mean account J...
Your startup on AWS - How to architect and maintain a Lean and Mean account J...
angelo60207
 
Providing an OGC API Processes REST Interface for FME Flow
Providing an OGC API Processes REST Interface for FME FlowProviding an OGC API Processes REST Interface for FME Flow
Providing an OGC API Processes REST Interface for FME Flow
Safe Software
 
Oracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI FoundationsOracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI Foundations
VICTOR MAESTRE RAMIREZ
 
Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...
Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...
Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...
Precisely
 
Introduction to Internet of things .ppt.
Introduction to Internet of things .ppt.Introduction to Internet of things .ppt.
Introduction to Internet of things .ppt.
hok12341073
 
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdfBoosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Alkin Tezuysal
 
Your startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean accountYour startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean account
angelo60207
 
Oracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI ProfessionalOracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI Professional
VICTOR MAESTRE RAMIREZ
 
PyData - Graph Theory for Multi-Agent Integration
PyData - Graph Theory for Multi-Agent IntegrationPyData - Graph Theory for Multi-Agent Integration
PyData - Graph Theory for Multi-Agent Integration
barqawicloud
 
How Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdf
How Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdfHow Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdf
How Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdf
Rejig Digital
 
AI Agents in Logistics and Supply Chain Applications Benefits and Implementation
AI Agents in Logistics and Supply Chain Applications Benefits and ImplementationAI Agents in Logistics and Supply Chain Applications Benefits and Implementation
AI Agents in Logistics and Supply Chain Applications Benefits and Implementation
Christine Shepherd
 
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdfcnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
AmirStern2
 
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc Webinar - 2025 Global Privacy SurveyTrustArc Webinar - 2025 Global Privacy Survey
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc
 
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven InfrastructureNo-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
Safe Software
 
Developing Schemas with FME and Excel - Peak of Data & AI 2025
Developing Schemas with FME and Excel - Peak of Data & AI 2025Developing Schemas with FME and Excel - Peak of Data & AI 2025
Developing Schemas with FME and Excel - Peak of Data & AI 2025
Safe Software
 
Crypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdfCrypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdf
Stephen Perrenod
 
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Shashikant Jagtap
 
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Impelsys Inc.
 
If You Use Databricks, You Definitely Need FME
If You Use Databricks, You Definitely Need FMEIf You Use Databricks, You Definitely Need FME
If You Use Databricks, You Definitely Need FME
Safe Software
 
Your startup on AWS - How to architect and maintain a Lean and Mean account J...
Your startup on AWS - How to architect and maintain a Lean and Mean account J...Your startup on AWS - How to architect and maintain a Lean and Mean account J...
Your startup on AWS - How to architect and maintain a Lean and Mean account J...
angelo60207
 
Providing an OGC API Processes REST Interface for FME Flow
Providing an OGC API Processes REST Interface for FME FlowProviding an OGC API Processes REST Interface for FME Flow
Providing an OGC API Processes REST Interface for FME Flow
Safe Software
 
Oracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI FoundationsOracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI Foundations
VICTOR MAESTRE RAMIREZ
 
Ad

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.