;; `original-numbers` is an identity with the value `(1 2)`
(def original-numbers (1 2))
;; The `conj` function returns a NEW vector.
(def new-numbers (conj original-numbers 3))
original-numbers
;=> (1 2) -- Unchanged original collection
new-numbers
;=> (1 2 3) -- A new collection with the joined values
The function is new here conj. This is Clojure’s universal mechanism for adding items to a collection. It works for lists, sets and maps. In this example we have the existing elements (1, 2) one 3 supplemented.
Due to the immutability principle used in Clojure, this code creates a new one Vector. This is because the original numbers variable is a identity acts. You can use these to create new identities with new values based on them – but you cannot change them.
There is no direct reassignment of variables like in Java in Clojure. Variables are created with an initial value or generated by function results.
parallelism
Concurrency is one of the main goals of Clojure. Immutability helps simplify this. But ultimately there is no way around dealing with shared state, where many threads can access the same memory at the same time. To manage shared states, Clojure includes several powerful concepts.
- Atoms represent a simple mechanism through which multiple threads can safely access a single value. The developer simply defines a normal function, Clojure takes care of the rest in the background (technically this is “Compare-and-Swap” or CAS).
- Refs is a sophisticated, transactional software memory that enables transactions with shared values - including commit and rollback operations as known from databases.
- Futures provide a simple mechanism to offload some of the work to another thread and retrieve the value at a later time.
- Promises are a way to wait for the completion of an asynchronous task running on another thread.
Clojure is suitable for all types of parallel programming, from very simple to highly complex projects. Below is a simple example of how Atoms can be used to process a multi-threaded counter value:
;; Create an atom to hold the counter (initially 0)
(def counter (atom 0))
;; Define a function for one thread's worth of work
;; `swap!` atomically applies the `inc` function to the atom's value.
(defn do-work ()
(dotimes (_ 1000)
(swap! counter inc)))
;; Use `future` to run our work on two separate threads
(def thread-1 (future (do-work)))
(def thread-2 (future (do-work)))
;; Dereferencing them with @
@thread-1
@thread-2
;; Read the final value of the atom
(println "Final count:" @counter)
;=> Final count: 2000
This snippet presents @ represents the syntax to “dereference” threads. This essentially means that they are blocked and waiting for their completion (or that of the futures). The integrated one swap!-Function and the atomvariables work together to enable safe changing of counter state across threads. The swap!– and also the associated one reset!function are tasked with managing thread-safe access to the Atom container. With the futurefunction allows the work to be carried out in a worker thread. (fm)
This article originally appeared at our sister publication Infoworld.com.
