TL;DR
In the previous post, we explained the recursion concept in Elixir Lists. Today we discuss Elixir Immutability concept. This post is part of the functional language series, and it is based on the remarkable book Elixir In Action by Sasa Juric.
Immutability
Immutability in functional programming is a straightforward concept. The function always returns the result of the calculation as a new copy of data. For example, in Python, the function could operate on the same memory location, with no needed copies. Developers must take care of data update side effects, which could cause nasty side effects, especially in concurrent programming.
So in Elixir, data that are the result of function calculation should be either rebound to an existing variable, or to a new variable or stored in a database or file system.
Benefits
Each function is a data transformation, and in Elixir, we pipe several data transformations in the chain using the pipe |>
operator. Data functions could work concurrently without possible deadlock or sharing issues because each function works on a dedicated copy of data.
Remember
- immutability
- shallow copy