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

Clojure's model of state and identity

Clojure has an immutable data model. However, this doesn't mean that we can't represent data and change it over time. Direct change or mutation should be avoided at all costs. If necessary, a binding macro will create new var values (or bindings) that are local only to a binding context or scope. These Vars must be marked as ^:dynamic and they should already exist:

(def ^:dynamic a 1)
(def ^:dynamic b 2)

(binding [a 10 b 20] (+ a b))
; => 30

a
; => 1

b
; => 2

It is, however, preferable to choose one of four reference types: Var, Atom, Agent, and Ref. Doing this requires an understanding of Clojure's notion of identity, value, and time. In his presentation Are We There Yet? (you can read more at http://www.infoq.com/presentations/Are-We-There-Yet-Rich-Hickey ...

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