Hands-On Reactive Programming with Clojure - Second Edition

Book description

Learn how to use RxClojure to deal with stateful computations

Key Features

  • Leverage the features of Functional Reactive Programming using Clojure
  • Create dataflow-based systems that are the building blocks of Reactive Programming
  • Use different Functional Reactive Programming frameworks, techniques, and patterns to solve real-world problems

Book Description

Reactive Programming is central to many concurrent systems, and can help make the process of developing highly concurrent, event-driven, and asynchronous applications simpler and less error-prone.

This book will allow you to explore Reactive Programming in Clojure 1.9 and help you get to grips with some of its new features such as transducers, reader conditionals, additional string functions, direct linking, and socket servers. Hands-On Reactive Programming with Clojure starts by introducing you to Functional Reactive Programming (FRP) and its formulations, as well as showing you how it inspired Compositional Event Systems (CES). It then guides you in understanding Reactive Programming as well as learning how to develop your ability to work with time-varying values thanks to examples of reactive applications implemented in different frameworks. You'll also gain insight into some interesting Reactive design patterns such as the simple component, circuit breaker, request-response, and multiple-master replication. Finally, the book introduces microservices-based architecture in Clojure and closes with examples of unit testing frameworks.

By the end of the book, you will have gained all the knowledge you need to create applications using different Reactive Programming approaches.

What you will learn

  • Understand how to think in terms of time-varying values and event streams
  • Create, compose, and transform observable sequences using Reactive extensions
  • Build a CES framework from scratch using core.async as its foundation
  • Develop a simple ClojureScript game using Reagi
  • Integrate Om and RxJS in a web application
  • Implement a reactive API in Amazon Web Services (AWS)
  • Discover helpful approaches to backpressure and error handling
  • Get to grips with futures and their applications

Who this book is for

If you're interested in using Reactive Programming to build asynchronous and concurrent applications, this is the book for you. Basic knowledge of Clojure programming is necessary to understand the concepts covered in this book.

Table of contents

  1. Title Page
  2. Copyright and Credits
    1. Hands-On Reactive Programming with Clojure Second Edition
  3. About Packt
    1. Why subscribe?
    2. Packt.com
  4. Contributors
    1. About the authors
    2. About the reviewer
    3. Packt is searching for authors like you
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Download the color images
      3. Conventions used
    4. Get in touch
      1. Reviews
  6. What is Reactive Programming?
    1. A taste of Reactive Programming
      1. The concept of time in RxJS
      2. More colors
      3. Making it reactive
      4. Exercise 1.1
    2. A bit of history
      1. Dataflow programming
      2. Object-oriented Reactive Programming
      3. The most widely used reactive program
      4. The Observer design pattern
      5. Functional Reactive Programming
      6. Higher-order FRP
    3. Signals and events
    4. Implementation challenges
      1. First-order FRP
      2. Asynchronous data flow
      3. Arrowized FRP
    5. Applications of FRP
      1. Asynchronous programming and networking
      2. Complex GUIs and animations
    6. Summary
    7. Further reading
  7. A Look at Reactive Extensions
    1. The observer pattern revisited
      1. Observer – an iterator's dual
    2. Creating observables
      1. Custom observables
    3. Manipulating observables
    4. flatmap and friends
      1. One more flatmap for the road
    5. Error handling
      1. OnError
      2. Catch
      3. Retry
    6. Backpressure
      1. Sample
      2. Backpressure strategies
    7. Summary
    8. Further reading
  8. Asynchronous Programming and Networking
    1. Building a stock market monitoring application
    2. Rolling averages
    3. Identifying problems with our current approach
    4. Removing incidental complexity with RxClojure
      1. Observable rolling averages
    5. Summary
  9. Introduction to core.async
    1. Asynchronous programming and concurrency
    2. The core.async library
      1. The Communicating Sequential Processes paper
    3. Rewriting the stock market application with core.async
      1. Implementing the application code
    4. Error handling
    5. Backpressure
      1. Fixed buffer
      2. Dropping buffer
      3. Sliding buffer
    6. Transducers
      1. Transducers and core.async
    7. Summary
    8. Further reading
  10. Creating Your Own CES Framework with core.async
    1. A minimal CES framework
      1. Clojure or ClojureScript?
      2. Designing the public API
      3. Implementing tokens
      4. Implementing event streams
      5. Implementing behaviors
    2. Exercises
      1. Exercise 5.1
      2. Exercise 5.2
    3. A respondent application
    4. CES versus core.async
    5. Summary
    6. Further reading
  11. Building a Simple ClojureScript Game with Reagi
    1. Setting up the project
      1. Game entities
      2. Putting it all together
      3. Modeling user input as event streams
      4. Working with the active keys stream
    2. Reagi and other CES frameworks
    3. Summary
    4. Further reading
  12. The UI as a Function
    1. The problem with complex web UIs
    2. Enter React.js
      1. Lessons from functional programming
    3. ClojureScript and Om
    4. Building a simple Contacts application with Om
      1. Setting up the Contacts project
      2. Application components
      3. Managing application state
      4. Om components
      5. Query expressions
      6. Filling in the blanks
      7. Identity
    5. Intercomponent communication
      1. Creating an agile board with Om
      2. The board state
      3. Components overview
      4. Life cycle and component local state
      5. Remaining components
      6. Utility functions
      7. Application's parser
    6. Exercises
    7. Summary
    8. Further reading
  13. A New Approach to Futures
    1. Clojure futures
    2. Fetching data in parallel
    3. Imminent – a composable futures library for Clojure
      1. Creating futures
      2. Combinators and event handlers
    4. The movies example revisited
    5. Futures and blocking IO
    6. Summary
    7. Further reading
  14. A Reactive API to Amazon Web Services
    1. Amazon Services use case
    2. Infrastructure automation
    3. AWS resources dashboard
    4. CloudFormation
      1. The describeStacks endpoint
      2. The describeStackResources endpoint
    5. Elastic Compute Cloud Service
      1. The describeInstances endpoint
    6. Relational Database Service
      1. The describeDBInstances endpoint
    7. Designing the solution
      1. Running the AWS stub server
      2. Setting up the dashboard project
      3. Creating AWS Observables
      4. Combining the AWS Observables
      5. Putting it all together
    8. Exercises
    9. Summary
    10. Further reading
  15. Reactive Microservices
    1. An introduction to microservices
      1. Advantages of microservices
        1. Scalability
      2. Disadvantages of microservices
      3. When to use microservices
    2. Microservices best practices
      1. Domain Driven Design
      2. Separate data storage
      3. Team building and structure
      4. Deployment
      5. Failure isolation
      6. Cross-cutting concerns
    3. Communication between services
      1. Event-driven communication
      2. The CQRS communication pattern
    4. Common pitfalls
      1. Attitudes towards microservices
      2. Service boundaries
      3. Service granularity
      4. Service separation
        1. Build dependencies
        2. Services contracts
        3. Services communication
      5. DevOps
    5. RESTful microservices in action
      1. Application overview
      2. Services messaging
      3. A Clojure RESTful service
        1. Setting up the project structure
        2. Database operations with Toucan
        3. Coercion with the schema
        4. The Card API
          1. Compojure HTTP API
          2. The GET Card API
          3. The GET Cards API
          4. The UPDATE Card API
          5. The DELETE Card API
        5. The Swagger UI
    6. Summary
    7. Further reading
  16. Testing Reactive Apps
    1. Why testing is important
    2. Testing approaches
      1. Functional testing
      2. Non-functional testing
    3. Clojure unit testing frameworks
      1. The clojure.test framework
        1. The is macro
        2. The are macro
      2. The expectations framework
      3. The Midje framework
        1. The fact macro
        2. The facts macro
        3. Top-down testing
        4. Checking for exceptions
        5. Checking collections
          1. The contains macro
          2. The just macro
      4. The Speclj framework
        1. The should-contain macro
        2. The should= and should== macros
        3. The should-throw macro
        4. The should-be-a macro
    4. Summary
    5. Further reading
  17. Concurrency Utilities in Clojure
    1. Introduction to concurrency
      1. Concurrency or parallelism
    2. State, identity, and value
      1. Common problems with states
        1. Lost updates
        2. Unrepeatable reads
        3. Phantom reads
      2. The traditional solution to the problem with states
        1. Deadlock
        2. Starvation
        3. Livelock
        4. Race conditions
        5. Other problems with locks
    3. The Clojure approach to concurrency
      1. Classifying constructs
        1. Synchronization
        2. Coordination
      2. Atoms
        1. Dereferencing atoms
        2. Mutating atoms
          1. reset!
          2. swap!
          3. compare-and-set!
      3. Agents
        1. Dereferencing agents
        2. Mutating agents
          1. send
          2. send-off
      4. Refs
        1. Dereferencing refs
        2. Mutating refs
          1. Software Transactional Memory
          2. ref-set
          3. alter
          4. commute
      5. Vars
        1. Var bindings
          1. Private vars
          2. Constant vars
    4. Futures and promises in Clojure
      1. Futures
      2. Promises
    5. Summary
    6. Further reading
  18. Appendix - The Algebra of Library Design
    1. The semantics of map
      1. Functors
      2. The option functor
    2. Finding the average of ages
    3. Applicative functors
    4. Gathering stats about ages
    5. Monads
    6. Summary
    7. Further reading
  19. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: Hands-On Reactive Programming with Clojure - Second Edition
  • Author(s): Konrad Szydlo, Leonardo Borges
  • Release date: January 2019
  • Publisher(s): Packt Publishing
  • ISBN: 9781789346138