Immutability: Why is it important?

Why is it important? Watch this video to get the answer:

In Object oriented programming the state of the application is shared, if you have several functions trying to change a data value at the same time it might lead to unexpected errors because the value they thought they would receive is changed by other processes. (Thanks to mutable data)

When we update a value in OOP we usually do something like this:

a = [1]
a.append(2)

This would update the original list, which is the opposite of what happens in elixir. In elixir each time we “change” a data value what’s really happening is we’re creating a new data value by copying the old one and then creating a new data structure, the original remaining intact.

What this means is that we can have several programs depending on the same data structure and be certain that none of them will affect the others because they can’t even change the original value!

Conventional languages are adding functionality inspired by functional languages but still it’s hard to take full advantage of them when you’re having the imperative paradigm (mutable state) clashing with the functional paradigm in the same language, for example to do concurrency they use locks so that only one process can modify a data value at a time.

In elixir all data is immutable. Doesn’t matter how complex or simple the data structure is, the data is always immutable and that’s perfect for parallel processes.

Remember: transforming data (copying it and making a new data structure) is different from modifying the data(taking the existing data structure and changing it)

Elixir reminds us of this with its syntax, instead of modifying objects like:

Carlos.age = 23

we transform data like:

Time.get_age(1996, current_year=2019)

and get 23 as a result.

References

Programming Elixir 1.6 Part 1, Section 3: Immutability

Learn Functional Programming with Elixir, Chapter 1: Thinking Functionally

Concurrency and parallel processes

In this post I’m going to explain what is concurrency, parallelism and their differences.

First watch this video:

You can think of concurrency as two things that exist at the same time. For example when we use our computer we have different programs open, we might have chrome, and excel. So you can switch back and forth between them, because using one doesn’t mean we have to exit from other programs. That is concurrency, two or more things existing at the same time frame.

So if you think about it, concurrency is needed to do parallel processing, you need two processes to exist at the same time if you want to run them on parallel. (I exist, therefore I run? Bad joke I know..)

For instance when we leave music running in a chrome tab and then do something else in another, the tabs are concurrent with each other, they both exist simultaneously but we can only interact with one of them at any time since computers do not allow having multiple mouses running in parallel.(If you connect a second mouse to your PC, you don’t see a second mouse popping up in your screen)

If you switch super fast back and forth between concurrent processes you can give the user the illusion that the processes are running in parallel, which is what computer did before having multi-core processors.

You may be interested in:

Understanding Concurrency and Parallelism in Elixir