SlideShare une entreprise Scribd logo
1  sur  38
FUNCTIONAL
PROGRAMMING
WITH IMMUTABLE.JS
DAVID WILDE
THE 45TH
PRESIDENT
BUT FOR
HOW
LONG?
FUNCTIONAL PROGRAMMING WITH IMMUTABLE.JS
IMMUTABILITY VS MUTABILITY
▸Mutable means ‘capable of changing’
▸Immutable means ‘cannot change’
▸Strings are immutable in most programming languages
▸Objects are mutable in JavaScript
FUNCTIONAL PROGRAMMING WITH IMMUTABLE.JS
IMMUTABLE OBJECTS IN THE
REAL WORLD
▸ Newspapers
▸ Accountancy Book-keeping
▸ Constitutions/Contracts
▸ Audits
▸ Facts presenting the state of
something at a given time
FUNCTIONAL PROGRAMMING WITH IMMUTABLE.JS
IMMUTABILITY IN CODE
▸Javascript numbers are immutable
let a = 1
let b = 2
let c = a + b
▸Strings are immutable
let a = “foo”
let b = “bar”
let c = a + b
FUNCTIONAL PROGRAMMING WITH IMMUTABLE.JS
IMMUTABILITY IN CODE
▸Javascript numbers are immutable
let a = 1
let b = 2
let c = a + b // a: 1, b: 2, c: 3
▸Strings are immutable
let a = “foo”
let b = “bar”
let c = a + b
FUNCTIONAL PROGRAMMING WITH IMMUTABLE.JS
IMMUTABILITY IN CODE
▸Javascript numbers are immutable
let a = 1
let b = 2
let c = a + b // a: 1, b: 2, c: 3
▸Strings are immutable
let a = “foo”
let b = “bar”
let c = a + b // a: “foo”, b: “bar”, c: “foobar”
FUNCTIONAL PROGRAMMING WITH IMMUTABLE.JS
IMMUTABILITY IN CODE
▸Some array methods are immutable
let a = [ 1, 2 ]
let b = [ 3, 4 ]
let c = a.concat(b)
▸Others are not
let a = [ 1, 2, 5 ]
let b = a.push(8)
FUNCTIONAL PROGRAMMING WITH IMMUTABLE.JS
IMMUTABILITY IN CODE
▸Some array methods are immutable
let a = [ 1, 2 ]
let b = [ 3, 4 ]
let c = a.concat(b) // a: [ 1, 2 ]
// b: [ 3, 4 ]
// c: [ 1, 2, 3, 4 ]
▸Others are not
let a = [ 1, 2, 5 ]
let b = a.push(8)
FUNCTIONAL PROGRAMMING WITH IMMUTABLE.JS
IMMUTABILITY IN CODE
▸Some array methods are immutable
let a = [ 1, 2 ]
let b = [ 3, 4 ]
let c = a.concat(b) // a: [ 1, 2 ]
// b: [ 3, 4 ]
// c: [ 1, 2, 3, 4 ]
▸Others are not
let a = [ 1, 2, 5 ]
let b = a.push(8) // a: [ 1, 2, 5, 8 ]
FUNCTIONAL PROGRAMMING WITH IMMUTABLE.JS
IMMUTABILITY IN CODE
▸Some array methods are immutable
let a = [ 1, 2 ]
let b = [ 3, 4 ]
let c = a.concat(b) // a: [ 1, 2 ]
// b: [ 3, 4 ]
// c: [ 1, 2, 3, 4 ]
▸Others are not
let a = [ 1, 2, 5 ]
let b = a.push(8) // a: [ 1, 2, 5, 8 ]
// b: 4
FUNCTIONAL PROGRAMMING WITH IMMUTABLE.JS
OBJECT-ORIENTATED PROGRAMMING
TENDS TO MUTABILITYclass Kettle {
int temperature = 0;
int getTemperature() {
return this.temperature;
}
void boil() {
this.temperature = 100;
}
}
class Order {
int id;
drink[] drinks;
Order(id) {
this.id = id;
}
addDrink(drink) {
this.drinks.push(drink);
}
process() {
}
}
class Coffee extends Beverage {
...
FUNCTIONAL PROGRAMMING WITH IMMUTABLE.JS
MAKING THIS OPERATION IMMUTABLE
var list1 = Immutable.List.of(1, 2)
var list2 = list1.push(3, 4, 5)
// list1: [ 1, 2 ]
// list2: [ 1, 2, 3, 4, 5 ]
FUNCTIONAL
PROGRAMMING
FUNCTIONAL PROGRAMMING WITH IMMUTABLE.JS
FUNCTIONS VS CLASSES
▸Functions do specific things
▸Classes are specific things
f(x)
FUNCTIONAL PROGRAMMING WITH IMMUTABLE.JS
ARRAY MAP
a = [1, 2, 5]
function double(value) {
return value * 2;
}
function addOne(value) {
return + 1;
}
b = a.map(double).map(addOne)
// b: [3, 5, 11]
FUNCTIONAL PROGRAMMING WITH IMMUTABLE.JS
ARRAY MAP
a = [1, 2, 5]
const double = (value) => value * 2;
const addOne = (value) => value + 1;
b = a.map(double).map(addOne)
// b: [3, 5, 11]
FUNCTIONAL PROGRAMMING WITH IMMUTABLE.JS
PURE FUNCTIONS
var me = { name: “David” };
const hello = () => (
console.log(“Hello, “ + me.name)
);
FUNCTIONAL PROGRAMMING WITH IMMUTABLE.JS
PURE FUNCTIONS
const hello = (person) => (
“Hello, “ + person.name
);
console.log(hello({name: “David”}));
FUNCTIONAL PROGRAMMING WITH IMMUTABLE.JS
PURE FUNCTIONS
const myObject = {a: 1, b: 2}
const myFunc = (obj) => {
obj.a = obj.a * 2
return obj
}
const newObj = myFunc(myObject)
console.log(myObject) // {a: 2, b: 2}
console.log(newObj) // {a: 2, b: 2)
FUNCTIONAL PROGRAMMING WITH IMMUTABLE.JS
PURE FUNCTIONS
const myImmutable = Immutable.fromJS({a: 1, b: 2})
const myFunc = (obj) => {
return myImmutable.set(‘a’, myImmutable.get(‘a’) * 2)
}
const newObj = myFunc(myObject)
console.log(myObject.toJS()) // {a: 1, b: 2}
console.log(newObj.toJS()) // {a: 2, b: 2)
PERFORMANCE
ENHANCEMENTS
USING IMMUTABLE
DATA STRUCTURES
FUNCTIONAL PROGRAMMING WITH IMMUTABLE.JS
DRAW DOM
BASED ON THE
STATE
STATE
OBJECT
SOME
INPUT
FUNCTIONAL PROGRAMMING WITH IMMUTABLE.JS
DRAW DOM
BASED ON
THE STATE
STATE
OBJECT
SOME
INPUT
CLONED
OBJECT
COMPARE
STATE VS
CLONE
Same Differ
FUNCTIONAL PROGRAMMING WITH IMMUTABLE.JS
COMPARING TWO OBJECTS IN JAVASCRIPT
FUNCTIONAL PROGRAMMING WITH IMMUTABLE.JS
COMPARING TWO OBJECTS IN JAVASCRIPT
function compare2Objects (x, y) {
var p;
// remember that NaN === NaN returns false
// and isNaN(undefined) returns true
if (isNaN(x) && isNaN(y) && typeof x === 'number' && typeof y === 'number') {
return true;
}
// Compare primitives and functions.
// Check if both arguments link to the same object.
// Especially useful on the step where we compare prototypes
if (x === y) {
return true;
}
// Works in case when functions are created in constructor.
// Comparing dates is a common scenario. Another built-ins?
// We can even handle functions passed across iframes
if ((typeof x === 'function' && typeof y === 'function') ||
(x instanceof Date && y instanceof Date) ||
(x instanceof RegExp && y instanceof RegExp) ||
(x instanceof String && y instanceof String) ||
(x instanceof Number && y instanceof Number)) {
return x.toString() === y.toString();
}
// At last checking prototypes as good as we can
if (!(x instanceof Object && y instanceof Object)) {
return false;
}
if (x.isPrototypeOf(y) || y.isPrototypeOf(x)) {
return false;
}
if (x.constructor !== y.constructor) {
return false;
}
if (x.prototype !== y.prototype) {
return false;
}
// Check for infinitive linking loops
if (leftChain.indexOf(x) > -1 || rightChain.indexOf(y) > -1) {
return false;
}
// Quick checking of one object being a subset of another.
// todo: cache the structure of arguments[0] for performance
for (p in y) {
if (y.hasOwnProperty(p) !== x.hasOwnProperty(p)) {
return false;
}
else if (typeof y[p] !== typeof x[p]) {
return false;
}
}
for (p in x) {
if (y.hasOwnProperty(p) !== x.hasOwnProperty(p)) {
return false;
}
else if (typeof y[p] !== typeof x[p]) {
return false;
}
switch (typeof (x[p])) {
case 'object':
case 'function':
leftChain.push(x);
rightChain.push(y);
if (!compare2Objects (x[p], y[p])) {
return false;
}
leftChain.pop();
rightChain.pop();
break;
default:
if (x[p] !== y[p]) {
return false;
}
break;
}
}
FUNCTIONAL PROGRAMMING WITH IMMUTABLE.JS
COMPARING TWO OBJECTS IN JAVASCRIPT
FUNCTIONAL PROGRAMMING WITH IMMUTABLE.JS
PARTICULAR PERFORMANCE
IMPROVEMENTS ARE AVAILABLE
DRAW DOM
BASED ON
THE STATE
STATE
OBJECT
SOME
INPUT
STORE
OLD REF
COMPARE
STATE REF VS
OLD REF
Same
Differ
STRUCTURAL
SHARING
FUNCTIONAL PROGRAMMING WITH IMMUTABLE.JS
PERSISTENT DATA STRUCTURES
1
List.of([ 1, 2, 3, 5, 8 ]).push(13)
2 3 50x0012 8
FUNCTIONAL PROGRAMMING WITH IMMUTABLE.JS
PERSISTENT DATA STRUCTURES
1 2 3 50x0012
1 2 3 50x0042 8
8
13
List.of([ 1, 2, 3, 5, 8 ]).push(13)
TRIE
FUNCTIONAL PROGRAMMING WITH IMMUTABLE.JS
STRUCTURAL SHARING
2
3 5
0x0012
1
8
List.of([ 1, 2, 3, 5, 8 ]).push(13)
FUNCTIONAL PROGRAMMING WITH IMMUTABLE.JS
STRUCTURAL SHARING
2
3 5
0x0012
1
8
1
8
13
0x0042
List.of([ 1, 2, 3, 5, 8 ]).push(13)
CONCLUSION
WORKSHOP
https://github.com/davidwilde/poop-sweeper
FUNCTIONAL PROGRAMMING WITH IMMUTABLE.JS
IMMUTABLE.JS
var map = Immutable.Map({ a: 1, b: 2, c: 3 })
map
.set('b', 50)
.get('b') // 50
var list = Immutable.List.of(1, 2)
list
.push(3, 4, 5)
.unshift(0)
.concat(list2, list3)
.get(0)
.size
var nested = Immutable.fromJS({ user: { profile: { name: 'John' } } })
nested
.mergeDeep({ user: { profile: { age: 90 } } })
.setIn([ 'user', 'profile', 'name' ], 'Jack')
.updateIn([ 'user', 'profile', 'name' ], (s) => s.toUpperCase())
.getIn(['user', 'profile', 'name']) // 'JACK'

Contenu connexe

Tendances

On Functional Programming - A Clojurian Perspective
On Functional Programming - A Clojurian PerspectiveOn Functional Programming - A Clojurian Perspective
On Functional Programming - A Clojurian Perspective
looselytyped
 
completion_proc and history
completion_proc and historycompletion_proc and history
completion_proc and history
Nobuhiro IMAI
 

Tendances (19)

Reactive Programming with RxSwift
Reactive Programming with RxSwiftReactive Programming with RxSwift
Reactive Programming with RxSwift
 
Swift & ReactiveX – Asynchronous Event-Based Funsies with RxSwift
Swift & ReactiveX – Asynchronous Event-Based Funsies with RxSwiftSwift & ReactiveX – Asynchronous Event-Based Funsies with RxSwift
Swift & ReactiveX – Asynchronous Event-Based Funsies with RxSwift
 
Reactive programming with RxSwift
Reactive programming with RxSwiftReactive programming with RxSwift
Reactive programming with RxSwift
 
Kotlin, a modern language for modern times
Kotlin, a modern language for modern timesKotlin, a modern language for modern times
Kotlin, a modern language for modern times
 
Kotlin For Android - Basics (part 1 of 7)
Kotlin For Android - Basics (part 1 of 7)Kotlin For Android - Basics (part 1 of 7)
Kotlin For Android - Basics (part 1 of 7)
 
Luis Atencio on RxJS
Luis Atencio on RxJSLuis Atencio on RxJS
Luis Atencio on RxJS
 
Groovify your java code by hervé roussel
Groovify your java code by hervé rousselGroovify your java code by hervé roussel
Groovify your java code by hervé roussel
 
RxJS 5 in Depth
RxJS 5 in DepthRxJS 5 in Depth
RxJS 5 in Depth
 
EcmaScript 6
EcmaScript 6 EcmaScript 6
EcmaScript 6
 
ClojureScript - A functional Lisp for the browser
ClojureScript - A functional Lisp for the browserClojureScript - A functional Lisp for the browser
ClojureScript - A functional Lisp for the browser
 
CoffeeScript
CoffeeScriptCoffeeScript
CoffeeScript
 
Groovy
GroovyGroovy
Groovy
 
Type Driven Development with TypeScript
Type Driven Development with TypeScriptType Driven Development with TypeScript
Type Driven Development with TypeScript
 
How to practice functional programming in react
How to practice functional programming in reactHow to practice functional programming in react
How to practice functional programming in react
 
On Functional Programming - A Clojurian Perspective
On Functional Programming - A Clojurian PerspectiveOn Functional Programming - A Clojurian Perspective
On Functional Programming - A Clojurian Perspective
 
ECMAScript 6
ECMAScript 6ECMAScript 6
ECMAScript 6
 
Testing Without Writing Tests
Testing Without Writing TestsTesting Without Writing Tests
Testing Without Writing Tests
 
Python GC
Python GCPython GC
Python GC
 
completion_proc and history
completion_proc and historycompletion_proc and history
completion_proc and history
 

Similaire à Functional programming with Immutable .JS

JavaScript Growing Up
JavaScript Growing UpJavaScript Growing Up
JavaScript Growing Up
David Padbury
 
JavaScript (without DOM)
JavaScript (without DOM)JavaScript (without DOM)
JavaScript (without DOM)
Piyush Katariya
 
Your Library Sucks, and why you should use it.
Your Library Sucks, and why you should use it.Your Library Sucks, and why you should use it.
Your Library Sucks, and why you should use it.
Peter Higgins
 

Similaire à Functional programming with Immutable .JS (20)

Intro to Javascript
Intro to JavascriptIntro to Javascript
Intro to Javascript
 
Advanced JavaScript
Advanced JavaScript Advanced JavaScript
Advanced JavaScript
 
JavaScript Growing Up
JavaScript Growing UpJavaScript Growing Up
JavaScript Growing Up
 
1. George Shevtsov - Functional JavaScript
1. George Shevtsov - Functional JavaScript1. George Shevtsov - Functional JavaScript
1. George Shevtsov - Functional JavaScript
 
ES6 PPT FOR 2016
ES6 PPT FOR 2016ES6 PPT FOR 2016
ES6 PPT FOR 2016
 
ES6 - Next Generation Javascript
ES6 - Next Generation JavascriptES6 - Next Generation Javascript
ES6 - Next Generation Javascript
 
From Javascript To Haskell
From Javascript To HaskellFrom Javascript To Haskell
From Javascript To Haskell
 
Scala in Practice
Scala in PracticeScala in Practice
Scala in Practice
 
JavaScript (without DOM)
JavaScript (without DOM)JavaScript (without DOM)
JavaScript (without DOM)
 
Clojure And Swing
Clojure And SwingClojure And Swing
Clojure And Swing
 
Reactive Programming - ReactFoo 2020 - Aziz Khambati
Reactive Programming - ReactFoo 2020 - Aziz KhambatiReactive Programming - ReactFoo 2020 - Aziz Khambati
Reactive Programming - ReactFoo 2020 - Aziz Khambati
 
HTML5 for the Silverlight Guy
HTML5 for the Silverlight GuyHTML5 for the Silverlight Guy
HTML5 for the Silverlight Guy
 
Kotlin For Android - Functions (part 3 of 7)
Kotlin For Android - Functions (part 3 of 7)Kotlin For Android - Functions (part 3 of 7)
Kotlin For Android - Functions (part 3 of 7)
 
"Javascript" por Tiago Rodrigues
"Javascript" por Tiago Rodrigues"Javascript" por Tiago Rodrigues
"Javascript" por Tiago Rodrigues
 
Your Library Sucks, and why you should use it.
Your Library Sucks, and why you should use it.Your Library Sucks, and why you should use it.
Your Library Sucks, and why you should use it.
 
2013-06-15 - Software Craftsmanship mit JavaScript
2013-06-15 - Software Craftsmanship mit JavaScript2013-06-15 - Software Craftsmanship mit JavaScript
2013-06-15 - Software Craftsmanship mit JavaScript
 
2013-06-24 - Software Craftsmanship with JavaScript
2013-06-24 - Software Craftsmanship with JavaScript2013-06-24 - Software Craftsmanship with JavaScript
2013-06-24 - Software Craftsmanship with JavaScript
 
A Lifecycle Of Code Under Test by Robert Fornal
A Lifecycle Of Code Under Test by Robert FornalA Lifecycle Of Code Under Test by Robert Fornal
A Lifecycle Of Code Under Test by Robert Fornal
 
Functional Reactive Programming
Functional Reactive ProgrammingFunctional Reactive Programming
Functional Reactive Programming
 
Functional Programming In JS
Functional Programming In JSFunctional Programming In JS
Functional Programming In JS
 

Plus de Laura Steggles

Plus de Laura Steggles (9)

HR Insights - Tax Reforms & Spring updates 2018
HR Insights - Tax Reforms & Spring updates 2018HR Insights - Tax Reforms & Spring updates 2018
HR Insights - Tax Reforms & Spring updates 2018
 
Tech Talk - Blockchain presentation
Tech Talk - Blockchain presentationTech Talk - Blockchain presentation
Tech Talk - Blockchain presentation
 
Elasticsearch workshop presentation
Elasticsearch workshop presentationElasticsearch workshop presentation
Elasticsearch workshop presentation
 
HR Insights - Mental Health Awareness in the Workplace
HR Insights - Mental Health Awareness in the WorkplaceHR Insights - Mental Health Awareness in the Workplace
HR Insights - Mental Health Awareness in the Workplace
 
Anna Denton Jones HR Insights September 2017
Anna Denton Jones HR Insights September 2017Anna Denton Jones HR Insights September 2017
Anna Denton Jones HR Insights September 2017
 
How to find and build your audience using social media
How to find and build your audience using social mediaHow to find and build your audience using social media
How to find and build your audience using social media
 
Anna Denton Jones HR Insights June 2017
Anna Denton Jones HR Insights June 2017Anna Denton Jones HR Insights June 2017
Anna Denton Jones HR Insights June 2017
 
Running local, going global yolk
Running local, going global   yolkRunning local, going global   yolk
Running local, going global yolk
 
Social Media and the common challenges employers have to deal with
Social Media and the common challenges employers have to deal withSocial Media and the common challenges employers have to deal with
Social Media and the common challenges employers have to deal with
 

Dernier

CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Dernier (20)

What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 

Functional programming with Immutable .JS

  • 3.
  • 4. FUNCTIONAL PROGRAMMING WITH IMMUTABLE.JS IMMUTABILITY VS MUTABILITY ▸Mutable means ‘capable of changing’ ▸Immutable means ‘cannot change’ ▸Strings are immutable in most programming languages ▸Objects are mutable in JavaScript
  • 5. FUNCTIONAL PROGRAMMING WITH IMMUTABLE.JS IMMUTABLE OBJECTS IN THE REAL WORLD ▸ Newspapers ▸ Accountancy Book-keeping ▸ Constitutions/Contracts ▸ Audits ▸ Facts presenting the state of something at a given time
  • 6. FUNCTIONAL PROGRAMMING WITH IMMUTABLE.JS IMMUTABILITY IN CODE ▸Javascript numbers are immutable let a = 1 let b = 2 let c = a + b ▸Strings are immutable let a = “foo” let b = “bar” let c = a + b
  • 7. FUNCTIONAL PROGRAMMING WITH IMMUTABLE.JS IMMUTABILITY IN CODE ▸Javascript numbers are immutable let a = 1 let b = 2 let c = a + b // a: 1, b: 2, c: 3 ▸Strings are immutable let a = “foo” let b = “bar” let c = a + b
  • 8. FUNCTIONAL PROGRAMMING WITH IMMUTABLE.JS IMMUTABILITY IN CODE ▸Javascript numbers are immutable let a = 1 let b = 2 let c = a + b // a: 1, b: 2, c: 3 ▸Strings are immutable let a = “foo” let b = “bar” let c = a + b // a: “foo”, b: “bar”, c: “foobar”
  • 9. FUNCTIONAL PROGRAMMING WITH IMMUTABLE.JS IMMUTABILITY IN CODE ▸Some array methods are immutable let a = [ 1, 2 ] let b = [ 3, 4 ] let c = a.concat(b) ▸Others are not let a = [ 1, 2, 5 ] let b = a.push(8)
  • 10. FUNCTIONAL PROGRAMMING WITH IMMUTABLE.JS IMMUTABILITY IN CODE ▸Some array methods are immutable let a = [ 1, 2 ] let b = [ 3, 4 ] let c = a.concat(b) // a: [ 1, 2 ] // b: [ 3, 4 ] // c: [ 1, 2, 3, 4 ] ▸Others are not let a = [ 1, 2, 5 ] let b = a.push(8)
  • 11. FUNCTIONAL PROGRAMMING WITH IMMUTABLE.JS IMMUTABILITY IN CODE ▸Some array methods are immutable let a = [ 1, 2 ] let b = [ 3, 4 ] let c = a.concat(b) // a: [ 1, 2 ] // b: [ 3, 4 ] // c: [ 1, 2, 3, 4 ] ▸Others are not let a = [ 1, 2, 5 ] let b = a.push(8) // a: [ 1, 2, 5, 8 ]
  • 12. FUNCTIONAL PROGRAMMING WITH IMMUTABLE.JS IMMUTABILITY IN CODE ▸Some array methods are immutable let a = [ 1, 2 ] let b = [ 3, 4 ] let c = a.concat(b) // a: [ 1, 2 ] // b: [ 3, 4 ] // c: [ 1, 2, 3, 4 ] ▸Others are not let a = [ 1, 2, 5 ] let b = a.push(8) // a: [ 1, 2, 5, 8 ] // b: 4
  • 13. FUNCTIONAL PROGRAMMING WITH IMMUTABLE.JS OBJECT-ORIENTATED PROGRAMMING TENDS TO MUTABILITYclass Kettle { int temperature = 0; int getTemperature() { return this.temperature; } void boil() { this.temperature = 100; } } class Order { int id; drink[] drinks; Order(id) { this.id = id; } addDrink(drink) { this.drinks.push(drink); } process() { } } class Coffee extends Beverage { ...
  • 14. FUNCTIONAL PROGRAMMING WITH IMMUTABLE.JS MAKING THIS OPERATION IMMUTABLE var list1 = Immutable.List.of(1, 2) var list2 = list1.push(3, 4, 5) // list1: [ 1, 2 ] // list2: [ 1, 2, 3, 4, 5 ]
  • 16. FUNCTIONAL PROGRAMMING WITH IMMUTABLE.JS FUNCTIONS VS CLASSES ▸Functions do specific things ▸Classes are specific things f(x)
  • 17. FUNCTIONAL PROGRAMMING WITH IMMUTABLE.JS ARRAY MAP a = [1, 2, 5] function double(value) { return value * 2; } function addOne(value) { return + 1; } b = a.map(double).map(addOne) // b: [3, 5, 11]
  • 18. FUNCTIONAL PROGRAMMING WITH IMMUTABLE.JS ARRAY MAP a = [1, 2, 5] const double = (value) => value * 2; const addOne = (value) => value + 1; b = a.map(double).map(addOne) // b: [3, 5, 11]
  • 19. FUNCTIONAL PROGRAMMING WITH IMMUTABLE.JS PURE FUNCTIONS var me = { name: “David” }; const hello = () => ( console.log(“Hello, “ + me.name) );
  • 20. FUNCTIONAL PROGRAMMING WITH IMMUTABLE.JS PURE FUNCTIONS const hello = (person) => ( “Hello, “ + person.name ); console.log(hello({name: “David”}));
  • 21. FUNCTIONAL PROGRAMMING WITH IMMUTABLE.JS PURE FUNCTIONS const myObject = {a: 1, b: 2} const myFunc = (obj) => { obj.a = obj.a * 2 return obj } const newObj = myFunc(myObject) console.log(myObject) // {a: 2, b: 2} console.log(newObj) // {a: 2, b: 2)
  • 22. FUNCTIONAL PROGRAMMING WITH IMMUTABLE.JS PURE FUNCTIONS const myImmutable = Immutable.fromJS({a: 1, b: 2}) const myFunc = (obj) => { return myImmutable.set(‘a’, myImmutable.get(‘a’) * 2) } const newObj = myFunc(myObject) console.log(myObject.toJS()) // {a: 1, b: 2} console.log(newObj.toJS()) // {a: 2, b: 2)
  • 24. FUNCTIONAL PROGRAMMING WITH IMMUTABLE.JS DRAW DOM BASED ON THE STATE STATE OBJECT SOME INPUT
  • 25. FUNCTIONAL PROGRAMMING WITH IMMUTABLE.JS DRAW DOM BASED ON THE STATE STATE OBJECT SOME INPUT CLONED OBJECT COMPARE STATE VS CLONE Same Differ
  • 26. FUNCTIONAL PROGRAMMING WITH IMMUTABLE.JS COMPARING TWO OBJECTS IN JAVASCRIPT
  • 27. FUNCTIONAL PROGRAMMING WITH IMMUTABLE.JS COMPARING TWO OBJECTS IN JAVASCRIPT function compare2Objects (x, y) { var p; // remember that NaN === NaN returns false // and isNaN(undefined) returns true if (isNaN(x) && isNaN(y) && typeof x === 'number' && typeof y === 'number') { return true; } // Compare primitives and functions. // Check if both arguments link to the same object. // Especially useful on the step where we compare prototypes if (x === y) { return true; } // Works in case when functions are created in constructor. // Comparing dates is a common scenario. Another built-ins? // We can even handle functions passed across iframes if ((typeof x === 'function' && typeof y === 'function') || (x instanceof Date && y instanceof Date) || (x instanceof RegExp && y instanceof RegExp) || (x instanceof String && y instanceof String) || (x instanceof Number && y instanceof Number)) { return x.toString() === y.toString(); } // At last checking prototypes as good as we can if (!(x instanceof Object && y instanceof Object)) { return false; } if (x.isPrototypeOf(y) || y.isPrototypeOf(x)) { return false; } if (x.constructor !== y.constructor) { return false; } if (x.prototype !== y.prototype) { return false; } // Check for infinitive linking loops if (leftChain.indexOf(x) > -1 || rightChain.indexOf(y) > -1) { return false; } // Quick checking of one object being a subset of another. // todo: cache the structure of arguments[0] for performance for (p in y) { if (y.hasOwnProperty(p) !== x.hasOwnProperty(p)) { return false; } else if (typeof y[p] !== typeof x[p]) { return false; } } for (p in x) { if (y.hasOwnProperty(p) !== x.hasOwnProperty(p)) { return false; } else if (typeof y[p] !== typeof x[p]) { return false; } switch (typeof (x[p])) { case 'object': case 'function': leftChain.push(x); rightChain.push(y); if (!compare2Objects (x[p], y[p])) { return false; } leftChain.pop(); rightChain.pop(); break; default: if (x[p] !== y[p]) { return false; } break; } }
  • 28. FUNCTIONAL PROGRAMMING WITH IMMUTABLE.JS COMPARING TWO OBJECTS IN JAVASCRIPT
  • 29. FUNCTIONAL PROGRAMMING WITH IMMUTABLE.JS PARTICULAR PERFORMANCE IMPROVEMENTS ARE AVAILABLE DRAW DOM BASED ON THE STATE STATE OBJECT SOME INPUT STORE OLD REF COMPARE STATE REF VS OLD REF Same Differ
  • 31. FUNCTIONAL PROGRAMMING WITH IMMUTABLE.JS PERSISTENT DATA STRUCTURES 1 List.of([ 1, 2, 3, 5, 8 ]).push(13) 2 3 50x0012 8
  • 32. FUNCTIONAL PROGRAMMING WITH IMMUTABLE.JS PERSISTENT DATA STRUCTURES 1 2 3 50x0012 1 2 3 50x0042 8 8 13 List.of([ 1, 2, 3, 5, 8 ]).push(13)
  • 33. TRIE
  • 34. FUNCTIONAL PROGRAMMING WITH IMMUTABLE.JS STRUCTURAL SHARING 2 3 5 0x0012 1 8 List.of([ 1, 2, 3, 5, 8 ]).push(13)
  • 35. FUNCTIONAL PROGRAMMING WITH IMMUTABLE.JS STRUCTURAL SHARING 2 3 5 0x0012 1 8 1 8 13 0x0042 List.of([ 1, 2, 3, 5, 8 ]).push(13)
  • 38. FUNCTIONAL PROGRAMMING WITH IMMUTABLE.JS IMMUTABLE.JS var map = Immutable.Map({ a: 1, b: 2, c: 3 }) map .set('b', 50) .get('b') // 50 var list = Immutable.List.of(1, 2) list .push(3, 4, 5) .unshift(0) .concat(list2, list3) .get(0) .size var nested = Immutable.fromJS({ user: { profile: { name: 'John' } } }) nested .mergeDeep({ user: { profile: { age: 90 } } }) .setIn([ 'user', 'profile', 'name' ], 'Jack') .updateIn([ 'user', 'profile', 'name' ], (s) => s.toUpperCase()) .getIn(['user', 'profile', 'name']) // 'JACK'

Notes de l'éditeur

  1. Thank Steve + Yolk Introduce myself. Graduated in 2002, been developing for 15 years. Today I will give an overview about what immutable data structures are what functional programming is and how immutable.js can help you to write pure functions in javascript efficiently
  2. Some people will be thinking less than 4 years, some will think that he will see out the term. There may be some that think 8 years. Forever… the next one will be the 46th Does anyone think more than 8 years?
  3. I wanted to think about what immutable objects there are in the real world. Ask the audience for their ideas
  4. Prohibition is 18th Amendment, 21st Amendment repeal If we are concerned with truthfulness, then it is a good idea to use an immutable object
  5. This is just very normal to us
  6. This shows an inconsistency, which could cause bugs At the point in time that we are performing the push operation we don’t know what a actually is. Something else could have messed with it
  7. Objects are usually made up of identity, internal state and behaviour The problem is that in object-orientation you usually don't create data-structures. You encapsulate and hide data instead. Data-access is often even viewed as bad We often think about objects in the present
  8. All well and good but what’s so great about this…
  9. Something that I’m new to. I’ve had my brain wired for Object-orientated programming for 15 years. It’s a bit hipster - Lisp was invented in 1958 Until recently has been seen as something for mathematicians - not part of the enterprise world. Parts can get really hard - e.g. Monads This is changing because: Current popularity due to massively parallel systems. Fixing problems with state management Due to Javascript not being purely functional - it is a great introduction to FP. Some lousy things go away like ‘this’
  10. Functional programming at its core is is thinking of functions as king rather than objects. Data is transformed through functions which can be composed together to make an application With functional programming, for a given input it will return a consistent output Truth
  11. Higher-order function Snazzy ES-6 notation
  12. The console is not part of the function
  13. When dealing with objects and arrays there is a danger of unintended impurity
  14. Immutablity is not a magic bullet, but used well, they can be used to improve the performance of applications
  15. Maybe key presses don’t change the DOM. Maybe this is a webmail client and it is polling the network
  16. Copying is simply a matter of creating a new reference to the existing piece of data
  17. With immutable objects you only create a new object if the object has changed. This is comparing two references and couldn’t be much cheaper and easier.
  18. Copying is simply a matter of creating a new reference to the existing piece of data However this can be further optimised thanks to a feature of immutable.js…
  19. People can’t let go of the idea that you are creating masses of data
  20. You can see from this how an undo feature is really easy. We just somehow go back to the memory address 0x0012
  21. Immutable objects can be a beneficial tool in your programming arsenal Reach for them when you care about the truthfulness of state