SlideShare a Scribd company logo
Add some FUN to your
Functional programming
with RXJS
Java…
Groovy…
and JavaScript
Senior UI Engineer at Netflix
We do a lot of RX stuff with…
About Me
bittersweetryan
About Me
Before We Begin
Before We Begin
Be KIND to those of us with OCD and…
Before We Begin
Change those Dropbox icons to B&W!
Functional Review!
(With an RX twist)
Functional Review
Map
Reduce
Filter
Zip
var searchResultsSets =
keyups.
map( function( key ){
return Observable.getJSON('/search?' +
input.value)
});
Functional Review
Map - Transforms data
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
var html =
searchResultsSets.
reduce(function( prev,curr ) {
return prev + '<li>' + curr.name + '</li>';
},'' );
Functional Review
Reduce - Turns a collection into a single value
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…' } ]
Functional Review
Observable Data Streams Are Like Crazy Straws
keyPresses Observable
forEach
throttle
filter
map
distinctUntilChanged
reduce
Thinking Functionally!
!
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 );
}
});
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
})
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
}
});
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
});
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.
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.
Flattening Patterns!
managing concurrency!
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
Observables = Events Over Time
1s 2s 3s 4s 5s 6s
BR
BRE
BREAK
BREAKJ
BREAKING
BREAKING BA
BREAKING BAD
Ajax requests over time…
The Three Musketeers
Goofy as merge
Donald as concat
Mickey as switchLatest
Starring
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
Merge
1s 2s
http://jsbin.com/wehusi/13/edit
data
data
data
data
Concat
1s 2s
http://jsbin.com/fejod/4/edit
!
!
data
data
data
data
SwitchLatest
!
1s 2s
data
data
data
data
Building Animated
AutoComplete!
Putting Everything Together
Animated Autocomplete
SearchBBrBreBreaBreak
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
var keyups =
Observable.
fromEvent( searchInput, 'keypress');
Animated Autocomplete
var searchResultsSets =
keyups.
filter( function ( e ){
return input.value.length > 1;
}).
map( function( e ){
return Observable.
getJSON('/search?' + input.value);
}).
switchLatest();
Animated Autocomplete
var animateOuts =
keyups.
map(function( resultSet ){
return animateOut(resultsDiv);
});
!
var animateIns =
searchResultsSets.
map( function( resultSet ){
return Observable.
of(resultsSet).
concat(animateIn(resultsDiv));
});
Animated Autocomplete
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
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
Thank You.!
!
ranklam@netflix.com

@bittersweetryan!

More Related Content

PDF
rx.js make async programming simpler
PDF
RxJS 5 in Depth
PPTX
Luis Atencio on RxJS
PPTX
Async Redux Actions With RxJS - React Rally 2016
PDF
RxJS - The Reactive extensions for JavaScript
PPTX
Functional Reactive Programming with RxJS
PDF
RxJS Evolved
PPTX
Angular2 rxjs
rx.js make async programming simpler
RxJS 5 in Depth
Luis Atencio on RxJS
Async Redux Actions With RxJS - React Rally 2016
RxJS - The Reactive extensions for JavaScript
Functional Reactive Programming with RxJS
RxJS Evolved
Angular2 rxjs

What's hot (20)

PDF
Cascadia.js: Don't Cross the Streams
PPTX
Data Types and Processing in ES6
PDF
Ns2: Introduction - Part I
PPTX
RxJS In-Depth - AngularConnect 2015
PPTX
The State of JavaScript
PDF
NS2: AWK and GNUplot - PArt III
PDF
Ns2: OTCL - PArt II
PDF
Compose Async with RxJS
PDF
An Intro To ES6
PDF
서버 개발자가 바라 본 Functional Reactive Programming with RxJava - SpringCamp2015
PPT
ECMAScript 6: A Better JavaScript for the Ambient Computing Era
PDF
Map kit light
PDF
GPars For Beginners
PPTX
MiamiJS - The Future of JavaScript
PDF
如何「畫圖」寫測試 - RxJS Marble Test
PDF
RxJS101 - What you need to know to get started with RxJS tomorrow
PDF
Functional Reactive Programming / Compositional Event Systems
PDF
Oop assignment 02
PDF
Mozilla とブラウザゲーム
PDF
Think Async: Asynchronous Patterns in NodeJS
Cascadia.js: Don't Cross the Streams
Data Types and Processing in ES6
Ns2: Introduction - Part I
RxJS In-Depth - AngularConnect 2015
The State of JavaScript
NS2: AWK and GNUplot - PArt III
Ns2: OTCL - PArt II
Compose Async with RxJS
An Intro To ES6
서버 개발자가 바라 본 Functional Reactive Programming with RxJava - SpringCamp2015
ECMAScript 6: A Better JavaScript for the Ambient Computing Era
Map kit light
GPars For Beginners
MiamiJS - The Future of JavaScript
如何「畫圖」寫測試 - RxJS Marble Test
RxJS101 - What you need to know to get started with RxJS tomorrow
Functional Reactive Programming / Compositional Event Systems
Oop assignment 02
Mozilla とブラウザゲーム
Think Async: Asynchronous Patterns in NodeJS
Ad

Viewers also liked (20)

PDF
Rethink Async With RXJS
PPTX
Reactive Extensions for JavaScript
PPTX
Rxjs ngvikings
PPTX
RxJS and Reactive Programming - Modern Web UI - May 2015
PDF
You will learn RxJS in 2017
PPTX
Solit 2014, Реактивный Javascript. Победа над асинхронностью и вложенностью, ...
PDF
FRP with Ractive and RxJS
PDF
ReactiveX-SEA
PDF
Progressive Web Apps
PDF
Programação reativa com RxJS e Angular
PPTX
Reactive programming
PDF
Functional Reactive Angular 2
PPTX
Angular2 + rxjs
PPTX
PPTX
Reactive Programming and RxJS
PDF
Reactive design: languages, and paradigms
PDF
RxJS - The Reactive Extensions for JavaScript
PDF
Java 8 Stream API and RxJava Comparison
PPTX
Reactive Programming in Java 8 with Rx-Java
PDF
Electron, databases, and RxDB
Rethink Async With RXJS
Reactive Extensions for JavaScript
Rxjs ngvikings
RxJS and Reactive Programming - Modern Web UI - May 2015
You will learn RxJS in 2017
Solit 2014, Реактивный Javascript. Победа над асинхронностью и вложенностью, ...
FRP with Ractive and RxJS
ReactiveX-SEA
Progressive Web Apps
Programação reativa com RxJS e Angular
Reactive programming
Functional Reactive Angular 2
Angular2 + rxjs
Reactive Programming and RxJS
Reactive design: languages, and paradigms
RxJS - The Reactive Extensions for JavaScript
Java 8 Stream API and RxJava Comparison
Reactive Programming in Java 8 with Rx-Java
Electron, databases, and RxDB
Ad

Similar to Add Some Fun to Your Functional Programming With RXJS (20)

PDF
Rethink Async with RXJS
PDF
Rethink Async with RXJS
PPTX
Javascript And J Query
PDF
Design for succcess with react and storybook.js
PDF
PK chunking presentation from Tahoe Dreamin' 2016
PPTX
JavaScript Objects and OOP Programming with JavaScript
PDF
Forcelandia 2016 PK Chunking
PDF
ReactJS
PDF
ES6: The Awesome Parts
ODP
Pick up the low-hanging concurrency fruit
PDF
Cocoa Design Patterns in Swift
PDF
Spark with Elasticsearch
PDF
React.js Basics - ConvergeSE 2015
PDF
Cycle.js - A functional reactive UI framework
PDF
Cycle.js - Functional reactive UI framework (Nikos Kalogridis)
KEY
Object-Oriented JavaScript
KEY
Object-Oriented Javascript
PPTX
Столпы функционального программирования для адептов ООП, Николай Мозговой
PDF
Functional Programming in Go
PDF
Programmation fonctionnelle en JavaScript
Rethink Async with RXJS
Rethink Async with RXJS
Javascript And J Query
Design for succcess with react and storybook.js
PK chunking presentation from Tahoe Dreamin' 2016
JavaScript Objects and OOP Programming with JavaScript
Forcelandia 2016 PK Chunking
ReactJS
ES6: The Awesome Parts
Pick up the low-hanging concurrency fruit
Cocoa Design Patterns in Swift
Spark with Elasticsearch
React.js Basics - ConvergeSE 2015
Cycle.js - A functional reactive UI framework
Cycle.js - Functional reactive UI framework (Nikos Kalogridis)
Object-Oriented JavaScript
Object-Oriented Javascript
Столпы функционального программирования для адептов ООП, Николай Мозговой
Functional Programming in Go
Programmation fonctionnelle en JavaScript

Recently uploaded (20)

PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
KodekX | Application Modernization Development
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Approach and Philosophy of On baking technology
PPTX
Cloud computing and distributed systems.
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
cuic standard and advanced reporting.pdf
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Encapsulation theory and applications.pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Building Integrated photovoltaic BIPV_UPV.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
NewMind AI Monthly Chronicles - July 2025
Dropbox Q2 2025 Financial Results & Investor Presentation
“AI and Expert System Decision Support & Business Intelligence Systems”
The Rise and Fall of 3GPP – Time for a Sabbatical?
KodekX | Application Modernization Development
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
NewMind AI Weekly Chronicles - August'25 Week I
Approach and Philosophy of On baking technology
Cloud computing and distributed systems.
CIFDAQ's Market Insight: SEC Turns Pro Crypto
cuic standard and advanced reporting.pdf
Understanding_Digital_Forensics_Presentation.pptx
Encapsulation theory and applications.pdf

Add Some Fun to Your Functional Programming With RXJS