SlideShare a Scribd company logo
Functional Programming
for the People
Christian Gill
HousingAnywhere
A monad in X is just a monoid in the category
of endofunctors of X.
Saunders Mac Lane.
Categories for the Working Mathematician
Christian Gill ''Functional programming for the people''
What does it mean
to write functional code?
code terse, yet easy to reason about
don't reinvent the wheel
represent problems in terms of generic, composable
bits
benefit from laws
First class citizens
(functions are values)
const add = (a, b) => a + b
const sum = (x, y) => add(x, y)
sum(1, 2) // 3
const sum = add
sum(1, 2) // 3
;[1, 2, 3].reduce((acc, i) => sum(acc, i), 0)
;[1, 2, 3].reduce(sum, 0)
const calculations = [
(w) => w + w,
(x) => x + 1,
(y) => y / 2,
(z) => z * z * z,
]
const obj = {
f: (x) => x + 1,
g: (y) => y / 2,
h: (z) => z * z * z,
}
Higher-Order Functions
;[1, 2, 3].reduce(sum, 0)
const mult = (a) => (b) => a * b
const by2 = mult(2)
typeof by2 // 'function'
by2(2) // 4
mult(2)(2) // 4
const fs = require('fs')
fs.readFile('file.txt', (err, text) => {
if (err) {
console.error('Error:', err)
return
}
console.log('File content: ', text)
})
fetch(url)
.then((res) => res.json())
.then(({data}) => console.log(data))
Pure functions
A pure function is a function that, given the same
input, will always return the same output and does
not have any observable side effect.
Brian Lonsdorf
Mostly adequate guide to FP
Math.random() // 0.08140343648722337
Date.now() // 1560981656763
splice vs. slice
const xs = [1, 2, 3, 4, 5]
xs.splice(0, 3) // [1, 2, 3]
xs.splice(0, 3) // [4, 5]
xs.splice(0, 3) // []
xs // []
const xs = [1, 2, 3, 4, 5]
xs.slice(0, 3) // [1, 2, 3]
xs.slice(0, 3) // [1, 2, 3]
xs.slice(0, 3) // [1, 2, 3]
xs // [1, 2, 3, 4, 5]
Predictability
But what if I want to
(write a file|save to databse|fetch from
server) ?!
Christian Gill ''Functional programming for the people''
pure functions === easy tests
same input -> same output
no observable side effects
const saveUser = (user) => Db.save(user)
const saveUser = (Db) => (user) => Db.save(user)
const saveUser = (Db) => Db.save
// saveUser :: Db -> User -> Promise User
const saveUser = (Db) => Db.save
// production
const db = connect(config)
const saveUserToDB = saveUser(db)
// testing
const testDB = createTestDB({users: []})
const saveUserTest = saveUser(testsDB)
const createTestDB = (obj) => ({
save: (user) => {
obj.users.push(user)
return Promise.resolve(obj.users)
},
})
saveUserTest('Carla') //=> Promise(['Carla'])
saveUserTest('Manu') //=> Promise(['Carla', 'Manu'])
Also
Cache
Parallel
Portable
Self-documenting
React
f(data) -> view
Redux
f(state, action) -> state
const add = (a) => (b) => a + b
const inc = add(1) // =)
inc(1) // 2
add(1)(3) // 4 =(
const add = (a, b) => a + b
const inc = (b) => add(1, b) // =(
inc(1) // 2
add(1, 3) // 4 =)
Currying & Partial Application
// live coding - implement curry
const add = (a) => (b) => a + b
const inc = add(1)
inc(10)
add(10)(20)
Composition
(f ∘ g)(x) = f(g(x))
// live codign - implement compose
// (f ∘ g)(x) = f(g(x))
const compose = () => {}
const head = (xs) => xs[0]
const reverse = (xs) => [...xs].reverse()
const tail = (xs) => head(reverse(xs))
const arr = ['Odessa', 'JS', 'Rocks']
tail(arr)
live coding - implement compose3
onst compose = (f, g) => (x) => f(g(x))
onst join = (delim) => (xs) => xs.join(delim)
onst toUpper = (str) => str.toUpperCase()
onst exclaim = (str) => str + '!!!'
onst titlelize = (xs) => exclaim(toUpper(join(' ')(xs)
onst arr = ['Odessa', 'JS', 'Rocks']
tlelize(arr)
/ live coding - implement generic compose
onst compose = (f, g) => (x) => f(g(x))
onst compose3 = (f, g, h) => (x) => f(g(h(x)))
onst join = (delim) => (xs) => xs.join(delim)
onst toUpper = (str) => str.toUpperCase()
onst exclaim = (str) => str + '!!!'
onst h1 = (str) => '<h1>' + str + '</h1>'
onst titlelize = compose3(exclaim, toUpper, join(' ')
onst arr = ['Odessa', 'JS', 'Rocks']
itlelize(arr)
Composition is fractal
operation -> service -> use case -> app
{ coding time -- small CLI example app }
What does it mean
to write functional code?
Christian Gill ''Functional programming for the people''
Christian Gill ''Functional programming for the people''
Thλnks!

More Related Content

What's hot (19)

PDF
Day 4b iteration and functions for-loops.pptx
Adrien Melquiond
 
PDF
Rkf
faintcardy
 
TXT
Bmpread
sahu487
 
PDF
Haskell
Aycan iRiCAN
 
PDF
The detailed derivation of the derivatives in Table 2 of Marginalized Denoisi...
Tomonari Masada
 
PDF
Taking Inspiration From The Functional World
Piotr Solnica
 
DOC
Hw #2
Abdo Khalaf
 
PDF
A gentle introduction to functional programming through music and clojure
Paul Lam
 
PDF
Stefan Kanev: Clojure, ClojureScript and Why They're Awesome at I T.A.K.E. Un...
Mozaic Works
 
PPTX
adders/subtractors, multiplexers, intro to ISA
i i
 
DOCX
imager package in R and examples..
Dr. Volkan OBAN
 
PPTX
Python Tidbits
Mitchell Vitez
 
PPT
Compfuncdiff
dianenz
 
PDF
Useful javascript
Lei Kang
 
PPTX
Presentation1
teacherhebaa
 
TXT
Efnsjdnfsuies
htmrk
 
DOCX
Interpolation graph c++
rpiitcbme
 
PDF
C# Assignmet Help
Programming Homework Help
 
PPTX
Building HTML5 enabled web applications with Visual Studio 2011
Microsoft Developer Network (MSDN) - Belgium and Luxembourg
 
Day 4b iteration and functions for-loops.pptx
Adrien Melquiond
 
Bmpread
sahu487
 
Haskell
Aycan iRiCAN
 
The detailed derivation of the derivatives in Table 2 of Marginalized Denoisi...
Tomonari Masada
 
Taking Inspiration From The Functional World
Piotr Solnica
 
A gentle introduction to functional programming through music and clojure
Paul Lam
 
Stefan Kanev: Clojure, ClojureScript and Why They're Awesome at I T.A.K.E. Un...
Mozaic Works
 
adders/subtractors, multiplexers, intro to ISA
i i
 
imager package in R and examples..
Dr. Volkan OBAN
 
Python Tidbits
Mitchell Vitez
 
Compfuncdiff
dianenz
 
Useful javascript
Lei Kang
 
Presentation1
teacherhebaa
 
Efnsjdnfsuies
htmrk
 
Interpolation graph c++
rpiitcbme
 
C# Assignmet Help
Programming Homework Help
 
Building HTML5 enabled web applications with Visual Studio 2011
Microsoft Developer Network (MSDN) - Belgium and Luxembourg
 

Similar to Christian Gill ''Functional programming for the people'' (20)

KEY
Haskellで学ぶ関数型言語
ikdysfm
 
PDF
From Javascript To Haskell
ujihisa
 
PDF
Scala Functional Patterns
league
 
PDF
Fp in scala part 2
Hang Zhao
 
PPTX
Seminar PSU 10.10.2014 mme
Vyacheslav Arbuzov
 
PDF
ECMAScript 6 and beyond
Francis Johny
 
PPTX
Millionways
Brian Lonsdorf
 
PPT
Intro to Functional Programming Workshop (code4lib)
Will Kurt
 
PPTX
Oh Composable World!
Brian Lonsdorf
 
PDF
Артём Акуляков - F# for Data Analysis
SpbDotNet Community
 
PDF
Functional JS for everyone - 4Developers
Bartek Witczak
 
PPTX
Seminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov Vyacheslav
Vyacheslav Arbuzov
 
PDF
Gentle Introduction to Functional Programming
Saurabh Singh
 
PDF
Basic R Data Manipulation
Chu An
 
KEY
関数潮流(Function Tendency)
riue
 
PDF
Introduction to R programming
Alberto Labarga
 
PDF
React, Redux, ES2015 by Max Petruck
Lingvokot
 
PDF
Why Haskell Matters
romanandreg
 
PDF
React, Redux, ES2015 by Max Petruck
Maksym Petruk
 
PDF
Yoyak ScalaDays 2015
ihji
 
Haskellで学ぶ関数型言語
ikdysfm
 
From Javascript To Haskell
ujihisa
 
Scala Functional Patterns
league
 
Fp in scala part 2
Hang Zhao
 
Seminar PSU 10.10.2014 mme
Vyacheslav Arbuzov
 
ECMAScript 6 and beyond
Francis Johny
 
Millionways
Brian Lonsdorf
 
Intro to Functional Programming Workshop (code4lib)
Will Kurt
 
Oh Composable World!
Brian Lonsdorf
 
Артём Акуляков - F# for Data Analysis
SpbDotNet Community
 
Functional JS for everyone - 4Developers
Bartek Witczak
 
Seminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov Vyacheslav
Vyacheslav Arbuzov
 
Gentle Introduction to Functional Programming
Saurabh Singh
 
Basic R Data Manipulation
Chu An
 
関数潮流(Function Tendency)
riue
 
Introduction to R programming
Alberto Labarga
 
React, Redux, ES2015 by Max Petruck
Lingvokot
 
Why Haskell Matters
romanandreg
 
React, Redux, ES2015 by Max Petruck
Maksym Petruk
 
Yoyak ScalaDays 2015
ihji
 
Ad

More from OdessaJS Conf (20)

PPTX
'GraphQL Schema Design' by Borys Mohyla. OdessaJS'2021
OdessaJS Conf
 
PDF
'How i came up with my talk' by Yurii Artiukh. OdessaJS'2021
OdessaJS Conf
 
PDF
"Is there life in react without redux" by Babich Sergiy. OdessaJS'2021
OdessaJS Conf
 
PPTX
Олексій Павленко. CONTRACT PROTECTION ON THE FRONTEND SIDE: HOW TO ORGANIZE R...
OdessaJS Conf
 
PPTX
Андрій Троян. Розробка мікросервісів з NestJS. OdessaJS'2021
OdessaJS Conf
 
PPTX
Олексій Гончар "Використання Electron в розробці корпоративної відео-мессeндж...
OdessaJS Conf
 
PDF
Максим Климишин "Що такого особливого у пропозиції вартості шаблону Micro Fro...
OdessaJS Conf
 
PDF
Павло Галушко. GOOD CODE MYTHS. OdessaJS'2021
OdessaJS Conf
 
PPTX
"NODEJS & GRAPHQL COOKBOOK. LET’S TALK ABOUT MICRO-SERVICES" by Антон Чередні...
OdessaJS Conf
 
PPTX
'BUILDING ANGULAR APPS WITH NX' by Anastasia Necheporenko
OdessaJS Conf
 
PPTX
'IS THERE JAVASCRIPT ON SWAGGER PLUGINS?' by Dmytro Gusev
OdessaJS Conf
 
PPTX
'ETHEREUM SMART CONTRACTS ON JS' by Yaroslav Dvorovenko
OdessaJS Conf
 
PPTX
'GOLANG USAGE IN DEVELOPMENT OF NODE.JS APPLICATIONS (NODE.JS: IN GO WE TRUST...
OdessaJS Conf
 
PPTX
'MICROFRONTENDS WITH REACT' by Liliia Karpenko
OdessaJS Conf
 
PDF
'Web performance metrics' BY ROMAN SAVITSKYI at OdessaJS'2020
OdessaJS Conf
 
PDF
'STORY OF ANOTHER ANIMATION' by YURII ARTYUKH at OdessaJS'2020
OdessaJS Conf
 
PDF
'JavaScript was invented in Odessa' by DMITRIY GUSEV at OdessaJS'2020
OdessaJS Conf
 
PDF
'Why svelte' by BORYS MOHYLA at OdessaJS'2020
OdessaJS Conf
 
PDF
'Effective node.js development' by Viktor Turskyi at OdessaJS'2020
OdessaJS Conf
 
PDF
'Tensorflow.js in real life' by Pavlo Galushko at OdessaJS'2020
OdessaJS Conf
 
'GraphQL Schema Design' by Borys Mohyla. OdessaJS'2021
OdessaJS Conf
 
'How i came up with my talk' by Yurii Artiukh. OdessaJS'2021
OdessaJS Conf
 
"Is there life in react without redux" by Babich Sergiy. OdessaJS'2021
OdessaJS Conf
 
Олексій Павленко. CONTRACT PROTECTION ON THE FRONTEND SIDE: HOW TO ORGANIZE R...
OdessaJS Conf
 
Андрій Троян. Розробка мікросервісів з NestJS. OdessaJS'2021
OdessaJS Conf
 
Олексій Гончар "Використання Electron в розробці корпоративної відео-мессeндж...
OdessaJS Conf
 
Максим Климишин "Що такого особливого у пропозиції вартості шаблону Micro Fro...
OdessaJS Conf
 
Павло Галушко. GOOD CODE MYTHS. OdessaJS'2021
OdessaJS Conf
 
"NODEJS & GRAPHQL COOKBOOK. LET’S TALK ABOUT MICRO-SERVICES" by Антон Чередні...
OdessaJS Conf
 
'BUILDING ANGULAR APPS WITH NX' by Anastasia Necheporenko
OdessaJS Conf
 
'IS THERE JAVASCRIPT ON SWAGGER PLUGINS?' by Dmytro Gusev
OdessaJS Conf
 
'ETHEREUM SMART CONTRACTS ON JS' by Yaroslav Dvorovenko
OdessaJS Conf
 
'GOLANG USAGE IN DEVELOPMENT OF NODE.JS APPLICATIONS (NODE.JS: IN GO WE TRUST...
OdessaJS Conf
 
'MICROFRONTENDS WITH REACT' by Liliia Karpenko
OdessaJS Conf
 
'Web performance metrics' BY ROMAN SAVITSKYI at OdessaJS'2020
OdessaJS Conf
 
'STORY OF ANOTHER ANIMATION' by YURII ARTYUKH at OdessaJS'2020
OdessaJS Conf
 
'JavaScript was invented in Odessa' by DMITRIY GUSEV at OdessaJS'2020
OdessaJS Conf
 
'Why svelte' by BORYS MOHYLA at OdessaJS'2020
OdessaJS Conf
 
'Effective node.js development' by Viktor Turskyi at OdessaJS'2020
OdessaJS Conf
 
'Tensorflow.js in real life' by Pavlo Galushko at OdessaJS'2020
OdessaJS Conf
 
Ad

Recently uploaded (20)

PDF
From Code to Challenge: Crafting Skill-Based Games That Engage and Reward
aiyshauae
 
PDF
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
PDF
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
PDF
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PDF
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
PDF
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
What Makes Contify’s News API Stand Out: Key Features at a Glance
Contify
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
PDF
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
PDF
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PPTX
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PPTX
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
From Code to Challenge: Crafting Skill-Based Games That Engage and Reward
aiyshauae
 
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
What Makes Contify’s News API Stand Out: Key Features at a Glance
Contify
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 

Christian Gill ''Functional programming for the people''