- Clojure Tutorial
- Clojure - Home
- Clojure - Overview
- Clojure - Environment
- Clojure - Basic Syntax
- Clojure - REPL
- Clojure - Data Types
- Clojure - Variables
- Clojure - Operators
- Clojure - Loops
- Clojure - Decision Making
- Clojure - Functions
- Clojure - Numbers
- Clojure - Recursion
- Clojure - File I/O
- Clojure - Strings
- Clojure - Lists
- Clojure - Sets
- Clojure - Vectors
- Clojure - Maps
- Clojure - Namespaces
- Clojure - Exception Handling
- Clojure - Sequences
- Clojure - Regular Expressions
- Clojure - Predicates
- Clojure - Destructuring
- Clojure - Date & Time
- Clojure - Atoms
- Clojure - Metadata
- Clojure - StructMaps
- Clojure - Agents
- Clojure - Watchers
- Clojure - Macros
- Clojure - Reference Values
- Clojure - Databases
- Clojure - Java Interface
- Clojure - Concurrent Programming
- Clojure - Applications
- Clojure - Automated Testing
- Clojure - Libraries
- Clojure Useful Resources
- Clojure - Quick Guide
- Clojure - Useful Resources
- Clojure - Discussion
Clojure - Immutable Nature
By default structures are also immutable, so if we try to change the value of a particular key, it will not change.
Example
An example of how this happens is shown in the following program.
(ns clojure.examples.example (:gen-class)) (defn Example [] (defstruct Employee :EmployeeName :Employeeid) (def emp (struct-map Employee :EmployeeName "John" :Employeeid 1)) (println (:EmployeeName emp)) (assoc emp :EmployeeName "Mark") (println (:EmployeeName emp))) (Example)
In the above example, we try to use the ‘assoc’ function to associate a new value for the Employee Name in the structure.
Output
The above program produces the following output.
John John
This clearly shows that the structure is immutable. The only way to change the value is to create a new variable with the changed value as shown in the following program.
Example
(ns clojure.examples.example (:gen-class)) (defn Example [] (defstruct Employee :EmployeeName :Employeeid) (def emp (struct-map Employee :EmployeeName "John" :Employeeid 1)) (def newemp (assoc emp :EmployeeName "Mark")) (println newemp)) (Example)
Output
The above program produces the following output.
{:EmployeeName Mark, :Employeeid 1}
clojure_structmaps.htm
Advertisements