SlideShare a Scribd company logo
Damian Łabas
Frontend Developer
Functional Programming in JS
Numer telefonu:
+48 788 266 229
Adres e-mail:
damian.labas@codibly.com
Meetup: JS dla zaawansowanych!
Autor: Damian Łabas
Functional Programming in JS
www.codibly.com
JavaScript
Procedural Programming
Object-Oriented Programming
Functional Programming
4
What is Functional Programming?
Imperative vs Declarative
In Imperative Programming, you write down how to do it.
In Declarative Programming, you write down what to do it.
6
What is in JS?
• First class functions
• Anonymous functions
• Closures
www.codibly.com
What is missing in JS?
• Recursion
• Immutability
• Purity
www.codibly.com
9
First class function
• They can by passed around
• They can be assigned to variables
• They can be stored in more complex data
structure, like array or objects
www.codibly.com
11
Closures
Closures - example
www.codibly.com
function grandParent (g1, g2) {
const g3 = 3;
return function parent (p1, p2) {
const p3 = 33;
return function child(c1, c2) {
const c3 = 333;
return g1 + g2 + g3 + p1 + p2 + p3 + c1 + c2 + c3
}
}
}
Closures - usage
www.codibly.com
const parentFunc = grandParent(1, 2); // return parent()
const childFunc = parentFunc(11, 22); // return child()
console.log(childFunc(111, 222)); // return 738
14
Recursion
Purity
Pure function - examples
www.codibly.com
function coin () {
return Math.random() < 0.5 ? 'heads' : 'tails'
}
let firstName = 'Damian';
function uppercaseName (lastName) {
return `${firstName.toUpperCase()} ${lastName.toUpperCase()}`
}
function calculatePrice (unitPrce, noOfUnits, couponValue = 0) {
return unitPrce * noOfUnits - couponValue;
}
17
Why pure function are better than impure?
Advantages of pure functions
• They are easier to read
• They are easier to test
• They can by more performant
www.codibly.com
19
20
Side Effects
• Modifying any external variable or object property
• Logging to the console
• Writing to file
• Call to API
www.codibly.com
22
Immutability
Immutability in JS
www.codibly.com
const freezeObject = Object.freeze({
foo: 'Hello',
bar: 'world',
baz: '!'
});
const freezeObject = Object.freeze({
foo: { greeting: 'Hello' },
bar: 'world',
baz: '!'
});
24
Higher-order Functions
Higher-order Functions either take functions as parameters, return functions
or both
Higher-order Function
• Abstract or isolate actions
• Async flow control using callback functions
• Promises
• Create utilities which can act on a wide variety
of data types
www.codibly.com
Higher-order Function - own map implementation
www.codibly.com
const arr1 = [1, 2, 3];
function mapForEach (arr, fn) {
const newArray = [];
for (let i = 0; i < arr.length; i++) {
newArray.push(fn(arr[i]))
}
return newArray;
}
const result = mapForEach(arr1,function (item) {
return item * 2
});
27
Function composition
Function composition - example
www.codibly.com
const users = [
{name: 'Damian', age: 23},
{name: 'Andrzej', age: 14},
{name: 'Dominika', age: 18}
];
const filter = (fn, arr) => arr.filter(fn);
const map = (fn, arr) => arr.map(fn);
map(user => user.name, filter(user => user.age >= 18)); // ['Damian,
'Dominika']
Higher-order function - compose and pipe
www.codibly.com
const compose = (...functions) => data =>
functions.reduceRight((value, fn) => fn(value), data);
const pipe = (...functions) => data =>
functions.reduce((value, fn) => fn(value), data);
30
Currying
Currying transforms a function into a sequence of functions each taking a
single argument of the function
Currying - example
www.codibly.com
function multiply (a, b, c) {
return a * b * c
}
function multiply (a) {
return function (b) {
return function (c) {
return a * b * c;
}
}
}
What is a partial application
or partial application function
33
What is a difference?
Is currying useful?
Partial application and currying - example
www.codibly.com
function discount (price, discount) {
return price * discount
}
function discount (discount) {
return function (price) {
return price * discount;
}
}
const tenPercentDiscunt = discount(0.1);
36
Questions?
Thank you
for your time!
www.codibly.com
/in/codibly
/codibly
/codibly
/codibly

More Related Content

PDF
Introduction into ES6 JavaScript.
boyney123
 
PDF
Why Vue.js?
Jonathan Goode
 
PPTX
An introduction to Vue.js
Pagepro
 
PPTX
React web development
Rully Ramanda
 
PDF
Angular
Lilia Sfaxi
 
PPTX
Introduction to HTML+CSS+Javascript.pptx
KADAMBARIPUROHIT
 
PPTX
laravel.pptx
asif290119
 
PDF
The Point of Vue - Intro to Vue.js
Holly Schinsky
 
Introduction into ES6 JavaScript.
boyney123
 
Why Vue.js?
Jonathan Goode
 
An introduction to Vue.js
Pagepro
 
React web development
Rully Ramanda
 
Angular
Lilia Sfaxi
 
Introduction to HTML+CSS+Javascript.pptx
KADAMBARIPUROHIT
 
laravel.pptx
asif290119
 
The Point of Vue - Intro to Vue.js
Holly Schinsky
 

What's hot (20)

PPTX
Angular Data Binding
Jennifer Estrada
 
PPTX
Introduction to web application development with Vue (for absolute beginners)...
Lucas Jellema
 
PPTX
Django - Python MVC Framework
Bala Kumar
 
PDF
Angular components
Sultan Ahmed
 
ODP
Angular 4 The new Http Client Module
arjun singh
 
PDF
Angular Directives
iFour Technolab Pvt. Ltd.
 
KEY
Introduction to Django
James Casey
 
PDF
React
중운 박
 
PDF
Testing Angular
Lilia Sfaxi
 
PPTX
Blazor Full-Stack
Ed Charbeneau
 
PPTX
Angular 2.0 forms
Eyal Vardi
 
PPT
Introduction To Django
Jay Graves
 
PDF
How to implement internationalization (i18n) in angular application(multiple ...
Katy Slemon
 
PPTX
Python (Jinja2) Templates for Network Automation
Rick Sherman
 
ODP
Angular 6 - The Complete Guide
Sam Dias
 
PPT
Vue.js Getting Started
Murat Doğan
 
PDF
VueJS Introduction
David Ličen
 
PPT
Ajax Ppt 1
JayaPrakash.m
 
PPTX
Spring framework in depth
Vinay Kumar
 
Angular Data Binding
Jennifer Estrada
 
Introduction to web application development with Vue (for absolute beginners)...
Lucas Jellema
 
Django - Python MVC Framework
Bala Kumar
 
Angular components
Sultan Ahmed
 
Angular 4 The new Http Client Module
arjun singh
 
Angular Directives
iFour Technolab Pvt. Ltd.
 
Introduction to Django
James Casey
 
React
중운 박
 
Testing Angular
Lilia Sfaxi
 
Blazor Full-Stack
Ed Charbeneau
 
Angular 2.0 forms
Eyal Vardi
 
Introduction To Django
Jay Graves
 
How to implement internationalization (i18n) in angular application(multiple ...
Katy Slemon
 
Python (Jinja2) Templates for Network Automation
Rick Sherman
 
Angular 6 - The Complete Guide
Sam Dias
 
Vue.js Getting Started
Murat Doğan
 
VueJS Introduction
David Ličen
 
Ajax Ppt 1
JayaPrakash.m
 
Spring framework in depth
Vinay Kumar
 
Ad

Similar to Functional Programming In JS (20)

PPTX
Getting Started with MongoDB and NodeJS
MongoDB
 
PPT
17javascript.ppt17javascript.ppt17javascript.ppt
hodit46
 
PPT
Java Script Programming on Web Application
SripathiRavi1
 
PPT
17javascript.ppt
bcanawakadalcollege
 
PPT
17javascript.ppt
PraveenRai90
 
PPTX
Java Script Overview
Return on Intelligence
 
PPTX
Things about Functional JavaScript
ChengHui Weng
 
PPTX
Building High Perf Web Apps - IE8 Firestarter
Mithun T. Dhar
 
KEY
Exciting JavaScript - Part I
Eugene Lazutkin
 
KEY
JavaScript Growing Up
David Padbury
 
PDF
Functional Web Development
FITC
 
KEY
Exciting JavaScript - Part II
Eugene Lazutkin
 
PPTX
Single Page Applications with AngularJS 2.0
Sumanth Chinthagunta
 
PPTX
Functional programming with Immutable .JS
Laura Steggles
 
PDF
"JS: the right way" by Mykyta Semenistyi
Binary Studio
 
PDF
Fundamental Node.js (Workshop bersama Front-end Developer GITS Indonesia, War...
GITS Indonesia
 
PPTX
Advanced JavaScript
Zsolt Mészárovics
 
PDF
HTML5 for the Silverlight Guy
David Padbury
 
PPTX
ECMAScript 6 and the Node Driver
MongoDB
 
PPTX
Intro to Javascript
Anjan Banda
 
Getting Started with MongoDB and NodeJS
MongoDB
 
17javascript.ppt17javascript.ppt17javascript.ppt
hodit46
 
Java Script Programming on Web Application
SripathiRavi1
 
17javascript.ppt
bcanawakadalcollege
 
17javascript.ppt
PraveenRai90
 
Java Script Overview
Return on Intelligence
 
Things about Functional JavaScript
ChengHui Weng
 
Building High Perf Web Apps - IE8 Firestarter
Mithun T. Dhar
 
Exciting JavaScript - Part I
Eugene Lazutkin
 
JavaScript Growing Up
David Padbury
 
Functional Web Development
FITC
 
Exciting JavaScript - Part II
Eugene Lazutkin
 
Single Page Applications with AngularJS 2.0
Sumanth Chinthagunta
 
Functional programming with Immutable .JS
Laura Steggles
 
"JS: the right way" by Mykyta Semenistyi
Binary Studio
 
Fundamental Node.js (Workshop bersama Front-end Developer GITS Indonesia, War...
GITS Indonesia
 
Advanced JavaScript
Zsolt Mészárovics
 
HTML5 for the Silverlight Guy
David Padbury
 
ECMAScript 6 and the Node Driver
MongoDB
 
Intro to Javascript
Anjan Banda
 
Ad

Recently uploaded (20)

PDF
Brief History of Internet - Early Days of Internet
sutharharshit158
 
PPTX
Coupa-Overview _Assumptions presentation
annapureddyn
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PPTX
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
PDF
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
codernjn73
 
PDF
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
Artjoker Software Development Company
 
PDF
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
PPT
Coupa-Kickoff-Meeting-Template presentai
annapureddyn
 
PDF
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
PDF
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
PDF
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
PDF
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
PDF
Doc9.....................................
SofiaCollazos
 
PDF
Software Development Company | KodekX
KodekX
 
PDF
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
PDF
A Day in the Life of Location Data - Turning Where into How.pdf
Precisely
 
PDF
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
PPTX
IoT Sensor Integration 2025 Powering Smart Tech and Industrial Automation.pptx
Rejig Digital
 
PDF
Event Presentation Google Cloud Next Extended 2025
minhtrietgect
 
PDF
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
Brief History of Internet - Early Days of Internet
sutharharshit158
 
Coupa-Overview _Assumptions presentation
annapureddyn
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
codernjn73
 
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
Artjoker Software Development Company
 
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
Coupa-Kickoff-Meeting-Template presentai
annapureddyn
 
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
Doc9.....................................
SofiaCollazos
 
Software Development Company | KodekX
KodekX
 
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
A Day in the Life of Location Data - Turning Where into How.pdf
Precisely
 
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
IoT Sensor Integration 2025 Powering Smart Tech and Industrial Automation.pptx
Rejig Digital
 
Event Presentation Google Cloud Next Extended 2025
minhtrietgect
 
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 

Functional Programming In JS