🤖

From Functional to Reactive Programming

Jun 27, 2024

From Functional to Reactive Programming

Introduction

  • Speaker shares their journey learning reactive programming.
  • Initial struggle with understanding reactive programming.
  • Emphasis on the necessity of understanding paradigms before learning syntax or APIs.
  • Six paradigm shifts experienced by the speaker:
    1. Procedural Programming
    2. Object-Oriented Programming (OOP)
    3. Distributed Computing (COM, CORBA)
    4. Asynchronous Programming (Node.js)
    5. Functional Programming
    6. Reactive Programming

Reactive Programming Overview

  • Speaker questions the need for a new programming model.
  • Reactive programming described as "functional programming plus plus".
  • Analogy: Understanding reactive programming and building on functional programming knowledge.

Functional Programming Recap

  • Key Concepts: Functional Programming:
    • Immutability
    • Higher-Order Functions
      • Pass a function to a function
      • Create a function within a function
      • Return a function from a function
    • Declarative Style
    • Functional Composition (Pipeline)
    • Lazy Evaluation

Comparison: Lists vs Streams

  • Lists: Container for data (like a bucket).
  • Streams: Pipeline for data flow (like a pipeline).
  • Difference between data structure (list) and system of functions (stream).
  • Example of data transformation using streams to achieve functional composition and lazy evaluation.

Efficiency in Functional Programming

  • Importance of lazy evaluation for efficiency.
  • Avoid unnecessary computations to achieve efficiency, not just faster computation.
  • Example of a stream only processing data when a terminal operator like findFirst() is invoked.

Introduction to Reactive Programming

  • Explains reactive programming using the current technological context (Internet of Things, high-volume, high-frequency data).
  • Historical Examples of reactive applications:
    • Microsoft Excel (reactive cells update instantly)
    • Google Docs (real-time collaboration)
  • Reactive programming geared towards handling modern demands (e.g., real-time data, scalable and responsive systems).

Characteristics of Reactive Programming

  • Key Features:
    • Scale: Handling high volume and frequency of data.
    • Elasticity: Expand and shrink resources as needed to manage load.
    • Message-Driven: Use messaging systems to handle data exchange without central databases (e.g., Kafka streams).
    • Responsiveness: Applications provide instantaneous feedback (e.g., infinite scrolling).
    • Resilience: Gracefully handling failures treating them as a normal part of operations.

Implementing Reactive Programming

  • Reactive streams vs Java streams:
    • Stream pipelines with lazy evaluation in both.
    • Reactive programming includes three channels: Data, Error, and Complete.
    • Errors handled via Error Channel, avoiding disruptive stack unwinding.
  • Key Differences:
    • Ordinary Java streams convey only data.
    • Reactive streams can also convey errors and completion signals.
  • Example: Implementation of a reactive stream with Flowable and handling of backpressure.

Additional Concepts in Reactive Programming

  • Incorporates backpressure: Managing producer and consumer speeds.
  • Asynchronous vs Synchronous: Deciding how data is processed according to needs.

Conclusion

  • Reactive programming is an evolution of functional programming with additional capabilities for handling modern application needs.
  • Emphasizes scalability, responsiveness, elasticity, and resilience.
  • Endnote on the importance of merging concepts to build efficient systems.