Book Image

Elixir Cookbook

By : Paulo Pereira
Book Image

Elixir Cookbook

By: Paulo Pereira

Overview of this book

Table of Contents (16 chapters)
Elixir Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Creating a key/value store with a map


In Elixir, map is the tool to use when we need a very simple key/value store. A map is a data type for associative collections (or dictionaries).

The Map module is an implementation of the Dict API. The following is the Dict documentation page:

Getting ready

Start a new IEx session in your console.

How to do it…

In this recipe, we will create an in-memory database of the English Premier League, where we will keep the current points, number of played games, and the club name. We will be creating a map to hold the league and a map for each team. This will be a map of maps! The steps are as follows:

  1. We will create the map to hold the League data:

    iex(1)> premier_league_2013 = %{}
    

    Tip

    To create a new map, we might also use the Map.new/0 function:

    premier_league = Map.new
  2. Now, it's time to add some data about the teams:

    iex(2)> man_city = %{:position=> 1, :points=> 86, :played=> 38, :name=> "Manchester City"}
    iex(3)> liverpool = %{:position =...