O'Reilly logo

Clojure for Finance by Timothy Washington

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Understanding Vars and bindings

The previous expressions work. However, there's some assumed knowledge that may be helpful to deconstruct. Firstly, I'm using def to bind a value (lazy sequences, in this case) to a symbol. To be precise, these values are called Vars. Vars are just a way to bind a named location (a symbol) to something else, such as numbers, functions, or in our case, lazy sequences. The notion of binding is important. It allows us to use a value on a per context basis, that is, we have the ability to dynamically rebind Vars in different contexts. So, the following code is possible (taken from http://clojure.org/vars):

  (def ^:dynamic x 1)
  (def ^:dynamic y 1)

  (+ x y)
  ;;=> 2

  (binding [x 2 y 3]
    (+ x y))
  ;;=> 5

  (+ x y)
  ;;=> 2

The root ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required