List the list in the built-in Elixir

I'm currently trying to implement a built-in elixir (in my case .html.eex files). I know how to render elixir hashes, but I couldn’t understand how I create content showing all the elements inside the list. In Ruby, it will work as follows:

<% array.each do |item| %> <p> <%= item %> </p> <% end %> 
+7
list elixir
source share
2 answers

Elixir equivalent

 <%= for item <- list do %> <p><%= item %></p> <% end %> 

Note that you need to use <%= before for in Elixir.

+18
source share

I was curious if this is possible using the Enum module, since Patrick Ocity's answer relies on Comprehensions , which look just like a wrapper for the Enum module.

The answer is yes. I tried Enum.each first. Which mysteriously just printed ok on the screen, but what does Enum.each ; it always returns an atom :ok .

I thought Enum.map would be better since it will return a list of results. Take a look:

 <%= Enum.map(@list, fun(item) -> %> <p><%= item %></p> <% end) %> 

EEx works almost the same as ERB . In the ERB example, you passed a “block”, which is similar to a lambda or anonymous function, to each . In my example, EEx fn (item) -> is replaced with do |item| .

So, now you can not only iterate over Lists , but you can experiment with a wider range of functions that use an anonymous function that controls the template.

+2
source share

All Articles