Lesson 3
Introduction to Maps
Introduction to Maps

Welcome back! In the previous lessons, we discussed lists and tuples in Elixir, both of which are fundamental data structures for storing and managing data. In this unit, we will focus on another essential data structure: maps.

Maps are incredibly useful when you need to store key-value pairs. They provide an efficient way to associate values with unique keys, making data retrieval swift and intuitive. Mastering maps will significantly enhance your ability to work with structured data in Elixir.

What You'll Learn

In this lesson, we will explore various operations you can perform with maps in Elixir:

  1. Creating a map
  2. Inspecting a map
  3. Accessing elements within a map

Let's start by creating and inspecting a map:

Elixir
1# Create a map with two key-value pairs 2map = %{name: "Elixir", age: 10} 3 4# Display the map using IO.inspect 5IO.inspect(map) # Output: %{name: "Elixir", age: 10} 6 7# Access the value associated with the key :name 8IO.puts(map[:name]) # Output: Elixir 9IO.puts(map.age) # Output: 10

The above code snippet demonstrates how to create a map and inspect its contents. The IO.inspect(map) line will print %{name: "Elixir", age: 10} to the console.

A key difference between maps and other Elixir data structures like lists and tuples is that maps use keys to access data, whereas lists and tuples use positions. Lists and tuples are better for ordered data, while maps are great when you want to label each piece of data with a meaningful key.

Let's also understand what are the ways to access the values from the map:

  1. Using square brackets [:key] syntax: With this syntax, you can access the value associated with a specific key by providing the key in square brackets as an atom. For example, map[:name] will return the value "Elixir" associated with the key :name. Notice that the key is an atom, which is a unique identifier in Elixir – even if the keys don't look like atoms, they are converted to atoms internally.
  2. Using dot notation .key syntax: You can also access the value associated with a key using the dot notation. For example, map.age will return the value 10 associated with the key :age.

Notice, that you can explicitely use strings as keys as follows:

Elixir
1map = %{"name" => "Elixir", "age" => 10} 2 3IO.inspect(map) # Output: %{"name" => "Elixir", "age" => 10} 4IO.puts(map["name"]) # Output: Elixir 5IO.puts(map[:name]) # Output: nil (empty value) 6# IO.puts(map.name) # This will raise an error

Notice that in this case, the keys are strings, and you can access the values using the string keys. However, you cannot access the values using the dot notation because the keys are not atoms. Similarly, you cannot access the values using square brackets with atom keys.

Why It Matters

Maps are a vital part of Elixir programming for several reasons:

  1. Flexibility: Maps allow you to associate keys with values, providing a flexible way to store and retrieve data.
  2. Efficiency: Accessing data in maps is usually fast because keys are unique, making data retrieval efficient.
  3. Organization: Maps help you organize related pieces of data, making your code cleaner and easier to maintain.

Understanding maps will empower you to handle complex data structures more effectively, improving your overall coding proficiency in Elixir.

Are you excited to dive deeper into maps and enhance your Elixir skills? Let's proceed to the practice section and explore these concepts hands-on.

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.