lc0815

lc0815

what does something like this do
(t ++ [d])

and why use
[d] instead of d

and finally, why the parentheses…

unfortunately the code is not documented as it seems that if you cain't read ELIXIR, you cain.t read.... so documentation ain't needed.

thank you for your help.

Showing Posts 1 to 10

t0t0

t0t0

Hi,

it is a merge of 2 lists: you append the list [d] (a list of one element, d) to t which is a list. There are several ways to write the same thing (for example: List.insert_at(t, -1, d))

why the parentheses…

Can’t say without the rest of the code. To avoid a conflict (precedence) with an other operator?

lc0815

lc0815 OP

if t and d are lists then why use [d] instead of d since t does not have the brackets [[]
???

t0t0

t0t0

Kernel.++/2 only works with lists as arguments.

t ++ d won’t work (not exactly, you’ll end with an improper list)

EDIT:

Yeah, I was editing my post at the same time and hesitate if, for the PO, this “detail” would be confusing and worths mentioning it.

dorgan

dorgan

d needs to be wrapped in a list if it’s a single element, otherwise you’ll end up with an improper list [1, 2, 3 | d]

I go over the fine details here: Making sense of Elixir (improper) lists (dorgan.ar). The tl;dr is that is has to do with the way lists are constructed. [1, 2, 3] is actually [1 | [2 | [3 | []]]], and all the ++ operator does is replace the last [] with whatever you have at the right side of the operator. Because the tail of a list needs to be a list or an empty list to be a proper list, you need to wrap a non-list element in a list to not break the data structure.

That’s partially correct. It only checks that the left side is a list, the right side can be whatever. Try doing [] ++ :wut for a nice surprise.

lc0815

lc0815 OP

this is the complete function:

defp add_when_in(t, k, l, d) do

if k in l do
  (t ++ [d])
else
  t
end

end

lc0815

lc0815 OP

Thank you for all of your answers, it’s amazing how a simple list can complicate matters depending on how it’s used. in my case it was the naming convention or the lack of it that threw me for a loop.

Thanks guys not only for clear and prompt description but for being there.

Now I can apply this new found knowledge to refactor the code and move on… :sunny:

t0t0

t0t0

You were right, parentheses are not required here.

lc0815

lc0815 OP

when testing the [] ++ :wut I get:

LC-210722 inside add_when_in with ([] ++ :wut) lists

LC-210712 my data: :wut

so now, what does this mean?

Thanks again

PS: I am reading the article: Making sense of Elixir (improper) lists (dorgan.ar).

dorgan

dorgan

The ++ operator looks for the last [] in the list and replaces it with the value at the right side without checking if it’s also a list. So if you give it [] at the left, it will “replace” it with the value at the right, so you get the right side value and no list.

lc0815

lc0815 OP

humm, food for thought, will play with this a bit more…

thanks again,

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews