The Pragmatic Programmer by Dave Thomas Andy Hunt
Author:Dave Thomas, Andy Hunt
Language: eng
Format: epub
Tags: Pragmatic Bookshelf
Publisher: The Pragmatic Bookshelf, LLC
Reactive Programming, Streams, and Events
If youâve ever used a spreadsheet, then youâll be familiar with reactive programming. If a cell contains a formula which refers to a second cell, then updating that second cell causes the first to update as well. The values react as the values they use change.
There are many frameworks that can help with this kind of data-level reactivity: in the realm of the browser React and Vue.js are current favorites (but, this being JavaScript, this information will be out-of-date before this book is even printed).
Itâs clear that events can also be used to trigger reactions in code, but it isnât necessarily easy to plumb them in. Thatâs where streams come in.
Streams let us treat events as if they were a collection of data. Itâs as if we had a list of events, which got longer when new events arrive. The beauty of that is that we can treat streams just like any other collection: we can manipulate, combine, filter, and do all the other data-ish things we know so well. We can even combine event streams and regular collections. And streams can be asynchronous, which means your code gets the opportunity to respond to events as they arrive.
The current de facto baseline for reactive event handling is defined on the site http://reactivex.io, which defines a language-agnostic set of principles and documents some common implementations. Here weâll use the RxJs library for JavaScript.
Our first example takes two streams and zips them together: the result is a new stream where each element contains one item from the first input stream and one item from the other. In this case, the first stream is simply a list of five animal names. The second stream is more interesting: itâs an interval timer which generates an event every 500ms. Because the streams are zipped together, a result is only generated when data is available on both, and so our result stream only emits a value every half second:
event/rx0/index.js
â âimportâ * âasâ Observable âfromâ â'rxjs'â
â âimportâ { logValues } âfromâ â"../rxcommon/logger.js"â
â
â âletâ animals = Observable.âofâ(â"ant"â, â"bee"â, â"cat"â, â"dog"â, â"elk"â)
â âletâ ticker = Observable.interval(500)
â
â âletâ combined = Observable.zip(animals, ticker)
â
â combined.subscribe(next => logValues(JSON.stringify(next)))
This code uses a simple logging function[40] which adds items to a list in the browser window. Each item is timestamped with the time in milliseconds since the program started to run. Hereâs what it shows for our code:
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
AI & Machine Learning | Bioinformatics |
Computer Simulation | Cybernetics |
Human-Computer Interaction | Information Theory |
Robotics | Systems Analysis & Design |
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8301)
Test-Driven Development with Java by Alan Mellor(6731)
Data Augmentation with Python by Duc Haba(6644)
Principles of Data Fabric by Sonia Mezzetta(6396)
Learn Blender Simulations the Right Way by Stephen Pearson(6295)
Microservices with Spring Boot 3 and Spring Cloud by Magnus Larsson(6167)
Hadoop in Practice by Alex Holmes(5959)
Jquery UI in Action : Master the concepts Of Jquery UI: A Step By Step Approach by ANMOL GOYAL(5807)
RPA Solution Architect's Handbook by Sachin Sahgal(5563)
Big Data Analysis with Python by Ivan Marin(5368)
The Infinite Retina by Robert Scoble Irena Cronin(5253)
Life 3.0: Being Human in the Age of Artificial Intelligence by Tegmark Max(5148)
Pretrain Vision and Large Language Models in Python by Emily Webber(4331)
Infrastructure as Code for Beginners by Russ McKendrick(4092)
Functional Programming in JavaScript by Mantyla Dan(4038)
The Age of Surveillance Capitalism by Shoshana Zuboff(3955)
WordPress Plugin Development Cookbook by Yannick Lefebvre(3806)
Embracing Microservices Design by Ovais Mehboob Ahmed Khan Nabil Siddiqui and Timothy Oleson(3610)
Applied Machine Learning for Healthcare and Life Sciences Using AWS by Ujjwal Ratan(3581)
