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"}]
Use Enum.with_index
:
iex(1)> for {c, counter} <- Enum.with_index(["a", "b"]), do: {counter, c} [{0, "a"}, {1, "b"}]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With