Functional Reactive Programming
eBook - ePub

Functional Reactive Programming

  1. 360 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Functional Reactive Programming

About this book

Summary Functional Reactive Programming teaches the concepts and applications of FRP. It offers a careful walk-through of core FRP operations and introduces the concepts and techniques you'll need to use FRP in any language.Purchase of the print book includes a free eBook in PDF, Kindle, and ePub formats from Manning Publications. About the Technology Today's software is shifting to more asynchronous, event-based solutions. For decades, the Observer pattern has been the go-to event infrastructure, but it is known to be bug-prone. Functional reactive programming (FRP) replaces Observer, radically improving the quality of event-based code. About the Book Functional Reactive Programming teaches you how FRP works and how to use it. You'll begin by gaining an understanding of what FRP is and why it's so powerful. Then, you'll work through greenfield and legacy code as you learn to apply FRP to practical use cases. You'll find examples in this book from many application domains using both Java and JavaScript. When you're finished, you'll be able to use the FRP approach in the systems you build and spend less time fixing problems. What's Inside

  • Think differently about data and events
  • FRP techniques for Java and JavaScript
  • Eliminate Observer one listener at a time
  • Explore Sodium, RxJS, and Kefir.js FRP systems


About the Reader Readers need intermediate Java or JavaScript skills. No experience with functional programming or FRP required. About the Authors Stephen Blackheath and Anthony Jones are experienced software developers and the creators of the Sodium FRP library for multiple languages. Foreword by Heinrich Apfelmus. Illustrated by Duncan Hill. Table of Contents

  • Stop listening!
  • Core FRP
  • Some everyday widget stuff
  • Writing a real application
  • New concepts
  • FRP on the web
  • Switch
  • Operational primitives
  • Continuous time
  • Battle of the paradigms
  • Programming in the real world
  • Helpers and patterns
  • Refactoring
  • Adding FRP to existing projects
  • Future directions

Tools to learn more effectively

Saving Books

Saving Books

Keyword Search

Keyword Search

Annotating Text

Annotating Text

Listen to it instead

Listen to it instead

Information

Chapter 1. Stop listening!

This chapter covers
  • What FRP is
  • What events are, and how they cause trouble
  • What FRP is for: the problem we’re trying to solve
  • The benefits of FRP
  • How an FRP system works
  • A different way of thinking that underlies FRP
Welcome to our book! We love functional reactive programming (FRP). Many people like the idea too, yet they aren’t entirely clear what FRP is and what it will do for them. The short answer: it comes in the form of a simple library in a standard programming language, and it replaces listeners (also known as callbacks) in the widely used observer pattern, making your code cleaner, clearer, more robust, and more maintainable—in a word, simpler.
It’s more than this: FRP is a very different way of doing things. It will improve your code and transform your thinking for the better. Yet it’s surprisingly compatible with the usual ways of writing code, so it’s easy to factor into existing projects in stages. This book is about the concepts of FRP as they apply to a range of FRP systems and programming languages.
FRP is based on ideas from functional programming, but this book doesn’t assume any prior knowledge of functional programming. Chapter 1 will lay down some underlying concepts, and in chapter 2 we’ll get into the coding. So stop listening, and start reacting!

1.1. Project, meet complexity wall

It seemed to be going so well. The features weren’t all there yet, but development was swift. The boss was happy, the customers were impressed, the investors were optimistic. The future was bright.
It came out of nowhere ... Software quality crumbled. The speed of development went from treacle to molasses. Before long, there were unhappy customers and late nights. What happened?
Sooner or later, many big projects hit the complexity wall. The complexities in the program that seemed acceptable compound exponentially: At first you hardly notice, and then—BAM! It hits broadside. The project will then typically go one of four ways:
  • It’s shelved.
  • It’s rewritten from scratch, and a million dollars later, it hits the same wall again.
  • The company staffs up. As the team expands, its productivity shambles off into the realm of the eternal quagmire. (Often the company has been acquired around this time.)
  • It undergoes major refactoring, leading eventually to maintainable code.
Refactoring is the only way forward. It’s your primary tool to save a project that has hit the wall, but it’s best used earlier, as part of a development methodology, to prevent disaster before it happens.
But this book isn’t about refactoring. It’s about functional reactive programming (FRP), a programming style that works well with refactoring because it can prevent or repair out-of-control complexity. FRP isn’t a methodology, and—apologies if you bought this book under false pretenses—it won’t solve all of your problems. FRP is a specific programming technique to improve your code in an area that just happens to be a common source of complexity (and therefore bugs): event propagation.
Simple things taking too long
I joined a team that was developing a Java-based configuration tool for an embedded system. The software was difficult to modify to the point where a request for adding a check box to one of the screens was estimated as a two-week job.
This was caused by having to plumb the Boolean value through layers of interfaces and abstraction. To solve this, we put together what we’d later discover was a basic FRP system. Adding a check box was reduced to a one-line change.
We learned that every piece of logic, every listener, and every edge case you need to write code for is a potential source of bugs.

1.2. What is functional reactive programming?

FRP can be viewed from different angles:
  • It’s a replacement for the widely used observer pattern, also known as listeners or callbacks.
  • It’s a composable, modular way to code event-driven logic.
  • It’s a different way of thinking: the program is expressed as a reaction to its inputs, or as a flow of data.
  • It brings order to the management of program state.
  • It’s something fundamental: we think that anyone who tries to solve the problems in the observer pattern will eventually invent FRP.
  • It’s normally implemented as a lightweight software library in a standard programming language.
  • It can be seen as a complete embedded language for stateful logic.
If you’re familiar with the idea of a domain-specific language (DSL), then you can understand FRP as a minimal complete DSL for stateful logic. Aside from the I/O parts, an arbitrarily complex video game (for example) can be written completely in FRP. That’s how powerful and expressive it is. Yet it isn’t all-or-nothing—FRP can be easily introduced into an existing project to any extent you like.

1.2.1. A stricter definition

Conal Elliott is one of the inventors of FRP, and this book is about FRP by his definition. We’ll call this true FRP as a shorthand. What is and isn’t FRP? Here’s part of Elliott’s reply to a Stack Overflow post, ā€œSpecification for a Functional Reactive Programming languageā€ (http://mng.bz/c42s):
I’m glad you’re starting by asking about a specification rather than implementation first. There are a lot of ideas floating around about what FRP is. For me it’s always been two things: (a) denotative and (b) temporally continuous. Many folks drop both of these properties and identify FRP with various implementation notions, all of which are beside the point in my perspective.
By ā€œdenotative,ā€ I mean founded on a precise, simple, implementation-independent, compositional semantics that exactly specifies the meaning of each type and building block. The compositional nature of the semantics then determines the meaning of all type-correct combinations of the building blocks.
A true FRP system has to be specified using denotational semantics.
Definition
Denotational semantics is a mathematical expression of the formal meaning of a programming language. For an FRP system, it provides both a formal specification of the system and a proof that the important property of compositionality ...

Table of contents

  1. Copyright
  2. Brief Table of Contents
  3. Table of Contents
  4. Foreword
  5. Preface
  6. Acknowledgments
  7. About this Book
  8. About the Cover
  9. Chapter 1. Stop listening!
  10. Chapter 2. Core FRP
  11. Chapter 3. Some everyday widget stuff
  12. Chapter 4. Writing a real application
  13. Chapter 5. New concepts
  14. Chapter 6. FRP on the web
  15. Chapter 7. Switch
  16. Chapter 8. Operational primitives
  17. Chapter 9. Continuous time
  18. Chapter 10. Battle of the paradigms
  19. Chapter 11. Programming in the real world
  20. Chapter 12. Helpers and patterns
  21. Chapter 13. Refactoring
  22. Chapter 14. Adding FRP to existing projects
  23. Chapter 15. Future directions
  24. Appendix A. Sodium API
  25. Appendix B. The six plagues of event handling
  26. Appendix C. Comparison of FRP systems
  27. Appendix D. A section for managers
  28. Appendix E. Denotational semantics of Sodium
  29. Index
  30. List of Figures
  31. List of Tables
  32. List of Listings

Frequently asked questions

Yes, you can cancel anytime from the Subscription tab in your account settings on the Perlego website. Your subscription will stay active until the end of your current billing period. Learn how to cancel your subscription
No, books cannot be downloaded as external files, such as PDFs, for use outside of Perlego. However, you can download books within the Perlego app for offline reading on mobile or tablet. Learn how to download books offline
Perlego offers two plans: Essential and Complete
  • Essential is ideal for learners and professionals who enjoy exploring a wide range of subjects. Access the Essential Library with 800,000+ trusted titles and best-sellers across business, personal growth, and the humanities. Includes unlimited reading time and Standard Read Aloud voice.
  • Complete: Perfect for advanced learners and researchers needing full, unrestricted access. Unlock 1.4M+ books across hundreds of subjects, including academic and specialized titles. The Complete Plan also includes advanced features like Premium Read Aloud and Research Assistant.
Both plans are available with monthly, semester, or annual billing cycles.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 990+ topics, we’ve got you covered! Learn about our mission
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more about Read Aloud
Yes! You can use the Perlego app on both iOS and Android devices to read anytime, anywhere — even offline. Perfect for commutes or when you’re on the go.
Please note we cannot support devices running on iOS 13 and Android 7 or earlier. Learn more about using the app
Yes, you can access Functional Reactive Programming by Stephen Blackheath in PDF and/or ePUB format, as well as other popular books in Computer Science & Computer Science General. We have over one million books available in our catalogue for you to explore.