Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concise way to run code 0 to N times in Elixir?

Tags:

elixir

Is there a concise way to run some code 0 to N times in Elixir?

My specific use case is for star ratings in an Eex template. If the rating is 0, output 0 stars; if 1, output 1 star, etc.

List.duplicate/2 and String.duplicate/2 are along the right lines, but don't work well for HTML in an Eex template.

This is what I'm doing now:

<%= for _i <- List.duplicate(true, star_count) do  %>
  <svg...>svg data here</svg>
<% end %>

Is there a better way?

like image 821
Nathan Long Avatar asked Dec 14 '17 16:12

Nathan Long


1 Answers

for i <- 0..n, i > 0, do: ...
like image 139
Aleksei Matiushkin Avatar answered Nov 20 '22 21:11

Aleksei Matiushkin