Complex loop in EEx

Hello, I’d try to simplify it by moving the function to the view module, using string interpolation, i.e. (disclaimer: untested code ahead..)

def eval_item(x, index) do
  case x do
    {k, v} -> 
      "#{k} #{v} : @index: #{index}"
    val ->
      "#{val} : @index: #{index}"
    end
end

or with two functions using pattern matching on arguments (that I prefer as I find it easier to read):

def eval_item({k, v} = x, index), do: "#{k} #{v} : @index: #{index}"

def eval_item(x, index), do: "#{x} : @index: #{index}"

then in the template you could just:

<%= Enum.with_index(list) |> Enum.map(&eval_item/2) %> 

hth.