Looping through an array / list

Essential Elixir : part 4 of 10 published on Aug 10, 2015

We have an array/list of posts.

posts = ["Elixir is cool", "Making sense of Ember Data", "Gulp is awesome"]

Our objective is to output each post to console.

Enum.each

We can use Enum.each like this:

Enum.each posts, fn post -> 
  IO.inspect post
end

Recursion & Pattern Matching

We can also use recursion and pattern matching.

def output_post([post|posts]) do
  IO.inspect post
  output_post(posts)
end
def output_post([]), do: nil
Next: Viewing all the functions in a module