# Immutable Vars and Clojure – DEV Community

Folks typically really feel at a loss after they first take a look at languages with immutability. I do know I did. How will you write a program with out altering the worth of a variable?

## Mutable

To begin with, what does it imply to vary a variable? Think about some easy JavaScript:

``````var x = 5
x = x + 3
``````

This creates a variable referred to as `x` that incorporates the quantity 5. The second line modifications the worth of x to itself plus 3, which implies that it will get modified to the quantity 8.

This works, although the selection of the “equals” register languages like JavaScript is a bit awkward.

If you happen to had been in highschool algebra class and also you noticed:

``````x = x + 3
``````

…you’d most likely throw your arms up in despair. When taking a look at math, we anticipate `x` to be described by a set of situations, the place `=` can be utilized as a part of that description. This assertion cannot be true, since a quantity `x` cannot be equal to itself plus 3. Because of this some languages will not use a easy `=` character when altering a variable. As an illustration, Pascal makes use of `:=`, R makes use of an arrow `<-`, and lots of laptop science texts use an arrow character `←`.

Nonetheless, math gives a strong method to have a look at the world. Anybody with vital expertise debugging a program has seen variables change unexpectedly. When the state of your software is predicated on the values in its variables, then because of this understanding the present state can get very troublesome when these variables change. Maybe the concept of values that do not change could possibly be helpful.

## Immutable

How does this examine to utilizing immutable values?

JavaScript helps you to use immutable values with the `const` declaration. Which means we will not change the worth:

``````const x = 5
x = x + 3
``````

Uncaught TypeError: Project to fixed variable.

As an alternative, now we have to declare a complete new factor to take the brand new worth:

``````const x = 5
const y = x + 3
``````

That is restricted, nevertheless it’s extra versatile than you may anticipate.

You possibly can even re-use the identify `x` in different elements of the code, with out altering the unique worth. That is referred to as shadowing. In JavaScript this should be achieved in a brand new scope:

``````const x = 5
const y = x + 3
{ // a brand new scope
const x = y + 1
console.log("the brand new worth is: " + x)
}
console.log("the outdated worth continues to be: " + x)
``````

the brand new worth is: 9
the outdated worth continues to be: 5

Inside that new scope, the worth of `x` is about to 9, however within the outer scope it stays at 5.

However we by no means print `y` so why not simply skip that and add `x` to itself? Properly, it seems that JavaScript thinks that each one references to `x` within the inside scope are referring to the `x` that is already in that scope, which implies that the declaration of `x` cannot seek advice from itself:

``````const x = 5
{
const x = x + 3 + 1
console.log("the worth is: " + x)
}
``````

Uncaught ReferenceError: “x” just isn’t outlined

JavaScript can use immutable variables, nevertheless it is not fully easy.

## Clojure

In distinction, languages like Haskell, Erlang, Scala and Clojure are designed to make utilizing immutability pure. They typically have some method to permit mutability the place it may possibly assist, however these are usually awkward to make use of, or within the case of Haskell, merely not there.

In Clojure we do not have “variables” anymore. As an alternative, the factor that holds a worth is named a “var”. These could be declared globally with `def`, or typically in a neighborhood scope through `let`.

Like all Lisps, operations like `let` are enclosed in parentheses. The vars to be created seem first, inside an array, with the var instantly earlier than the worth it is going to be set to:

``````(let [x 5]
(println "worth is:" x))
``````

worth is: 5

Shadowing is even simpler than JavaScript, for the reason that outdated worth of `x` shall be used till the brand new worth is completed being outlined:

``````(let [x 5]
(let [x (+ x 3 1)]
(println "The brand new worth is:" x))
(println "This outdated worth continues to be:" x))
``````

The brand new worth is: 9
This outdated worth continues to be: 5

However we do not essentially want that outdated, outer worth. When that occurs, we do not want the separate scopes, and we will `let` the `x` a number of instances:

``````(let [x 5
x (+ x 3)
x (+ x 1)]
(println "worth:" x))
``````

worth: 9

This was made utilizing 3 completely different values for `x`, with every one shadowing the earlier one. It isn’t the way in which I like to put in writing code myself (principally as a result of I prefer to have entry to the varied values of x, and never have them hidden after they get shadowed), however this demonstrates `let` makes shadowing simple.

## Loops

Identical to shadowing, some loop constructs can help you reuse a reputation with a brand new worth every time. As an illustration, we will differ x from 0 to 4 and get the squares:

``````(for [x (range 5)]
(* x x))
``````

(0 1 4 9 16)

Every time going by this `for` assemble, the worth of `x` shall be set to one thing new. However in contrast to JavaScript, it has not modified a variable referred to as `x`. It is a wholly new `x` every time.

In an identical method, we will use a `loop`:

``````(loop [x 0]
(when (< x 5)
(print (* x x))
(recur (+ x 1))))
(println)
``````

0 1 4 9 16

The primary time by the `loop`, the preliminary vector acts like a `let` the place `x` has been set to `0`. Then, every time `recur` is named, it acts like a `let` once more, solely as an alternative of `0`, it would use no matter worth the `recur` was given.

## Constructions

Immutability takes on a brand new that means when utilized to a construction. As an illustration, contemplate an array in JavaScript:

``````const a = [1, 2, 3]
console.log(a)
a.push(4)
console.log(a)
``````

[ 1, 2, 3 ]
[ 1, 2, 3, 4 ]

How did the array `a` change when it was declared `const`? It occurred as a result of the array that `a` continued to level to the identical array, however the array modified what was in it. That is like referring to a closet, however hanging up extra garments in there. The closet does not change, however the contents of the closet do.

Nonetheless, languages like Clojure and Scala additionally provide immutable buildings:

``````(let [a [1 2 3]
a (conj a 4)]
(println a))
``````

[1 2 3 4]

Properly, we will see that we shadowed `a`, however how do we all know if modified the unique or not? Let’s not shadow it, and print the unique object after the additional quantity was added:

``````(let [a1 [1 2 3]
a2 (conj a1 4)]
(println "unique array:" a1)
(println "new array:" a2))
``````

unique array: [1 2 3]
new array: [1 2 3 4]

This works with all of Clojure’s structured varieties. As an illustration, maps:

``````(let [m1 {:one 1
:two 2
:three 3}
m2 (assoc m1 :four 4
:five 5)]
(println "unique map:" m1)
(println "new map:" m2))
``````

unique map: {:one 1, :two 2, :three 3}
new map: {:one 1, :two 2, :three 3, :4 4, :5 5}

A very powerful side of this conduct is that operations that modify a construction return the newly construction, for the reason that unique construction doesn’t change. That is completely different to buildings that mutate. Think about the array in JavaScript:

``````const a = [1, 2, 3]
console.log(a.push(4))
``````

4

This does not return the brand new array. As an alternative, it returns the worth that was added to the array. That is OK, as a result of we will return and test that the brand new array has this added to it. However for immutable buildings, the one time you will get entry to the brand new construction that outcomes from an operation is within the return worth of that operation.

## Immutable Vars with Immutable Constructions

Use a number of the above examples we will construct a construction that’s referred to with immutable vars. As an illustration, a loop can be utilized to construct a vector containing squares:

``````(loop [v []
i 0]
(if (> i 10)
v  ;; i is larger than 10, so return v
(recur (conj v (* i i)) (+ i 1))))
``````

[0 1 4 9 16 25 36 49 64 81 100]

Whereas this works, it is usually a bit clunky. Sometimes, when including plenty of issues to a single object, the proper method to do it in Clojure is to make use of the `scale back` operate. This wants a operate that accepts your construction, and one other argument, and is anticipated to return the brand new construction. As an illustration, the above instance would use a operate that takes a vector and a quantity, then returns a brand new vector that has the sq. of the quantity added. Like:

``````(defn the-function
[v n]
(conj v (* n n)))
``````

We are able to check it out:

``````(the-function [0 1] 2)
``````

[0 1 4]

After which `scale back` can use this for every quantity within the vary 0 to 10 (as much as, however excluding, 11):

``````(scale back
the-function
[]           ;; the beginning construction
(vary 11))  ;; the numbers to course of, from 0 to 10
``````

[0 1 4 9 16 25 36 49 64 81 100]

This does primarily the identical as the unique loop above, however now the operations are extra structured.

## Measurement

An instinct that many individuals develop round that is that the buildings are being copied, after which the copy will get modified. Whereas this could work, a duplicate has the disadvantages of doubling the house consumption of the unique, and requires all the information to be processed, which could possibly be sluggish when a construction will get giant. Making a vector of 1 million gadgets by including a single merchandise at a time would create 1 million intermediate vectors, with a complete of 500,000,500,000 parts between them.

As an alternative, Clojure buildings use “structural sharing”. Which means a brand new construction could be a reference to the information within the earlier construction, and solely include no matter modifications it requires. This protects on each house and processing time. There are even bulk operations internally to make the whole operation extra environment friendly.

This strategy would not work if buildings had been mutable. As an illustration, contemplate a map `m` containing 10 gadgets after which map `n` is created by including a brand new merchandise to `m`. Internally, `n` has a reference to `m` and its single new merchandise, giving it a complete of 11 gadgets. If we had been in a mutable system and we took away one thing from `m`, then `m` can be right down to 9 gadgets. `n` nonetheless incorporates its reference to `m` plus its additional merchandise, so now `n` has additionally been modified, and finally ends up with solely 10 gadgets in whole. Anybody debugging the system could also be confused as to why `n` was modified, as a result of nothing appeared to vary `n`!

However as a result of buildings do not change, then structural sharing works cleanly.

If you happen to actually, actually wish to know the way structural sharing works, I gave a talk on this at :clojureD 2021.

## Wrap Up

This was an preliminary try to explain how programming continues to be doable when variables and information cannot be modified, and to point out how this may be achieved in Clojure.

Clojure makes use of Vars that do not change, fairly than Variables that do. New vars with present names can be utilized, and these will conceal (or shadow) the earlier var with that identify. Usually instances, a chunk of code shall be run many instances with its vars set to completely different values, akin to in a loop, however at no level will an present var be modified inside that block.

Clojure information buildings are additionally immutable, in that their contents by no means change. The impact of a change is finished by creating a brand new construction with the specified change, and the outdated construction is usually deserted (although it can be helpful to maintain the outdated buildings every now and then).

Modified buildings appear to be a duplicate of their predecessor, with a modification. Nonetheless, that is dealt with far more effectively than a duplicate would indicate.

## Afterword

Immutability is one thing that stops individuals after they first come to purposeful languages, so I wish to work on explaining it for individuals making the shift. Personally, I discovered about it after I first picked up the Scala language, so none of that is distinctive to Clojure.

That is my first try at explaining this matter, so I am anticipating that I have not achieved an important job of it. I’d admire suggestions on the elements that work and the elements which are complicated. In the meantime, if I left you feeling bewildered, attempt studying the identical matter in “Programming in Scala” by Odersky, Spoon, Venners and Sommers.