Book Image

Hands-On Reactive Programming with Clojure - Second Edition

By : Konrad Szydlo, Leonardo Borges
Book Image

Hands-On Reactive Programming with Clojure - Second Edition

By: Konrad Szydlo, Leonardo Borges

Overview of this book

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.
Table of Contents (15 chapters)

Clojure futures

The first step toward fixing this issue—that is, to prevent a potentially long-running task from blocking our application—is to create new threads, which do the work and wait for it to complete. This way, we keep the application's main thread free to serve more clients.

Working directly with threads, however, is tedious and error-prone, so Clojure's core library includes futures, which are extremely simple to use:

(def f (clojure.core/future 
         (println "doing some expensive work...") 
         (Thread/sleep 5000) 
         (println "done") 
         10)) 
(println "You'll see me before the future finishes") 
;; doing some expensive work... 
;; You'll see me before the future finishes 
;; done 

In the preceding snippet, we invoked the clojure.core/future macro with a body simulating an expensive...