Add Some Fun to Your Functional Programming With RXJS

4,539 views

Published on

Slides from my 2014 MounainWestJS talk "Add some Fun to Your Functional Programming With RXJS"

Published in: Technology

Add Some Fun to Your Functional Programming With RXJS

  1. 1. Add some FUN to your Functional programming with RXJS
  2. 2. Java… Groovy… and JavaScript Senior UI Engineer at Netflix We do a lot of RX stuff with… About Me
  3. 3. bittersweetryan About Me
  4. 4. Before We Begin
  5. 5. Before We Begin Be KIND to those of us with OCD and…
  6. 6. Before We Begin Change those Dropbox icons to B&W!
  7. 7. Functional Review! (With an RX twist)
  8. 8. Functional Review Map Reduce Filter Zip
  9. 9. var searchResultsSets = keyups. map( function( key ){ return Observable.getJSON('/search?' + input.value) }); Functional Review Map - Transforms data
  10. 10. var searchResultsSets = keyups. filter( function ( key ){ return input.value.length > 1; }). map( function( key ){ return Observable.getJSON('/search?' + input.value) ); Functional Review Filter - Narrows Collections
  11. 11. var html = searchResultsSets. reduce(function( prev,curr ) { return prev + '<li>' + curr.name + '</li>'; },'' ); Functional Review Reduce - Turns a collection into a single value
  12. 12. Functional Review Zip - Combines two collections var movies = [ 'Super Troopers', 'Pulp Fiction', 'Fargo' ] ; var boxArts =[ '/cdn/23212/120x80', '/cdn/73212/120x80','/cdn/99212/120x80' ] ; ! var withArt = Observable. zip(movies, boxarts, function(movie, boxart){ return {title : movie, boxart : boxart}; }); ! //[{title : 'Super Troo…', boxart : '/cdn…'}, // {title : 'Pulp Fict…', boxart : '/cdn…' }, // {title : 'Fargo', boxart : 'cdn…' } ]
  13. 13. Functional Review Observable Data Streams Are Like Crazy Straws keyPresses Observable forEach throttle filter map distinctUntilChanged reduce
  14. 14. Thinking Functionally! !
  15. 15. Thinking Functionally Replace loops with map, reduce, and filter. searchResults = Observable. getJSON(‘/people?’ + input.value); ! searchResults. forEach( function( reps ){ var names = []; for( var i = 1; i < resp; i++ ){ var name = data[i].fName + ' ' + data[i].lName; names.push( name ); } });
  16. 16. Thinking Functionally Replace loops with map and reduce. searchResults = Observable. getJSON(‘/people?’ + input.value). map( function( data ){ return data.fName + data.lName; } ); ! searchResults.forEach( function( resp ){ //resp will now be the names array })
  17. 17. Thinking Functionally Replace if’s with filters. var keyPresses = O.fromEvent( el, 'keyup' ) ! keyPresses.forEach( function( e ){ if( e.which === keys.enter ){ //=> no! //do something } });
  18. 18. Thinking Functionally Replace if’s with filters. var enterPresses = O.fromEvent( el, 'keyup' ) .filter( function( e ){ return e.which && e.which === keys.enter; }); ! enterPresses.forEach( function( e ){ //do something });
  19. 19. Thinking Functionally Don’t put too much in a single stream. var submits = O.fromEvent(input,'keypresses'). throttle(). map( function( e ){ return e.which } ). filter( function( key ){ return key === keys.enter || keys.escape; }). map(). reduce(). ... Smaller streams are OK.
  20. 20. Thinking Functionally var submits = Observable.fromEvent(input,'keypresses'). throttle(). map( function( e ){ return e.which } ); ! var enters = submits.filter( function( key ) ){ return e.which === keys.enter; } ! var escapes = submits.filter( function( key ) ){ Don’t put too much in a single stream. Smaller streams are OK.
  21. 21. Flattening Patterns! managing concurrency!
  22. 22. Observables = Events Over Time Key presses over time… .5s 1s 1.5s 2s 2.5s 3s B R E A K IJ <- N G B A D
  23. 23. Observables = Events Over Time 1s 2s 3s 4s 5s 6s BR BRE BREAK BREAKJ BREAKING BREAKING BA BREAKING BAD Ajax requests over time…
  24. 24. The Three Musketeers Goofy as merge Donald as concat Mickey as switchLatest Starring
  25. 25. Flattening Patterns merge - combines items in a collection as each item arrives concat - combines collections in the order they arrived switchLatest - switches to the latest collection that ! arrives
  26. 26. Merge 1s 2s http://jsbin.com/wehusi/13/edit data data data data
  27. 27. Concat 1s 2s http://jsbin.com/fejod/4/edit ! ! data data data data
  28. 28. SwitchLatest ! 1s 2s data data data data
  29. 29. Building Animated AutoComplete! Putting Everything Together
  30. 30. Animated Autocomplete SearchBBrBreBreaBreak
  31. 31. Observables = Events Over Time Simple Widget, High Complexity • Respond to key presses • Send off Ajax requests • Animate out when search results become invalid • Animate in when new search results come in • Don’t show old results • Make sure one animation is finished before starting 
 another
  32. 32. var keyups = Observable. fromEvent( searchInput, 'keypress'); Animated Autocomplete
  33. 33. var searchResultsSets = keyups. filter( function ( e ){ return input.value.length > 1; }). map( function( e ){ return Observable. getJSON('/search?' + input.value); }). switchLatest(); Animated Autocomplete
  34. 34. var animateOuts = keyups. map(function( resultSet ){ return animateOut(resultsDiv); }); ! var animateIns = searchResultsSets. map( function( resultSet ){ return Observable. of(resultsSet). concat(animateIn(resultsDiv)); }); Animated Autocomplete
  35. 35. var resultSets = animateOuts. merge(animateIns). concatAll(); ! resultSets.
 forEach( function( resultSet ){ if (resultSet.length === 0) { $('.search-results').addClass('hidden'); } else { resultsDiv.innerHTML = toHTML(resultSet ); } } ); Animated Autocomplete
  36. 36. var keyups = Observable. fromEvent( searchInput, ‘keypress'); !var searchResultsSets = keyups. filter( function ( e ){ return input.value.length > 1; }). map( function( e ){ return Observable. getJSON('/search?' + input.value); }). switchLatest(); var animateOuts = keyups. map(function( resultSet ){ return animateOut(resultsDiv); }); !var animateIns = searchResultsSets. map( function( resultSet ){ return Observable. of(resultsSet). concat(animateIn(resultsDiv)); }); !var resultSets = animateOuts. merge(animateIns). concatAll(); !resultSets.
 forEach( function( resultSet ){ if (resultSet.length === 0) { $('.search-results').addClass('hidden'); } else { resultsDiv.innerHTML = toHTML(resultSet ); } } ); Animated Autocomplete
  37. 37. Thank You.! ! ranklam@netflix.com
 @bittersweetryan!

×