Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a shortcut in Elixir similar to Python's enumerate?

Tags:

elixir

In Python:

>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]

It basically takes an iterable and adds the index of each item to the item itself.

Is there an equivalent in Elixir?

like image 365
Darian Moody Avatar asked Sep 08 '15 11:09

Darian Moody


1 Answers

So, a helpful person (OliverMT) in the Elixir IRC channel pointed me in the right direction:

iex> Enum.with_index [1,2,3]
[{1,0},{2,1},{3,2}]

Docs: Enum.with_index/1

NB: compared to the result from Python, the order of index/value is swapped.
e.g It is {value, index} and not (index, value) like in Python.

like image 65
Darian Moody Avatar answered Sep 27 '22 21:09

Darian Moody