vals

vals

Immutability simplified

iex(5)> list1 = [1,2,3]
[1, 2, 3]
iex(6)> list2 = [4 | list1]
[4, 1, 2, 3]

I am trying to imagine how immutability works in Elixir (and in other functional languages).
When line 6 is executed there will be no new list data structure created but both list1 and list2 will refer to the same underline data structure. Is it correct assumtion???

Marked As Solved

gregvaughn

gregvaughn

Yes. In your example you’ll have a single linked list in memory, but the different variables will point to different head elements of it. Since it is a singly linked list there is no way from the list1 variable to ever go “backwards” and see element “4”.

In a general hand-wavey sense, this is how a lot of data structures work in immutable languages. You basically have one large/combined structure and different variables have different visibility to parts of it.

Also Liked

vals

vals

However, ignoring the under-the-hood implementation, the end user of the language should be under the impression that a brand new copy of the list is created. The particular implementation shown is just a trick for the sake of efficiency.

Joseph

Joseph

I’m not so sure that it’s about copying as much as it is that immutability gives us the effect that it’s like copying. As you say, it’s like that underneath for efficiency, but the interesting thing is that if we have immutable data then we don’t have to copy underneath: we know that pointing to different parts of the data (from different variables) won’t give us a different value at different times.

Where Next?

Popular in Questions Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
Fl4m3Ph03n1x
Background Let’s assume I have a typical GenServer that receives messages as requests, does some operation in a DB and returns responses....
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New

We're in Beta

About us Mission Statement