45
A GENERAL THEORY OF REACTIVITY KRIS KOWAL [email protected] @KRISKOWAL

A General Theory of Reactivity

Embed Size (px)

Citation preview

Page 1: A General Theory of Reactivity

A GENERAL THEORY OF REACTIVITYKRIS KOWAL [email protected] @KRISKOWAL

Page 2: A General Theory of Reactivity

Troy McClure — The Simpsons

Page 3: A General Theory of Reactivity

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

Page 4: A General Theory of Reactivity
Page 5: A General Theory of Reactivity
Page 6: A General Theory of Reactivity

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

Page 7: A General Theory of Reactivity

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

Page 8: A General Theory of Reactivity

A GENERAL THEORY OF REACTIVITY (GTOR) KRIS KOWAL

MARCH 1, 2015

STREAMS ARE LIKE ARRAYS (PLURAL) PROMISES (ASYNC)

return Stream.from(fileNames).map(fileName => fs.readFile(fileName).then(content => ({fileName, content}) }, null, 20).forEach(({fileName, content}) => console.log(fileName, content.length));

8

Page 9: A General Theory of Reactivity

value

getter setter

singular

synchronous

Duality — a là Erik Meijer

Page 10: A General Theory of Reactivity

value (getter and setter)

get(Void):Value

set(Value):Void

info

rmat

ion

Page 11: A General Theory of Reactivity

space

singular plural

sync

collection

iterator generator

value

getter setter

Page 12: A General Theory of Reactivity

iterator (pull, sync)

next():{done:Boolean, value:Value}

info

rmat

ion

Page 13: A General Theory of Reactivity

generator (push, sync)

{next(Value), return(Value), throw(Error)}

info

rmat

ion

Page 14: A General Theory of Reactivity

generator and observer (push, sync)

{observe(onNext(Value), onReturn(Value), onThrow(Error))}

{next(Value), return(Value), throw(Error)}

info

rmat

ion

Page 15: A General Theory of Reactivity

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

Page 16: A General Theory of Reactivity

generator function (pull, sync)

next(Void):Iteration<Value>

yield Value / return Value / throw Error

info

rmat

ion

Page 17: A General Theory of Reactivity

A GENERAL THEORY OF REACTIVITY (GTOR) KRIS KOWAL

MARCH 1, 2015

ARRAY-RETURNING FUNCTION

function range(start, stop, step) { var result = []; while (start < stop) { result.push(start); start += step; } return result;}

17

Page 18: A General Theory of Reactivity

A GENERAL THEORY OF REACTIVITY (GTOR) KRIS KOWAL

MARCH 1, 2015

GENERATOR FUNCTION

function* range(start, stop, step) {

while (start < stop) {

yield start;

start += step;

} }

var iterator = range(0, 4, 2);

iterator.next(); // {done: false, value: 0}

iterator.next(); // {done: false, value: 2}

iterator.next(); // {done: true, value: undefined}

18

Page 19: A General Theory of Reactivity

space

time

sync

async

singular plural

collection

iterator generator

value

getter setter

deferred

promise resolver

Page 20: A General Theory of Reactivity

deferred (pomise and resolver)

{then(onReturn(Value), onThrow(Error))}

{return(Value | Promise<Value>), throw(Error)}

info

rmat

ion

Page 21: A General Theory of Reactivity

A GENERAL THEORY OF REACTIVITY (GTOR) KRIS KOWAL

MARCH 1, 2015

PROMISE IN A NUTSHELL

out = in.then( inval => outres, inerr => outres );

21

Page 22: A General Theory of Reactivity

A GENERAL THEORY OF REACTIVITY (GTOR) KRIS KOWAL

MARCH 1, 2015

PLAN INTERFERENCE

var i = 0; yolo(function lol() { i++; }); console.log(i);

22

Page 23: A General Theory of Reactivity

time

then(onReturn)

resolve(value)

time

resolve(value)

then(onReturn)

promises and order independence

onReturn(value)

Page 24: A General Theory of Reactivity

A GENERAL THEORY OF REACTIVITY (GTOR) KRIS KOWAL

MARCH 1, 2015

ASYNC FUNCTION

function time(job) { let start = Date.now(); return job().then( () => Date.now() - start ); } async function sum(getX, getY) { return await getX() + await getY(); } sum(time(task), time(task)).then(console.log);

24

Page 25: A General Theory of Reactivity

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

Page 26: A General Theory of Reactivity

space

time

sync

async

singular plural

collection

iterator generator

value

getter setter

stream

reader writer

deferred

promise resolver

Page 27: A General Theory of Reactivity

promise queue

get() Promise<Value>

put(Value | Promise<Value>)

info

rmat

ion

Page 28: A General Theory of Reactivity

order

put

order

promise queues and order independence

put

producer

consumer

put

get getget

Page 29: A General Theory of Reactivity

promise queue to transport iterations

get() Promise<{value: Value, done: Boolean}>

put({value: Value, done: Boolean})

info

rmat

ion

Page 30: A General Theory of Reactivity

A GENERAL THEORY OF REACTIVITY (GTOR) KRIS KOWAL

MARCH 1, 2015

LINKED LIST MEETS TEMPORAL RIFT

PROMISE QUEUE IMPLEMENTATION

function AsyncQueue() { var ends = Promise.defer(); this.put = function (value) { var next = Promise.defer(); ends.resolver.return({head: value, tail: next.promise}); ends.resolver = next.resolver; }; this.get = function () { var result = ends.promise.get("head"); ends.promise = ends.promise.get("tail"); return result; }; }

30

Page 31: A General Theory of Reactivity

reader.next() -> promise

writer.next(value) -> promise

promise queuesput

get put

get

Page 32: A General Theory of Reactivity

buffer (async, plural, push and pull)

{next(Value) -> Promise<Iteration<Value>>, return(Value), throw(Error)}

bid

irec

tional

Page 33: A General Theory of Reactivity

A GENERAL THEORY OF REACTIVITY (GTOR) KRIS KOWAL

MARCH 1, 2015

ASYNC GENERATOR FUNCTION

async function* sumPairwise(stream) { while (true) let x = await stream.next(); if (x.done) return x.value; let y = await stream.next(); if (y.done) return y.value; yield x.value + y.value; } }

33

Page 34: A General Theory of Reactivity

space

time

sync

async

singular plural

collection

iterator generator

value

getter setter

stream

reader writer

deferred

promise resolver

Page 35: A General Theory of Reactivity

A GENERAL THEORY OF REACTIVITY (GTOR) KRIS KOWAL

MARCH 1, 2015

SHARE A STREAM

var source = Stream.from(Iterator.range(100)) .map((n) => Task.delay(250).thenReturn(n))

Iterator.range(3).forEach((delay) => return source.map( (n) => Task.delay(delay * 1000).thenReturn(n); ); );

35

Page 36: A General Theory of Reactivity
Page 37: A General Theory of Reactivity

A GENERAL THEORY OF REACTIVITY (GTOR) KRIS KOWAL

MARCH 1, 2015

FORK A STREAM

var branches = Stream.from(Iterator.range(100)) .map((n) => Task.delay(250).thenReturn(n)) .fork(3); branches.forEach( (branch) => branch.forEach( (n) => Task.delay(1000 + Math.random() * 1000) .thenReturn(n) ).done() );

37

Page 38: A General Theory of Reactivity
Page 39: A General Theory of Reactivity

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.delay(random()).thenReturn(n), null, 32) .map((n) => Task.delay(random()).thenReturn(n), null, 16) .map((n) => Task.delay(random()).thenReturn(n), null, 4) .map((n) => Task.delay(random()).thenReturn(n), null, 1) .forEach((n) => null).done()

39

Page 40: A General Theory of Reactivity
Page 41: A General Theory of Reactivity

A GENERAL THEORY OF REACTIVITY (GTOR) KRIS KOWAL

MARCH 1, 2015

REDUCE A STREAM TO FIND THE MAX

return Stream.from(source).reduce((max, value) => Task.delay(500 + 500 * Math.random()) .thenReturn(Math.max(max, value)) ).then((max) => { console.log(max);})

41

Page 42: A General Theory of Reactivity
Page 43: A General Theory of Reactivity

A GENERAL THEORY OF REACTIVITY (GTOR) KRIS KOWAL

MARCH 1, 2015

MAP | REDUCE

return Stream.from(source).map( (value) => Task.delay(random()) .thenReturn(value), null, 32 ).reduce( (max, value) => Task.delay(random()) .thenReturn(Math.max(max, value)), null, 32 );

43

Page 44: A General Theory of Reactivity
Page 45: A General Theory of Reactivity

GTORKRIS KOWAL [email protected] @KRISKOWAL