4 ms·
Not exactly what you want, but I've found this snippet useful from time to time: for {item, counter} <- Enum.with_index([:a, :b, :c]) do IO.puts("#{c
by unvs 10y ago
Not exactly what you want, but I've found this snippet useful from time to time:
for {item, counter} <- Enum.with_index([:a, :b, :c]) do
IO.puts("#{counter} -> #{item}")
end
0 -> a
1 -> b
2 -> c
- pdimitar 10y agoThat's actually a perfect example of looping over data structures. Thank you. (Yes, not what the parent poster asked but I still found it useful.)
- shadeless 10y agoOr if you like pipelines as much as I do, you can write it as: [:a, :b, :c] |> Enum.with_index |> Enum.each(fn({item, counter}) -> IO.puts("#{counter} -> #{item}") end)
- sanderjd 10y agoThis is interesting. I'm a huge fan of pipelines like that and I think they're unmatched for being able to compose operations well, but I prefer the non-pipelines version of what is basically just an imperative loop.