Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Functional Reactive JavaScript

Functional Reactive JavaScript

Slides from a talk at the SF JavaScript meetup: http://www.meetup.com/jsmeetup/events/199214912/

code examples from the talk: http://frp-talk-examples.thepete.net/

Pete Hodgson

August 26, 2014
Tweet

More Decks by Pete Hodgson

Other Decks in Programming

Transcript

  1. variable
 snapshot of a value 
 at a specific time

    stream
 all future values over time
  2. UI elements produce streams of values. Functions combine and transform

    those streams into new streams. Streams can be assigned to UI attributes 
 to effect UI output.
  3. slider <div> var wireUpSliderToLabel = function ( $slider, $label ){

    var sliderValue = $slider.asEventStream('input', eventToVal); sliderValue.assign( $label, "text" ); }; ! var eventToVal = function(e){ return $(e.target).val(); };
  4. slider <div> var wireUpSliderToLabel = function ( $slider, $label ){

    var sliderValue = $slider.asEventStream('input', eventToVal); sliderValue.assign( $label, "text" ); }; ! var eventToVal = function(e){ return $(e.target).val(); };
  5. slider <div> var wireUpSliderToLabel = function ( $slider, $label ){

    var sliderValue = $slider.asEventStream('input', eventToVal); sliderValue.assign( $label, "text" ); }; ! var eventToVal = function(e){ return $(e.target).val(); };
  6. slider <div> 5 5 var wireUpSliderToLabel = function ( $slider,

    $label ){ var sliderValue = $slider.asEventStream('input', eventToVal); sliderValue.assign( $label, "text" ); }; ! var eventToVal = function(e){ return $(e.target).val(); };
  7. slider <div> 5 5 5 var wireUpSliderToLabel = function (

    $slider, $label ){ var sliderValue = $slider.asEventStream('input', eventToVal); sliderValue.assign( $label, "text" ); }; ! var eventToVal = function(e){ return $(e.target).val(); };
  8. 5 5 5 2 var wireUpSliderToLabel = function ( $slider,

    $label ){ var sliderValue = $slider.asEventStream('input', eventToVal); sliderValue.assign( $label, "text" ); }; ! var eventToVal = function(e){ return $(e.target).val(); };
  9. 5 5 5 2 2 var wireUpSliderToLabel = function (

    $slider, $label ){ var sliderValue = $slider.asEventStream('input', eventToVal); sliderValue.assign( $label, "text" ); }; ! var eventToVal = function(e){ return $(e.target).val(); };
  10. formatFn var wireUpSliderToFormattedLabel = function ( $slider, $label ){ var

    sliderValue = $slider.asEventStream('input', eventToVal); var formattedValue = sliderValue.map( ratioToPercentage ); formattedValue.assign( $label, "text" ); }; ! var ratioToPercentage = function(ratio){ return "" + Math.round(ratio*100) + "%"; } ! var eventToVal = function(e){ return $(e.target).val(); };
  11. formatFn 0.05 var wireUpSliderToFormattedLabel = function ( $slider, $label ){

    var sliderValue = $slider.asEventStream('input', eventToVal); var formattedValue = sliderValue.map( ratioToPercentage ); formattedValue.assign( $label, "text" ); }; ! var ratioToPercentage = function(ratio){ return "" + Math.round(ratio*100) + "%"; } ! var eventToVal = function(e){ return $(e.target).val(); };
  12. “5%” formatFn 0.05 0.05 var wireUpSliderToFormattedLabel = function ( $slider,

    $label ){ var sliderValue = $slider.asEventStream('input', eventToVal); var formattedValue = sliderValue.map( ratioToPercentage ); formattedValue.assign( $label, "text" ); }; ! var ratioToPercentage = function(ratio){ return "" + Math.round(ratio*100) + "%"; } ! var eventToVal = function(e){ return $(e.target).val(); };
  13. “5%” 5% formatFn 0.05 0.05 var wireUpSliderToFormattedLabel = function (

    $slider, $label ){ var sliderValue = $slider.asEventStream('input', eventToVal); var formattedValue = sliderValue.map( ratioToPercentage ); formattedValue.assign( $label, "text" ); }; ! var ratioToPercentage = function(ratio){ return "" + Math.round(ratio*100) + "%"; } ! var eventToVal = function(e){ return $(e.target).val(); };
  14. variable
 snapshot of a value 
 at a specific time

    stream
 all future values over time
  15. var a = 12 var b = a + 10

    // b == 22 a = 20 // b == 22
  16. var a = 12 var b = a + 10

    // b == 22 a = 20 // b == 22 var a = aStreamWith(12) var b = a.map( -> + 10 ) a.updateTo(20)