Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Clojure Reactive Programming

You're reading from   Clojure Reactive Programming Design and implement highly reusable reactive applications by integrating different frameworks with Clojure

Arrow left icon
Product type Paperback
Published in Mar 2015
Publisher Packt
ISBN-13 9781783986668
Length 232 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Leonardo Borges Leonardo Borges
Author Profile Icon Leonardo Borges
Leonardo Borges
Arrow right icon
View More author details
Toc

Table of Contents (13) Chapters Close

Preface 1. What is Reactive Programming? 2. A Look at Reactive Extensions FREE CHAPTER 3. Asynchronous Programming and Networking 4. Introduction to core.async 5. Creating Your Own CES Framework with core.async 6. Building a Simple ClojureScript Game with Reagi 7. The UI as a Function 8. Futures 9. A Reactive API to Amazon Web Services A. The Algebra of Library Design B. Bibliography
Index

Chapter 1. What is Reactive Programming?

Reactive Programming is both an overloaded term and a broad topic. As such, this book will focus on a specific formulation of Reactive Programming called Compositional Event Systems (CES).

Before covering some history and background behind Reactive Programming and CES, I would like to open with a working and hopefully compelling example: an animation in which we draw a sine wave onto a web page.

The sine wave is simply the graph representation of the sine function. It is a smooth, repetitive oscillation, and at the end of our animation it will look like the following screenshot:

What is Reactive Programming?

This example will highlight how CES:

  • Urges us to think about what we would like to do as opposed to how
  • Encourages small, specific abstractions that can be composed together
  • Produces terse and maintainable code that is easy to change

The core of this program boils down to four lines of ClojureScript:

(-> sine-wave
    (.take 600)
    (.subscribe (fn [{:keys [x y]}]
                  (fill-rect x y "orange"))))

Simply by looking at this code it is impossible to determine precisely what it does. However, do take the time to read and imagine what it could do.

First, we have a variable called sine-wave, which represents the 2D coordinates we will draw onto the web page. The next line gives us the intuition that sine-wave is some sort of collection-like abstraction: we use .take to retrieve 600 coordinates from it.

Finally, we .subscribe to this "collection" by passing it a callback. This callback will be called for each item in the sine-wave, finally drawing at the given x and y coordinates using the fill-rect function.

This is quite a bit to take in for now as we haven't seen any other code yet—but that was the point of this little exercise: even though we know nothing about the specifics of this example, we are able to develop an intuition of how it might work.

Let's see what else is necessary to make this snippet animate a sine wave on our screen.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime