Streams

Definition

  • But first, an experiment!
  • Flowing through instead of stopping every step               

 

 

 

  • List structure, but not all values at once (Delayed List)
  • Operate without having to know or care when the data flow ends (or if it ever ends)
  • Operate without having to know or care how much data is in the stream in total

A

B

C

A

B

C

Have you used streams?

youtube.com

Have you used streams?

Pipes

When? / Why?

  • Can't / Don't want to hold everything in memory
  • Use less bandwidth
  • Able to work with (potentially) infinite data
  • Separation of concerns (e.g. in Unix)

Push Streams

  • Producer: "Have the data."
  • Pushes data out
  • Someone handles it when it arrives
  • "Callback"
  • Non-blocking
  • Possible overload: "back pressure"
    • Block at the source
    • Sample (most recent)
    • First only
    • Debounce
    • Buffer
    • ...

Pull Streams

  • Consumer: "Can I get some data?"
  • Consumer gets data themselves
  • "Iterator"
  • Blocking: "Timeout"

Reactive

Reactive?

In computing, reactive programming is a programming paradigm oriented around data flows and the propagation of change. This means that it should be possible to express static or dynamic data flows with ease in the programming languages used, and that the underlying execution model will automatically propagate change through the data flow.

 

Wikipedia

In computing, reactive programming is a programming paradigm oriented around data flows and the propagation of change. This means that it should be possible to express static or dynamic data flows with ease in the programming languages used, and that the underlying execution model will automatically propagate change through the data flow.

 

Wikipedia

Passive       Reactive

Foo updates Bar when something in Foo changes

Bar listenes to Events happening in Foo and updates itself

Reacts to an Event

$('#cats-btn').click(function () {  
  getDataFromServer('cats');
});

function formatCats(cat) {  
  return { name: 'Hello ' + cat.name }
}

function renderUI(data) {  
  UI.render(data);
}

function getDataFromServer(type) {  
  $.ajax(URL + type).done(function (cats) {
    renderUI(cats.map(formatCats));
  });
}
_('click', $('#cats-btn'))
  .pipe(getDataFromServer)
  .map(formatCats)
  .pipe(UI.render);
  • No intermediate variables
  • Clear data flow
  • Doesn't work with individual events, but instead with event streams
  • Raises the abstraction level - focus on implementation of events rather than combined details

Passive       Reactive

Reactive Pattern

By queicherius

Reactive Pattern

  • 891