for n <- btn,do: content_tag(:button, "#{elem(n, 0)}", class: "#{elem(n, 1)}", onclick: "#{elem(n, 2)}")
Which ended up as a eternal loop of doom
I assume this has something to do with me not directly doing something to n which in my mind should be the tuple.
so I tried Enum.each because it looks like the right tool for the job.
return = []
Enum.each a, fn x -> return++[elem(x,0)] end
I get :Ok from this but not the list I expected [“first”,“second”]
so I am thinking I am missing something really obvious
I don’t think the for itself is causing your loop of doom, it looks fine. A more idiomatic way to write it would be to match the tuple inside the for:
for {name, class, onclick} <- btn do
content_tag(:button, name, class: class, onclick: onclick)
end
What exactly do you mean by “for loop of doom”? What does it return and what are you expecting instead?
This looks like you were used to mutation from other languages, but there is no mutation in elixir, only rebinding.
What you actually want is probably Enum.map(btn, &elem(&1, 0))
Enum.map/2 is used to iterate over a list and return a list where a given function has been applied to each element of the original list, while Enum.each/2 is meant to iterate over a list and using the current element of the list to create a sideeffect and one does not care about the return value at all.
I`m not entirely sure either I have made a note to test and prod a bit later
I put a IO.puts inside the loop to see what happened , It kept on spewing out the text in an eternal loop
I changed the Enum.each to Enum.map removed the array initialization , and it worked. I thought Enum.map was something to do with maps so I instictively skipped it I guess I should read more
yes as a matter of fact I am comparing C# / ASP.net Core to Elixir/Phoenix so I learned basic c# and now I am learning Elixir/Phoenix
I`m not going back to c# anytime soon, Elixir is more fun. documentation is less confusing and if I get this kind of help within minutes of asking questions, its a nobrainer.
yeah I will have a look into it I kinda rushed into removing what did not work and get the working stuff up. it could be because I had dgettext going in my original code.
but I`ll branch and rollback and see what the muffin was going on.