SlideShare a Scribd company logo
1 of 27
Download to read offline
Functional
Reactive
Programming
Reactive?
Å reagere på asynkrone hendelser
Web applikasjon er reactive:

Mouse clicks, key presses, async data fra
server
Hvordan være reactive?
State brukes til å representere systemet på et gitt
tidspunkt
Vanskelig med feilhåndtering
Bruk av mye state fører til kompleks spaghettikode
Functional Reactive
Programming to the
rescue!
Functional Reactive Programming
http://stackoverflow.com/questions/1028250/what-is-functional-reactive-programming/1030631#1030631
Datatypes som representerer en verdi over tid
med verktøy fra funksjonell programmering.
• Composition
• Event streams
• Tools
Functional Reactive Programming
Functional
Event streams er Immutable



Operasjoner på en event stream
returnerer alltid en ny event stream.
Event streams
En sekvens av events sortert i tid
En event stream kan sende ut:
• Values
• Errors
• Completed signal
Click Click Click Click
X Time
CompletedError
Event streams
Å lytte på en stream kalles subscribing.
Fungerer som en asynkron callback akkurat som
promises!
Observer Design Pattern
• Subject: Event Stream
• Observer: Subscriber
Event streams
ALT kan være en stream! 

DOM events, user input, properties, promises
Streams kan være input til andre streams
Streams kan filtreres, slås sammen og
manipuleres
Reactive Extensions (Rx*): 

.NET, Java, Scala, Clojure, JavaScript, Ruby,
Python, C++, Objective-C/Cocoa, Groovy
Bacon.js
Elm - A functional reactive language
Rammeverk (Tools)
Bacon.js
“Turns your event spaghetti into clean
and declarative feng shui bacon"
Lage en event stream
• $(“#myDiv").asEventStream("click")
• Bacon.fromPromise(…)
• Bacon.fromEventTarget(..)
• Bacon.fromCallback(…), Bacon.fromNodeCallback(…)
• Bacon.fromPoll(..)
• Bacon.once(…), Bacon.never(), Bacon.later(…)
• Bacon.fromArray(…)
• Bacon.interval(…)
• Bacon.sequentially(…)
• Bacon.repeatedly(…)
• new Bacon.EventStream()
Transformere en event stream ved
å bruke funksjoner som
map, scan, fold, flatMap
Transformere event streams
map(f)

Utfører en funksjon f på alle verdier i en event stream.
scan(seed, f)

Som map(f) men med en seed-verdi som sendes inn i neste
funksjon.
fold(seed, f) (reduce)

Som scan, men fold sender bare ut sluttverdien.
flatMap(f)

Som map(f), men hvor f returnerer event streams. flatMap vil flate
ut alle event streams returnert fra f og returnere disse som én event
stream.
Påvirk når verdier kommer ut av en 

event stream med funksjoner som
debounce, debounceImmediate, throttle,
bufferingThrottle, delay, slidingWindow
Filtrer event streams med funksjoner som
filter, take, skip, takeWhile, skipWhile,
takeUntil, skipUntil
Slå sammen streams
med funksjoner som
merge, zip og concat
Thinking reactive
Thinking reactive
• Alt kan representeres som en stream
• Sett sammen og manipuler streams til bedre
spesialiserte streams til formålet
• GUI-rendring i subscribe-funksjon
Demo - click events
Demo - drag drop
Hva er gjør man når man drar en ting rundt på
skjermen?
1. Start: Mouse down
2. Flytt: Mouse move
3. Stop: Mouse up
Demo - Konami Code
Alle nettsider med respekt for seg selv må ha
innebygde easter eggs.
↑ ↑ ↓ ↓ ← → ← → B A
Fordeler med FRP
• Abstraherer bort implementasjonsdetaljer
• Mindre state
• Immutable event streams gjør det lett å gjenbruke
• Forutsigbar kode
Dette høres ut som
noe hipster tøys
Hvem bruker FRP?
Facebook
Flux/React
Netflix
RxJava
Dette ble litt mye å huske på
Koden fra demo
https://github.com/olavhaugen/fagkveld-frp
The introduction to Reactive Programming 

you've been missing 



https://gist.github.com/staltz/868e7e9bc2a7b8c1f754

(Bare google: “Reactive programming”)
Bacon.js
https://github.com/baconjs/bacon.js/

http://baconjs.blogspot.fi/
RxMarbles
http://rxmarbles.com/

More Related Content

Featured

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Functional Reactive Programming

  • 2. Reactive? Å reagere på asynkrone hendelser Web applikasjon er reactive:
 Mouse clicks, key presses, async data fra server
  • 3. Hvordan være reactive? State brukes til å representere systemet på et gitt tidspunkt Vanskelig med feilhåndtering Bruk av mye state fører til kompleks spaghettikode
  • 6. Datatypes som representerer en verdi over tid med verktøy fra funksjonell programmering. • Composition • Event streams • Tools Functional Reactive Programming
  • 7. Functional Event streams er Immutable
 
 Operasjoner på en event stream returnerer alltid en ny event stream.
  • 8. Event streams En sekvens av events sortert i tid En event stream kan sende ut: • Values • Errors • Completed signal Click Click Click Click X Time CompletedError
  • 9. Event streams Å lytte på en stream kalles subscribing. Fungerer som en asynkron callback akkurat som promises! Observer Design Pattern • Subject: Event Stream • Observer: Subscriber
  • 10. Event streams ALT kan være en stream! 
 DOM events, user input, properties, promises Streams kan være input til andre streams Streams kan filtreres, slås sammen og manipuleres
  • 11. Reactive Extensions (Rx*): 
 .NET, Java, Scala, Clojure, JavaScript, Ruby, Python, C++, Objective-C/Cocoa, Groovy Bacon.js Elm - A functional reactive language Rammeverk (Tools)
  • 12. Bacon.js “Turns your event spaghetti into clean and declarative feng shui bacon"
  • 13. Lage en event stream • $(“#myDiv").asEventStream("click") • Bacon.fromPromise(…) • Bacon.fromEventTarget(..) • Bacon.fromCallback(…), Bacon.fromNodeCallback(…) • Bacon.fromPoll(..) • Bacon.once(…), Bacon.never(), Bacon.later(…) • Bacon.fromArray(…) • Bacon.interval(…) • Bacon.sequentially(…) • Bacon.repeatedly(…) • new Bacon.EventStream()
  • 14. Transformere en event stream ved å bruke funksjoner som map, scan, fold, flatMap
  • 15. Transformere event streams map(f)
 Utfører en funksjon f på alle verdier i en event stream. scan(seed, f)
 Som map(f) men med en seed-verdi som sendes inn i neste funksjon. fold(seed, f) (reduce)
 Som scan, men fold sender bare ut sluttverdien. flatMap(f)
 Som map(f), men hvor f returnerer event streams. flatMap vil flate ut alle event streams returnert fra f og returnere disse som én event stream.
  • 16. Påvirk når verdier kommer ut av en 
 event stream med funksjoner som debounce, debounceImmediate, throttle, bufferingThrottle, delay, slidingWindow
  • 17. Filtrer event streams med funksjoner som filter, take, skip, takeWhile, skipWhile, takeUntil, skipUntil
  • 18. Slå sammen streams med funksjoner som merge, zip og concat
  • 20. Thinking reactive • Alt kan representeres som en stream • Sett sammen og manipuler streams til bedre spesialiserte streams til formålet • GUI-rendring i subscribe-funksjon
  • 21. Demo - click events
  • 22. Demo - drag drop Hva er gjør man når man drar en ting rundt på skjermen? 1. Start: Mouse down 2. Flytt: Mouse move 3. Stop: Mouse up
  • 23. Demo - Konami Code Alle nettsider med respekt for seg selv må ha innebygde easter eggs. ↑ ↑ ↓ ↓ ← → ← → B A
  • 24. Fordeler med FRP • Abstraherer bort implementasjonsdetaljer • Mindre state • Immutable event streams gjør det lett å gjenbruke • Forutsigbar kode
  • 25. Dette høres ut som noe hipster tøys
  • 27. Dette ble litt mye å huske på Koden fra demo https://github.com/olavhaugen/fagkveld-frp The introduction to Reactive Programming 
 you've been missing 
 
 https://gist.github.com/staltz/868e7e9bc2a7b8c1f754
 (Bare google: “Reactive programming”) Bacon.js https://github.com/baconjs/bacon.js/
 http://baconjs.blogspot.fi/ RxMarbles http://rxmarbles.com/