Book Image

Clojure Reactive Programming

Book Image

Clojure Reactive Programming

Overview of this book

Table of Contents (19 chapters)
Clojure Reactive Programming
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Bibliography
Index

Preface

Highly concurrent applications such as user interfaces have traditionally managed state through the mutation of global variables. Various actions are coordinated via event handlers, which are procedural in nature.

Over time, the complexity of a system increases. New feature requests come in, and it becomes harder and harder to reason about the application.

Functional programming presents itself as an extremely powerful ally in building reliable systems by eliminating mutable states and allowing applications to be written in a declarative and composable way.

Such principles gave rise to Functional Reactive Programming and Compositional Event Systems (CES), programming paradigms that are exceptionally useful in building asynchronous and concurrent applications. They allow you to model mutable states in a functional style.

This book is devoted to these ideas and presents a number of different tools and techniques to help manage the increasing complexity of modern systems.

What this book covers

Chapter 1, What is Reactive Programming?, starts by guiding you through a compelling example of a reactive application written in ClojureScript. It then takes you on a journey through the history of Reactive Programming, during which some important terminology is introduced, setting the tone for the following chapters.

Chapter 2, A Look at Reactive Extensions, explores the basics of this Reactive Programming framework. Its abstractions are introduced and important subjects such as error handling and back pressure are discussed.

Chapter 3, Asynchronous Programming and Networking, walks you through building a stock market application. It starts by using a more traditional approach and then switches to an implementation based on Reactive Extensions, examining the trade-offs between the two.

Chapter 4, Introduction to core.async, describes core.async, a library for asynchronous programming in Clojure. Here, you learn about the building blocks of Communicating Sequential Processes and how Reactive Applications are built with core.async.

Chapter 5, Creating Your Own CES Framework with core.async, embarks on the ambitious endeavor of building a CES framework. It leverages knowledge gained in the previous chapter and uses core.async as the foundation for the framework.

Chapter 6, Building a Simple ClojureScript Game with Reagi, showcases a domain where Reactive frameworks have been used for great effects in games development.

Chapter 7, The UI as a Function, shifts gears and shows how the principles of functional programming can be applied to web UI development through the lens of Om, a ClojureScript binding for Facebook's React.

Chapter 8, Futures, presents futures as a viable alternative to some classes' reactive applications. It examines the limitations of Clojure futures and presents an alternative: imminent, a library of composable futures for Clojure.

Chapter 9, A Reactive API to Amazon Web Services, describes a case study taken from a real project, where a lot of the concepts introduced throughout this book have been put together to interact with a third-party service.

Appendix A, The Algebra of Library Design, introduces concepts from Category Theory that are helpful in building reusable abstractions. The appendix is optional and won't hinder learning in the previous chapters. It presents the principles used in designing the futures library seen in Chapter 8, Futures.

Appendix B, Bibliography, provides all the references used throughout the book.

What you need for this book

This book assumes that you have a reasonably modern desktop or laptop computer as well as a working Clojure environment with leiningen (see http://leiningen.org/) properly configured.

Installation instructions depend on your platform and can be found on the leiningen website (see http://leiningen.org/#install).

You are free to use any text editor of your choice, but popular choices include Eclipse (see https://eclipse.org/downloads/) with the Counterclockwise plugin (see https://github.com/laurentpetit/ccw), IntelliJ (https://www.jetbrains.com/idea/) with the Cursive plugin (see https://cursiveclojure.com/), Light Table (see http://lighttable.com/), Emacs, and Vim.

Who this book is for

This book is for Clojure developers who are currently building or planning to build asynchronous and concurrent applications and who are interested in how they can apply the principles and tools of Reactive Programming to their daily jobs.

Knowledge of Clojure and leiningen—a popular Clojure build tool—is required.

The book also features several ClojureScript examples, and as such, familiarity with ClojureScript and web development in general will be helpful.

Notwithstanding, the chapters have been carefully written in such a way that as long as you possess knowledge of Clojure, following these examples should only require a little extra effort.

As this book progresses, it lays out the building blocks required by later chapters, and as such my recommendation is that you start with Chapter 1, What is Reactive Programming?, and work your way through subsequent chapters in order.

A clear exception to this is Appendix A, The Algebra of Library Design, which is optional and can be read independent of the others—although reading Chapter 8, Futures, might provide a useful background.

Conventions

In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "We can include other contexts through the use of the include directive."

A block of code is set as follows:

(def numbers (atom []))

(defn adder [key ref old-state new-state]
  (print "Current sum is " (reduce + new-state)))

(add-watch numbers :adder adder)

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

(-> (repeat-obs 5)
    (rx/subscribe prn-to-repl))

;; 5
;; 5

Any command-line input or output is written as follows:

lein run -m sin-wave.server

New terms and important words are shown in bold. Words that you see on the screen, in menus, or dialog boxes, for example, appear in the text like this: "If this was a web application our users would be presented with a web server error such as the HTTP code 500 – Internal Server Error."

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title via the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at if you are having a problem with any aspect of this book, and we will do our best to address it.