a general theory of reactivity

Post on 16-Apr-2017

82 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

A GENERAL THEORY OF REACTIVITYKRIS 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(fileNames).map(fileName => fs.readFile(fileName).then(content => ({fileName, content}) }, null, 20).forEach(({fileName, content}) => console.log(fileName, content.length));

8

value

getter setter

singular

synchronous

Duality — a là Erik Meijer

value (getter and setter)

get(Void):Value

set(Value):Void

info

rmat

ion

space

singular plural

sync

collection

iterator generator

value

getter setter

iterator (pull, sync)

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

info

rmat

ion

generator (push, sync)

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

info

rmat

ion

generator and observer (push, sync)

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

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

info

rmat

ion

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

info

rmat

ion

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

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

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)}

info

rmat

ion

A GENERAL THEORY OF REACTIVITY (GTOR) KRIS KOWAL

MARCH 1, 2015

PROMISE IN A NUTSHELL

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

21

A GENERAL THEORY OF REACTIVITY (GTOR) KRIS KOWAL

MARCH 1, 2015

PLAN INTERFERENCE

var i = 0; yolo(function lol() { i++; }); console.log(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

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

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>)

info

rmat

ion

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})

info

rmat

ion

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

reader.next() -> promise

writer.next(value) -> promise

promise queuesput

get put

get

buffer (async, plural, push and pull)

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

bid

irec

tional

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

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.delay(250).thenReturn(n))

Iterator.range(3).forEach((delay) => return source.map( (n) => Task.delay(delay * 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.delay(250).thenReturn(n)) .fork(3); branches.forEach( (branch) => branch.forEach( (n) => Task.delay(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.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

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

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

GTORKRIS KOWAL KRIS@UBER.COM @KRISKOWAL

top related