All posts

Iterating a List with_index in Elixir


In Ruby you can get the index value when iterating over a list by calling the method each_with_index on the array object itself. This method makes the iterable object and its index available to you inside of a block, like so:

arr = ["one", "two", "three"]
arr.each_with_index do |number, index|
  # handle logic here

The way to handle this in Elixir is to use the Enum module which works with any enumerable data type in the Elixir language, including List. The Enum module has a function .with_index that can be used to get the index value for each item in the list on iteration. In practice that could look like:

    list = [Post.find(1), Post.find(2)]
    |> Enum.with_index
    |> Enum.filter(fn({post, index}) ->
      # handle logic here

Here we are piping the list to the Enum.with_index/2 and then piping the result of that to the Enum.filter/2 function. As demonstrated in the code above the iterable object and itโ€™s index are available in the callback function of the filter function.


I built two products to help fund continued efforts for this website, it would mean a lot if you would check them out:
1. 2500+ Words for the Undermotivated Software Engineer - A short essay I wrote on the titled topic ($3).
2. Build 3 Chrome Extensions in Under 2 Hours - A course I built to teach chrome extension development ($9-20).