Functional Reactive Programming with Bacon.js

Junction 22.9.2015

Olli Mahlamäki / Reaktor

Reaktor logo

Agenda

  • What?
  • Why?
  • Why not?

What?

Bacon.js

Javascript FRP library

Functional programming

FP


var messages = [
  { from: "olli",     to: "fernando", text: "Hi Ferkku" },
  { from: "fernando", to: "olli",     text: "What's up?" },
  { from: "olli",     to: "fernando", text: "Giving a presentation to Aalto ES" },
  { from: "fernando", to: "olli",     text: "Ok cool" },
  ]

messages
  .filter(function(message) { return isDirectedToMe(message) })
  .map(function(message) { return message.text })
  .forEach(show)
          

FRP (with bacon)


var messages = [
  { from: "olli",     to: "fernando", text: "Hi Ferkku" },
  { from: "fernando", to: "olli",     text: "What's up?" },
  { from: "olli",     to: "fernando", text: "Giving a presentation to Aalto ES" },
  { from: "fernando", to: "olli",     text: "Ok cool" },
  ]

// Simulate messages coming from server
var messagesE = Bacon.sequentially(1000, messages)

messagesE
  .filter(function(message) { return isDirectedToMe(message) })
  .map(function(message) { return message.text })
  .forEach(show)
          

Bacon vs. Promises

  • Both solve async cases
  • Promise: 0-1 values
  • EventStream: 0-n values
  • Combo: Bacon.fromPromise(...)

Why?

Why

  • Async problems are hard
  • Almost every modern web app is async
  • Tools from FP to async problems

Bacon.js ready for production

Bacon <3 React

Why not?

Why not?

  • Tough learning curve
  • Discipline needed for architecture

Where to next?

THE END

Olli Mahlamäki @omahlama