concept atom in category clojure

This is an excerpt from Manning's book Clojure: The Essential Reference.
atom creates a Clojure Atom, one of the possible Clojure reference types. memoizeuses anatomto store results. Use a custom “atom” whenmemoizeimplementation is too restrictive for a specific kind of caching. You can for example look into something different than a Clojure “hash-map” to store items in the map, like a mutable Java map with soft-references [42]. Keep in mind that there are already libraries like core.cache (github.com/clojure/core.cache) to provide common caching strategies if this is what you’re looking after.
ref, atom and agent are concurrency model designed to handle state, each one providing different guarantees in case of concurrent access. var and volatile! also belong to this group, but they are illustrated in other chapters for their role in scenarios other than pure concurrency.
It’s worth noting that using atom would not replace the need for
delay. In the following example using an atom to store state causes multiple initializations. To see the output from the example, you’ll need to have a service listening on port "61817". You can achieve this using the "netcat" command line utility withnc -l 61817:(import '[java.net InetAddress Socket]) (def connection (atom nil)) ; #1 (defn connect [] ; #2 (swap! connection (fn [conn] (or conn (let [socket (Socket. (InetAddress/getByName "localhost") 61817)] (print "Socket connected to 61817\n") socket))))) (defn handle-request [s] ; #3 (let [conn (connect)] (print (format "Doing something with %s\n" s)))) (dotimes [i 3] ; #4 (future (handle-request i))) (flush) ;; Socket connected to 61817 ;; Socket connected to 61817 ;; Doing something with 1 ;; Doing something with 0 ;; Socket connected to 61817 ;; Doing something with 2When the application starts receiving requests, multiple threads are able to execute
swap!requests concurrently. atom correctly handles concurrency by allowing a few retries until one thread is able to store the socket connection. In doing so, other connections have been created but immediately abandoned, wasting resources.By using
delayinstead of atom, we can achieve the desired effect of initializing the connection once:

This is an excerpt from Manning's book Clojure in Action, Second Edition.
By virtue of the fact that Clojure’s core data structures are all immutable, the issue of shared mutable state becomes largely moot. When some mutable state is required, Clojure provides concurrency data structures called vars, atoms, refs, and agents that have clearly defined semantics for how to change the underlying state they reference. Furthermore, Clojure always allows fast access to the value of these data structures—even if they’re in the middle of being changed by another thread—by maintaining a snapshot of the old values during changes. For use cases that need only parallel execution but not shared state, Clojure provides futures and promises similar to other languages but implemented using JVM threads and not bound to any particular callback as is common in languages like JavaScript.
Atoms are one step more powerful than vars: they’re the simplest way to manage a state change that must be written to and read by multiple threads. The vast majority of the time you’ll be using atoms because you don’t need anything more. But they have two drawbacks. First, multiple atoms cannot be changed together in an atomic and coordinated way. Second, changes to an atom must be free of side effects because its swap! function may be run multiple times. If you need coordination or side effects, you need to use one of the remaining two options.
Refs are atoms with coordination. Instead of using a giant atom, you can split up your state into multiple refs and read and write to several of them atomically inside a dosync transaction. If you have multiple pieces of shared state that must be updated together but rarely all of them in a single transaction, you can reduce the amount of contention in your application and increase concurrency. (This is similar to the tradeoff you’d make when deciding to use a single big lock or multiple smaller locks.) Refs add coordination to atoms, but like atoms all changes to them inside a transaction must be free of side effects because the transaction may be retried.
Creating an atom looks similar to creating either refs or agents:

This is an excerpt from Manning's book The Joy of Clojure, Second Edition.
Lisp has attracted some of the brightest minds in the history of computer science. But an argument from authority is insufficient, so you shouldn’t judge Lisp on this alone. The real value in the Lisp family of languages can be directly observed through the activity of using it to write applications. The Lisp style is one of expressivity, empowerment, and, in many cases, outright beauty. Joy awaits the Lisp neophyte. The original Lisp language, as defined by John McCarthy in his earth-shattering essay “Recursive Functions of Symbolic Expressions and Their Computation by Machine, Part I” (McCarthy 1960), defined the entire language in terms of only seven functions and two special forms: atom, car, cdr, cond, cons, eq, quote, lambda, and label.
Clojure currently provides four different reference types to aide in concurrent programming: refs, agents, atoms, and vars. All but vars are considered shared references and allow for changes to be seen across threads of execution. The most important point to remember about choosing between reference types is that although their features sometimes overlap, each has an ideal use. The reference types and their primary characteristics are shown in figure 10.4.
Just because we said that atoms should be used carefully in transactions, that’s not to say they can never be used that way. In fact, the use of an atom as the reference holding a function’s memoization cache is idempotent on update.
The core memoize function is great for creating simple function caches, but it has some limitations. First, it doesn’t allow for custom caching and expiration strategies. Additionally, memoize doesn’t allow you to manipulate the cache for the purposes of clearing it in part or wholesale. Therefore, let’s create a function manipulable-memoize that lets you get at the cache and perform operations on it directly. Throughout the book, we’ve mentioned Clojure’s metadata facility, and for this example it will come in handy. As the following listing shows, you can take in the function to be memoized and attach some metadata with an atom containing the cache itself for later manipulation.
Listing 10.6. Resettable memoize function
(defn manipulable-memoize [function] (let [cache (atom {})] #1 (with-meta (fn [& args] (or (second (find @cache args)) #2 (let [ret (apply function args)] #3 (swap! cache assoc args ret) #4 ret))) #5 {:cache cache}))) #6 #1 - Store the cache in an atom #2 - Check the cache first … #3 - … else calculate #4 - Store the result #5 - Return the result #6 - Attach metadata !@%STYLE%@! {"css":"{\"css\": \"font-weight: bold;\"}","target":"[]"} !@%STYLE%@!This example slightly modifies the core memoize function to attach the atom to the function being memoized. You can now observe manipulable-memoize in action: