SlideShare a Scribd company logo
Introduction to Functional
Programming in JavaScript
Boris Burdiliak @ WebUP
Boris Burdiliak
@borisburdiliak
github.com/bburdiliak
linkedin.com/in/borisburdiliak/
Functional programming in javascript
Functional programming in javascript
Agenda
● are features of JS suitable for FP
● pure functions
● functional toolbox
● how to improve your code now
General Programming Principles
● DRY
● YAGNI
● KISS
● loose coupling
● high cohesion
● the principle of least surprise
● single responsibility
Headaches
● mutable state
● unrestricted side effects
● unprincipled design
Javascript - important features for FP
HAS
Anonymous functions and concise
lambda syntax
Closures
Higher-order functions
First class functions
DOES NOT HAVE
Purity
Immutability
Recursion
Concise lambda syntax
lambda expressions are abstractions which enable a function to be passed around like
data (value)
ES6 fat arrow
(x, y) => x * y
Closures
A closure is the combination of a function bundled together (enclosed) with
references to its surrounding state (the lexical environment).
Higher order functions
functions as
● function parameters
● return value of a function
First Class Functions
constants
assignment to variables
values of keys of an object
array items
function parameters
return value of a function
Functional programming in javascript
First Class Functions - use their power
layers of indirection with no added value
const fullname = user => `${user.firstName} ${user.lastName}`
const fullnames = users.map(user => fullname(user))
const fullnames = users.map(fullname)
First Class Functions
const getUsers = callback => ajaxCall(json => callback(json));
First Class Functions
// ignorant
const getUsers = callback => ajaxCall(json => callback(json));
const getUsers = callback => ajaxCall(callback);
First Class Functions
// ignorant
const getUsers = callback => ajaxCall(json => callback(json));
const getUsers = callback => ajaxCall(callback);
// enlightened
const getUsers = ajaxCall;
First Class Functions - help with
layers of indirection with no added value
> amount of redundant code to maintain and search through
name and reference arguments
First Class Functions - Naming
// specific to our current blog
const validArticles = articles =>
articles.filter(article => article !== null && article !== undefined)
// vastly more relevant for future projects
const compact = xs => xs.filter(x => x !== null && x !== undefined)
Pure Functions
A pure function is a function where the return value is only determined by its input
values, without observable side effects.
Pure Functions - Example
let G = 6.67408
const force = (m1, m2, r) => G * m1 * m2 / (r * r)
Pure Functions - Example
let G = 6.67408
const force = (m1, m2, r) => G * m1 * m2 / (r * r)
❌
Pure Functions - Example
const trace = (title, text) => console.log(title, text)
Pure Functions - Example
const trace = (title, text) => console.log(title, text)
❌
Pure Functions - Example
const max = (m1, m2) => m1 > m2 ? m1 : m2
Pure Functions - Example
const max = (m1, m2) => m1 > m2 ? m1 : m2
✅
Pure Functions - Example
const oneDayLater = () => {
const today = moment();
return moment(today).add(1, 'days');
}
Pure Functions - Example
const oneDayLater = () => {
const today = moment();
return moment(today).add(1, 'days');
}
❌
Pure Functions - Example
const cached = memoize((a) => a*2)
Pure Functions - Example
const cached = memoize((a) => a*2)
❌✅
Pure Functions - Example
const signUp = (attrs) => {
const user = saveUser(attrs);
welcomeUser(user);
};
Pure Functions - Example
const signUp = (attrs) => {
const user = saveUser(attrs);
welcomeUser(user);
};
❌
Pure Functions - Example
const signUp = (Db, Email, attrs) => {
const user = saveUser(Db, attrs);
welcomeUser(Email, user);
};
Pure Functions - Example
const signUp = (Db, Email, attrs) => {
const user = saveUser(Db, attrs);
welcomeUser(Email, user);
};
❌
Side Effects
changing the file system
inserting a record into a database
making an http call
mutations
printing to the screen / logging
obtaining user input
querying the DOM
accessing system state
Functional programming in javascript
Pure Functions - Example
const signUp = (Db, Email, attrs) => () => {
const user = saveUser(Db, attrs);
welcomeUser(Email, user);
};
❌✅
Benefits of Pure Functions
Cacheable
const memoize = (f) => {
const cache = {};
return (...args) => {
const argStr = JSON.stringify(args);
cache[argStr] = cache[argStr] || f(...args);
return cache[argStr];
};
};
const computation = memoize(costlyComputation)
Self-documenting
explicit function dependencies
just from its signature, we know what it will use
forced to "inject" dependencies - more flexible - parametrized
Portable
pure functions
serialize
send
run anywhere
imperative programming methods
tangled with state
dependencies
effects
You wanted a banana but what
you got was a gorilla holding
the banana... and the entire
jungle.
Joe Armstrong, Erlang creator
Testable
give input and assert output
Offer Referential Transparency
In functional programming, referential transparency is generally defined as the fact
that an expression, in a program, may be replaced by its value (or anything having the
same value) without changing the result of the program.
Easy to reason about
no need to search for context
everything depends just on the input arguments
Parallelism
no need to access shared memory
no race conditions caused by side effects
Our Functional Toolbox
filter, map, reduce
const findEven = (numbers) => {
const even = []
numbers.forEach(number => {
if (isEven(number)) {
even.push(number)
}
})
return even;
}
const isEven = (number) => number % 2 === 0
const findEven = numbers => numbers.filter(isEven)
Curry
Curry
A curried function is a function that takes multiple arguments one at a time.
Call this function with one argument and it returns a function that takes the
remaining arguments
const sum = (x, y) => x + y;
const sum = x => y => x + y;
const increment = sum(1)
const add10 = sum(10)
Curry base
const match = curry((what, s) => s.match(what));
const replace = curry((what, replacement, s) => s.replace(what, replacement));
const filter = curry((f, xs) => xs.filter(f));
const map = curry((f, xs) => xs.map(f));
match(/r/g, 'hello world'); // [ 'r' ]
const hasLetterR = match(/r/g); // x => x.match(/r/g)
hasLetterR('hello world'); // [ 'r' ]
hasLetterR('just j and s and t etc'); // null
filter(hasLetterR, ['rock and roll', 'smooth jazz']); // ['rock and roll']
const removeStringsWithoutRs = filter(hasLetterR); // xs => xs.filter(x => x.match(/r/g))
removeStringsWithoutRs(['rock and roll', 'smooth jazz', 'drum circle']); // ['rock and roll', 'drum
circle']
const noVowels = replace(/[aeiou]/ig); // (r,x) => x.replace(/[aeiou]/ig, r)
const censored = noVowels('*'); // x => x.replace(/[aeiou]/ig, '*')
censored('Chocolate Rain'); // 'Ch*c*l*t* R**n'
Curry - positioning of arguments
iteratee-first
data-last
preload data / dependencies
const findEven = numbers => filter(isEven, numbers)
// apply currying to get pointfree version
const findEven = filter(isEven)
Real-world examples of currying
export default connect(mapStateToProps)(EventsComponent)
const handleChange = (fieldName) => (event) => {
saveField(fieldName, event.target.value)
}
<input type="text" onChange={handleChange('email')} />
React and Redux
Event Handling
Compose
Functional composition
Functional composition
const compose = (f, g) => x => f(g(x));
const toUpperCase = x => x.toUpperCase();
const exclaim = x => `${x}!`;
const shout = compose(exclaim, toUpperCase);
shout('send in the clowns'); // "SEND IN THE CLOWNS!"
const shout = x => exclaim(toUpperCase(x));
Associativity of Functional composition
compose(f, compose(g, h)) === compose(compose(f, g), h);
compose(toUpper, compose(head, reverse))
===
compose(compose(toUpper, head), reverse);
const lastUpper = compose(toUpper, head, reverse);
with variadic compose
Composition in real world
const withEditMode = compose(
withState('editMode', 'setEditMode', false),
withHandlers({
toggleEditMode: ({ setEditMode }) => () => {
setEditMode((editMode) => !editMode);
},
}),
);
export default compose(
withEditMode,
withModal,
withStyles(styles),
)(EventManagement);
Hooks!
const EditButton = () => {
const [editMode, setEditMode] = useState(0);
return (
<div>
<p>Edit mode is {String(editMode)}</p>
<button onClick={() => setEditMode(R.not)}>
Click me
</button>
</div>
);
}
Start today!
coding pure functions
cooking good curry
composing functions
embracing pointfree programming
using hooks
using ramda
References
https://mostly-adequate.gitbooks.io/mostly-adequate-guide
https://medium.com/javascript-scene/why-learn-functional-programming-in-javascript-composing-software-ea13afc7a257
https://blog.benestudio.co/currying-in-javascript-es6-540d2ad09400
https://egghead.io/courses/professor-frisby-introduces-composable-functional-javascript
https://alvinalexander.com/scala/fp-book/benefits-of-pure-functions
https://medium.com/javascript-scene/master-the-javascript-interview-what-is-a-pure-function-d1c076bec976
https://en.wikipedia.org/wiki/Lambda_calculus
https://medium.freecodecamp.org/discover-functional-programming-in-javascript-with-this-thorough-introduction-a2ad9af2d645
Thanks!Thank you for having me!

More Related Content

PDF
05. haskell streaming io
PPTX
FunctionalJS - George Shevtsov
PDF
Ramda, a functional JavaScript library
PDF
Ramda lets write declarative js
PDF
Introduction to JQ
PDF
Scala + WattzOn, sitting in a tree....
PDF
jq: JSON - Like a Boss
PPTX
Functional programming with Immutable .JS
05. haskell streaming io
FunctionalJS - George Shevtsov
Ramda, a functional JavaScript library
Ramda lets write declarative js
Introduction to JQ
Scala + WattzOn, sitting in a tree....
jq: JSON - Like a Boss
Functional programming with Immutable .JS

What's hot (20)

PPTX
LinkedIn TBC JavaScript 100: Functions
PDF
GPars For Beginners
PDF
The... Wonderful? World of Lambdas
PDF
04. haskell handling
PDF
JavaScript Functions
PPTX
Java script
PPTX
Kotlin For Android - Constructors and Control Flow (part 2 of 7)
PDF
2014-11-01 01 Денис Нелюбин. О сортах кофе
PDF
Kotlin Advanced - Apalon Kotlin Sprint Part 3
PDF
Introduction kot iin
PDF
Kotlin Perfomance on Android / Александр Смирнов (Splyt)
PPTX
Kotlin For Android - Basics (part 1 of 7)
PPTX
Groovy Api Tutorial
PDF
Javascript & Ajax Basics
PDF
Composition in JavaScript
PPT
JavaScript Functions
PDF
Reactive Programming Patterns with RxSwift
PDF
"Немного о функциональном программирование в JavaScript" Алексей Коваленко
ODP
Groovy intro for OUDL
PDF
Develop your next app with kotlin @ AndroidMakersFr 2017
LinkedIn TBC JavaScript 100: Functions
GPars For Beginners
The... Wonderful? World of Lambdas
04. haskell handling
JavaScript Functions
Java script
Kotlin For Android - Constructors and Control Flow (part 2 of 7)
2014-11-01 01 Денис Нелюбин. О сортах кофе
Kotlin Advanced - Apalon Kotlin Sprint Part 3
Introduction kot iin
Kotlin Perfomance on Android / Александр Смирнов (Splyt)
Kotlin For Android - Basics (part 1 of 7)
Groovy Api Tutorial
Javascript & Ajax Basics
Composition in JavaScript
JavaScript Functions
Reactive Programming Patterns with RxSwift
"Немного о функциональном программирование в JavaScript" Алексей Коваленко
Groovy intro for OUDL
Develop your next app with kotlin @ AndroidMakersFr 2017
Ad

Similar to Functional programming in javascript (20)

PDF
Introduction to Functional Programming (w/ JS)
ODP
Functional programming
PDF
Functional programming 101
PDF
Functional programing in Javascript (lite intro)
PDF
Introduction to Functional Programming
PDF
Functional JS for everyone - 4Developers
PPTX
Thinking Functionally with JavaScript
PDF
Functional Programming in JavaScript
PDF
Functional Programming with JavaScript
PPTX
A Skeptics guide to functional style javascript
PDF
Christian Gill ''Functional programming for the people''
PDF
Functional JavaScript Fundamentals
PPTX
Things about Functional JavaScript
PDF
Functional Programming for OO Programmers (part 2)
PDF
Building Functional Islands
PDF
React JS & Functional Programming Principles
PDF
379008-rc217-functionalprogramming
PPT
25-functions.ppt
PPTX
Functional Programming in Javascript - IL Tech Talks week
PPTX
Functional Programming in JavaScript by Luis Atencio
Introduction to Functional Programming (w/ JS)
Functional programming
Functional programming 101
Functional programing in Javascript (lite intro)
Introduction to Functional Programming
Functional JS for everyone - 4Developers
Thinking Functionally with JavaScript
Functional Programming in JavaScript
Functional Programming with JavaScript
A Skeptics guide to functional style javascript
Christian Gill ''Functional programming for the people''
Functional JavaScript Fundamentals
Things about Functional JavaScript
Functional Programming for OO Programmers (part 2)
Building Functional Islands
React JS & Functional Programming Principles
379008-rc217-functionalprogramming
25-functions.ppt
Functional Programming in Javascript - IL Tech Talks week
Functional Programming in JavaScript by Luis Atencio
Ad

Recently uploaded (20)

PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
DOCX
573137875-Attendance-Management-System-original
PPT
Mechanical Engineering MATERIALS Selection
PPTX
Geodesy 1.pptx...............................................
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
UNIT 4 Total Quality Management .pptx
PPTX
additive manufacturing of ss316l using mig welding
PPTX
web development for engineering and engineering
PDF
composite construction of structures.pdf
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
Welding lecture in detail for understanding
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PPTX
Internet of Things (IOT) - A guide to understanding
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PDF
PPT on Performance Review to get promotions
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
573137875-Attendance-Management-System-original
Mechanical Engineering MATERIALS Selection
Geodesy 1.pptx...............................................
UNIT-1 - COAL BASED THERMAL POWER PLANTS
UNIT 4 Total Quality Management .pptx
additive manufacturing of ss316l using mig welding
web development for engineering and engineering
composite construction of structures.pdf
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Welding lecture in detail for understanding
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
Internet of Things (IOT) - A guide to understanding
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPT on Performance Review to get promotions
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx

Functional programming in javascript

Editor's Notes

  • #8: http://curtclifton.net/papers/MoseleyMarks06a.pdf
  • #9: https://hackernoon.com/javascript-and-functional-programming-pt-2-first-class-functions-4437a1aec217 https://medium.com/javascript-scene/why-learn-functional-programming-in-javascript-composing-software-ea13afc7a257 Purity: In JavaScript, purity must be achieved by convention. If you’re not building most of your application by composing pure functions, you’re not programming using the functional style. It’s unfortunately easy in JavaScript to get off track by accidentally creating and using impure functions. Immutability: In pure functional languages, immutability is often enforced. JavaScript lacks efficient, immutable trie-based data structures used by most functional languages, but there are libraries that help, including Immutable.js and Mori. I’m hoping that future versions of the ECMAScript spec will embrace immutable data structures. Recursion: JavaScript technically supports recursion, but most functional languages have a feature called tail call optimization. Tail call optimization is a feature which allows recursive functions to reuse stack frames for recursive calls. Without tail call optimization, a call stack can grow without bounds and cause a stack overflow. JavaScript technically got a limited form of tail call optimization in the ES6 specification. Unfortunately, only one of the major browser engines implemented it, and the optimization was partially implemented and then subsequently removed from Babel (the most popular standard JavaScript compiler, used to compile ES6 to ES5 for use in older browsers).
  • #11: https://medium.com/javascript-scene/master-the-javascript-interview-what-is-a-closure-b2f0d2152b36
  • #13: https://hackernoon.com/javascript-and-functional-programming-pt-2-first-class-functions-4437a1aec217
  • #57: https://medium.com/javascript-scene/curry-and-function-composition-2c208d774983