SlideShare a Scribd company logo
1 of 45
Download to read offline
A GENERAL
THEORY OF
REACTIVITY
KRIS KOWAL
KRIS@UBER.COM
@KRISKOWAL
Troy McClure — The Simpsons
A GENERAL THEORY OF REACTIVITY (GTOR)
KRIS KOWAL
MARCH 1, 2015
A GENERAL THEORY OF RELATIVITY
3
Albert Einstein in 1921, as he rode in a motorcade in New York City with crowds welcoming his first visit to the U.S., Life Magazine, Public Domain
A GENERAL THEORY OF REACTIVITY (GTOR)
KRIS KOWAL
MARCH 1, 2015
• sync / async
• singular / plural
• single consumer / multiple consumers
• unicast (cancelable) / broadcast
• coping with fast producer | slow consumer
DIMENSIONS OF REACTIVITY
6
A GENERAL THEORY OF REACTIVITY (GTOR)
KRIS KOWAL
MARCH 1, 2015
• push → discrete over time, observables, gauges, drop messages
• pull ← continuous over time, behaviors, counters, lose fidelity
• pressure ↔ reliable, streams
FAST PRODUCER | SLOW CONSUMER
7
A GENERAL THEORY OF REACTIVITY (GTOR)
KRIS KOWAL
MARCH 1, 2015
STREAMS ARE LIKE ARRAYS (PLURAL) PROMISES (ASYNC)
return Stream.from(fileNames).map(fileName =>
fs.readFile(fileName).then(content =>
({fileName, content})
}, null, 20).forEach(({fileName, content}) =>
console.log(fileName, content.length)

);
8
value
getter setter
singular
synchronous
Duality — a là Erik Meijer
value (getter and setter)
get(Void):Value
set(Value):Void
information
space
singular plural
sync
collection
iterator generator
value
getter setter
iterator (pull, sync)
next():{done:Boolean, value:Value}
information
generator (push, sync)
{next(Value), return(Value), throw(Error)}
information
generator and observer (push, sync)
{observe(onNext(Value),
onReturn(Value),
onThrow(Error))}
{next(Value), return(Value), throw(Error)}
information
A GENERAL THEORY OF REACTIVITY (GTOR)
KRIS KOWAL
MARCH 1, 2015
PUSH
•Use Observer
•For discrete time series data
•changes in response to an event
•e.g., estimated time to completion
•(02:30)
•Rx, a là Erik Meijer
PUSH VS PULL FOR TIME SERIES VALUES
15
PULL
• Use Iterator
• For continuous time series data
• always changing, must be sampled
periodically
• e.g., progress to completion
• (50%)
• FRP, a là Conal Elliott
generator function (pull, sync)
next(Void):Iteration<Value>
yield Value / return Value / throw Error
information
A GENERAL THEORY OF REACTIVITY (GTOR)
KRIS KOWAL
MARCH 1, 2015
ARRAY-RETURNING FUNCTION
function range(start, stop, step) {
var result = [];
while (start < stop) {
result.push(start);
start += step;
}
return result;

}
17
A GENERAL THEORY OF REACTIVITY (GTOR)
KRIS KOWAL
MARCH 1, 2015
GENERATOR FUNCTION
function* range(start, stop, step) {
while (start < stop) {
yield start;
start += step;
}

}
var iterator = range(0, 4, 2);
iterator.next(); // {done: false, value: 0}
iterator.next(); // {done: false, value: 2}
iterator.next(); // {done: true, value: undefined}
18
space
time
sync
async
singular plural
collection
iterator generator
value
getter setter
deferred
promise resolver
deferred (pomise and resolver)
{then(onReturn(Value), onThrow(Error))}
{return(Value | Promise<Value>), throw(Error)}
information
A GENERAL THEORY OF REACTIVITY (GTOR)
KRIS KOWAL
MARCH 1, 2015
PROMISE IN A NUTSHELL
out = in.then(
inval => outres,
inerr => outres
);
21
A GENERAL THEORY OF REACTIVITY (GTOR)
KRIS KOWAL
MARCH 1, 2015
PLAN INTERFERENCE
var i = 0;
yolo(function lol() {
i++;

});
console.log(i);
22
time
then(onReturn)
resolve(value)
time
resolve(value)
then(onReturn)
promises and order independence
onReturn(value)
A GENERAL THEORY OF REACTIVITY (GTOR)
KRIS KOWAL
MARCH 1, 2015
ASYNC FUNCTION
function time(job) {
let start = Date.now();
return job().then(
() => Date.now() - start
);

}
async function sum(getX, getY) {
return await getX() + await getY();
}
sum(time(task), time(task)).then(console.log);
24
A GENERAL THEORY OF REACTIVITY (GTOR)
KRIS KOWAL
MARCH 1, 2015
PROMISES
25
•order independence
•guaranteed async (zalgo containment)
•defensive
•POLA (one-way communication)
•chainability
•composability
•gateway to proxies for remote objects
•a là Mark Miller
space
time
sync
async
singular plural
collection
iterator generator
value
getter setter
stream
reader writer
deferred
promise resolver
promise queue
get() Promise<Value>
put(Value | Promise<Value>)
information
order
put
order
promise queues and order independence
put
producer
consumer
put
get getget
promise queue to transport iterations
get() Promise<{value: Value, done: Boolean}>
put({value: Value, done: Boolean})
information
A GENERAL THEORY OF REACTIVITY (GTOR)
KRIS KOWAL
MARCH 1, 2015
LINKED LIST MEETS TEMPORAL RIFT
PROMISE QUEUE IMPLEMENTATION
function AsyncQueue() {
var ends = Promise.defer();
this.put = function (value) {
var next = Promise.defer();
ends.resolver.return({head: value, tail: next.promise});
ends.resolver = next.resolver;
};
this.get = function () {
var result = ends.promise.get("head");
ends.promise = ends.promise.get("tail");
return result;
};
}
30
reader.next() -> promise
writer.next(value) -> promise
promise queues
put
get put
get
buffer (async, plural, push and pull)
{next(Value) -> Promise<Iteration<Value>>,
return(Value),
throw(Error)}
bidirectional
A GENERAL THEORY OF REACTIVITY (GTOR)
KRIS KOWAL
MARCH 1, 2015
ASYNC GENERATOR FUNCTION
async function* sumPairwise(stream) {
while (true)
let x = await stream.next();
if (x.done) return x.value;
let y = await stream.next();
if (y.done) return y.value;
yield x.value + y.value;
}

}
33
space
time
sync
async
singular plural
collection
iterator generator
value
getter setter
stream
reader writer
deferred
promise resolver
A GENERAL THEORY OF REACTIVITY (GTOR)
KRIS KOWAL
MARCH 1, 2015
SHARE A STREAM
var source = Stream.from(Iterator.range(100))
.map((n) => Task.delay(250).thenReturn(n))
Iterator.range(3).forEach((delay) =>
return source.map(
(n) => Task.delay(delay * 1000).thenReturn(n);
);
);
35
A GENERAL THEORY OF REACTIVITY (GTOR)
KRIS KOWAL
MARCH 1, 2015
FORK A STREAM
var branches = Stream.from(Iterator.range(100))
.map((n) => Task.delay(250).thenReturn(n))
.fork(3);
branches.forEach(
(branch) => branch.forEach(
(n) => Task.delay(1000 + Math.random() * 1000)
.thenReturn(n)
).done()
);
37
A GENERAL THEORY OF REACTIVITY (GTOR)
KRIS KOWAL
MARCH 1, 2015
MAP A STREAM WITH A CONCURRENCY LIMIT
var branches = Stream.from(Iterator.range(100))
.map((n) => Task.delay(random()).thenReturn(n), null, 32)
.map((n) => Task.delay(random()).thenReturn(n), null, 16)
.map((n) => Task.delay(random()).thenReturn(n), null, 4)
.map((n) => Task.delay(random()).thenReturn(n), null, 1)
.forEach((n) => null).done()
39
A GENERAL THEORY OF REACTIVITY (GTOR)
KRIS KOWAL
MARCH 1, 2015
REDUCE A STREAM TO FIND THE MAX
return Stream.from(source).reduce((max, value) =>
Task.delay(500 + 500 * Math.random())
.thenReturn(Math.max(max, value))
).then((max) => {
console.log(max);

})
41
A GENERAL THEORY OF REACTIVITY (GTOR)
KRIS KOWAL
MARCH 1, 2015
MAP | REDUCE
return Stream.from(source).map(
(value) => Task.delay(random())
.thenReturn(value),
null, 32
).reduce(
(max, value) => Task.delay(random())
.thenReturn(Math.max(max, value)),
null, 32
);
43
GTORKRIS KOWAL
KRIS@UBER.COM
@KRISKOWAL

More Related Content

Featured

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

A General Theory of Reactivity

  • 1. A GENERAL THEORY OF REACTIVITY KRIS KOWAL KRIS@UBER.COM @KRISKOWAL
  • 2. Troy McClure — The Simpsons
  • 3. A GENERAL THEORY OF REACTIVITY (GTOR) KRIS KOWAL MARCH 1, 2015 A GENERAL THEORY OF RELATIVITY 3 Albert Einstein in 1921, as he rode in a motorcade in New York City with crowds welcoming his first visit to the U.S., Life Magazine, Public Domain
  • 4.
  • 5.
  • 6. A GENERAL THEORY OF REACTIVITY (GTOR) KRIS KOWAL MARCH 1, 2015 • sync / async • singular / plural • single consumer / multiple consumers • unicast (cancelable) / broadcast • coping with fast producer | slow consumer DIMENSIONS OF REACTIVITY 6
  • 7. A GENERAL THEORY OF REACTIVITY (GTOR) KRIS KOWAL MARCH 1, 2015 • push → discrete over time, observables, gauges, drop messages • pull ← continuous over time, behaviors, counters, lose fidelity • pressure ↔ reliable, streams FAST PRODUCER | SLOW CONSUMER 7
  • 8. A GENERAL THEORY OF REACTIVITY (GTOR) KRIS KOWAL MARCH 1, 2015 STREAMS ARE LIKE ARRAYS (PLURAL) PROMISES (ASYNC) return Stream.from(fileNames).map(fileName => fs.readFile(fileName).then(content => ({fileName, content}) }, null, 20).forEach(({fileName, content}) => console.log(fileName, content.length)
 ); 8
  • 10. value (getter and setter) get(Void):Value set(Value):Void information
  • 13. generator (push, sync) {next(Value), return(Value), throw(Error)} information
  • 14. generator and observer (push, sync) {observe(onNext(Value), onReturn(Value), onThrow(Error))} {next(Value), return(Value), throw(Error)} information
  • 15. A GENERAL THEORY OF REACTIVITY (GTOR) KRIS KOWAL MARCH 1, 2015 PUSH •Use Observer •For discrete time series data •changes in response to an event •e.g., estimated time to completion •(02:30) •Rx, a là Erik Meijer PUSH VS PULL FOR TIME SERIES VALUES 15 PULL • Use Iterator • For continuous time series data • always changing, must be sampled periodically • e.g., progress to completion • (50%) • FRP, a là Conal Elliott
  • 16. generator function (pull, sync) next(Void):Iteration<Value> yield Value / return Value / throw Error information
  • 17. A GENERAL THEORY OF REACTIVITY (GTOR) KRIS KOWAL MARCH 1, 2015 ARRAY-RETURNING FUNCTION function range(start, stop, step) { var result = []; while (start < stop) { result.push(start); start += step; } return result;
 } 17
  • 18. A GENERAL THEORY OF REACTIVITY (GTOR) KRIS KOWAL MARCH 1, 2015 GENERATOR FUNCTION function* range(start, stop, step) { while (start < stop) { yield start; start += step; }
 } var iterator = range(0, 4, 2); iterator.next(); // {done: false, value: 0} iterator.next(); // {done: false, value: 2} iterator.next(); // {done: true, value: undefined} 18
  • 20. deferred (pomise and resolver) {then(onReturn(Value), onThrow(Error))} {return(Value | Promise<Value>), throw(Error)} information
  • 21. A GENERAL THEORY OF REACTIVITY (GTOR) KRIS KOWAL MARCH 1, 2015 PROMISE IN A NUTSHELL out = in.then( inval => outres, inerr => outres ); 21
  • 22. A GENERAL THEORY OF REACTIVITY (GTOR) KRIS KOWAL MARCH 1, 2015 PLAN INTERFERENCE var i = 0; yolo(function lol() { i++;
 }); console.log(i); 22
  • 24. A GENERAL THEORY OF REACTIVITY (GTOR) KRIS KOWAL MARCH 1, 2015 ASYNC FUNCTION function time(job) { let start = Date.now(); return job().then( () => Date.now() - start );
 } async function sum(getX, getY) { return await getX() + await getY(); } sum(time(task), time(task)).then(console.log); 24
  • 25. A GENERAL THEORY OF REACTIVITY (GTOR) KRIS KOWAL MARCH 1, 2015 PROMISES 25 •order independence •guaranteed async (zalgo containment) •defensive •POLA (one-way communication) •chainability •composability •gateway to proxies for remote objects •a là Mark Miller
  • 26. space time sync async singular plural collection iterator generator value getter setter stream reader writer deferred promise resolver
  • 27. promise queue get() Promise<Value> put(Value | Promise<Value>) information
  • 28. order put order promise queues and order independence put producer consumer put get getget
  • 29. promise queue to transport iterations get() Promise<{value: Value, done: Boolean}> put({value: Value, done: Boolean}) information
  • 30. A GENERAL THEORY OF REACTIVITY (GTOR) KRIS KOWAL MARCH 1, 2015 LINKED LIST MEETS TEMPORAL RIFT PROMISE QUEUE IMPLEMENTATION function AsyncQueue() { var ends = Promise.defer(); this.put = function (value) { var next = Promise.defer(); ends.resolver.return({head: value, tail: next.promise}); ends.resolver = next.resolver; }; this.get = function () { var result = ends.promise.get("head"); ends.promise = ends.promise.get("tail"); return result; }; } 30
  • 31. reader.next() -> promise writer.next(value) -> promise promise queues put get put get
  • 32. buffer (async, plural, push and pull) {next(Value) -> Promise<Iteration<Value>>, return(Value), throw(Error)} bidirectional
  • 33. A GENERAL THEORY OF REACTIVITY (GTOR) KRIS KOWAL MARCH 1, 2015 ASYNC GENERATOR FUNCTION async function* sumPairwise(stream) { while (true) let x = await stream.next(); if (x.done) return x.value; let y = await stream.next(); if (y.done) return y.value; yield x.value + y.value; }
 } 33
  • 34. space time sync async singular plural collection iterator generator value getter setter stream reader writer deferred promise resolver
  • 35. A GENERAL THEORY OF REACTIVITY (GTOR) KRIS KOWAL MARCH 1, 2015 SHARE A STREAM var source = Stream.from(Iterator.range(100)) .map((n) => Task.delay(250).thenReturn(n)) Iterator.range(3).forEach((delay) => return source.map( (n) => Task.delay(delay * 1000).thenReturn(n); ); ); 35
  • 36.
  • 37. A GENERAL THEORY OF REACTIVITY (GTOR) KRIS KOWAL MARCH 1, 2015 FORK A STREAM var branches = Stream.from(Iterator.range(100)) .map((n) => Task.delay(250).thenReturn(n)) .fork(3); branches.forEach( (branch) => branch.forEach( (n) => Task.delay(1000 + Math.random() * 1000) .thenReturn(n) ).done() ); 37
  • 38.
  • 39. A GENERAL THEORY OF REACTIVITY (GTOR) KRIS KOWAL MARCH 1, 2015 MAP A STREAM WITH A CONCURRENCY LIMIT var branches = Stream.from(Iterator.range(100)) .map((n) => Task.delay(random()).thenReturn(n), null, 32) .map((n) => Task.delay(random()).thenReturn(n), null, 16) .map((n) => Task.delay(random()).thenReturn(n), null, 4) .map((n) => Task.delay(random()).thenReturn(n), null, 1) .forEach((n) => null).done() 39
  • 40.
  • 41. A GENERAL THEORY OF REACTIVITY (GTOR) KRIS KOWAL MARCH 1, 2015 REDUCE A STREAM TO FIND THE MAX return Stream.from(source).reduce((max, value) => Task.delay(500 + 500 * Math.random()) .thenReturn(Math.max(max, value)) ).then((max) => { console.log(max);
 }) 41
  • 42.
  • 43. A GENERAL THEORY OF REACTIVITY (GTOR) KRIS KOWAL MARCH 1, 2015 MAP | REDUCE return Stream.from(source).map( (value) => Task.delay(random()) .thenReturn(value), null, 32 ).reduce( (max, value) => Task.delay(random()) .thenReturn(Math.max(max, value)), null, 32 ); 43
  • 44.