Is there a way to add loop counter to the comprehension?
For example, the comprehension without counter:
for c <- ["a", "b"], do: c # => ["a", "b"] How can I add counter to it? Something like this:
for c <- ["a", "b"], do: {counter, c} # => [{0, "a"}, {1, "b"}] 1 Answer
Use Enum.with_index:
iex(1)> for {value, index} <- Enum.with_index(["a", "b"]), do: {value, index} [{"a", 0}, {"b", 1}]