Professional Documents
Culture Documents
Prepared By
Ms.Himabindu Sri
Ms.Afreen Fatima Mohammed
Ms.Deepika
Clojure
Unit – No-5
Objectives
•Importance of Clojure
•Clojure-an evolution of LISP
•Working of Read Eval Print Loop- Clojure’s interactive programming environment
•Know Keyword, Vectors, list, Macros and Syntax quote
•Identity, Value and State in Clojure
•Concurrent Programming in Clojure
•Know the Concurrent programming Problem and design solution
•Understand atomic power, working of Agent and Ref
Importance of Clojure
• Designed for concurrent programming
• Performance is closer to Java than to Python
• Access to Java from Clojure, and Clojure from Java, is easy, wrapper-free, and fast
• A Lisp dialect
• Dynamically typed
Getting Clojure: Why Lisp Still Matter
• To turn-off Evaluation
Keywords
• Are prepended with a colon
Vectors
• Created by placing the elements of the vector inside of square brackets
• Create a vector and name it some-keywords
• Evaluating funky-looking-list
Macros
• Special kind of function
• Takes a piece of data that represents code known as a form
• Transforms one form into another before Clojure’s compiler compiles it
• Does not evaluate its arguments
• Macro function make-printer is created
•By changing name and adress, old name and address would be lost
Clojure Model
• Defines Value, Identity and State
• Value
An immutable piece of data, or a composite of immutable pieces of data
• Identity
An entity that’s associated with a series of values over time
• State
Is just the value of an identity at a point in time
Persistent Data Structures
• If the keys have values existing in the map, they are overwritten
Managed Reference Types
• Is between an immutable data structure and the references to it and manage state
changes
• All of the references being managed point at the latest value for a given identity
• To see a value, the reference that points to it must be de-referenced
• De-referencing returns the state that the reference refers to, which captures it at
the point in time it was de-referenced
Types of managed references
• Atoms
Manage independent state synchronously
• Agents
• Refs
Atom
• The swap! function takes an atom, a function to call on the value it refers to, and
additional arguments for the function
• The swap! function then runs the passed-in function on the value of the atom and
the additional arguments, returning the new value
• Create an add-to-int-atom, which adds an integer to the atom
• Use add-to-int-atom to add the number 42 to the number that the atom refers to
Output:
Solution
• Use Java’s locking mechanisms to protect the counter variable to ensure that only
one thread can access the counter at a time, and that any modifications made to it
are visible when the lock is relinquished
• Another higher-level solution is the one that we demonstrated using AtomicInteger
• The AtomicInteger class provides an incrementAndGet, which atomically
increments an internal counter, ensuring that no thread can see it in an inconsistent
state
• Two main downsides to the AtomicInteger solution are that it can only be used for
integers, and it can only deal with coordinating changes to a single object
Clojure’s Solution
• Use Clojure’s immutable data structures, managed reference types, and software
transactional memory system combine to provide a model that’s both high level
and much easier to use than locking concurrency
Atomic Power
• Atoms are good for managing state that’s independent and to which we want to
make synchronous change
• The swap! function applies the passed in function to the value wrapped by the
atom, swaps the old value for the new one, and returns it
• Create a simple atom counter, and a function to increment it