Lesson 1
Introduction to Enum.map
Introduction to Enum.map

Welcome to this new unit about Elixir!

This final course focuses on building a TodoList application using Elixir. The course conists of 4 units, the last two units are about building the TodoList application. But before we start building the TodoList application, we will learn about more advanced topics in Elixir such as Enum.map, Enum.filter and Enum.reject. Let's get started!

Today, we will embark on an exciting journey to explore Enum.map, an important function in Elixir that empowers you to manipulate collections efficiently. As we delve into building a TodoList application, understanding Enum.map is your first step toward mastering list transformations in Elixir. Let's ensure you have a strong foundation to move forward confidently.

What You'll Learn

In this lesson, you'll learn the basics of Enum.map and how it can be used to update elements in a collection. This is a foundational concept in functional programming. Using Enum.map, you'll be able to traverse a list and apply changes to its elements, creating new lists reflecting those changes.

You will see Enum.map in action with a real-world example. Consider the following code from our lesson:

Elixir
1defmodule Student do 2 defstruct id: nil, name: nil, grade: nil 3 4 def update_grade(students, id, new_grade) do 5 Enum.map(students, fn student -> 6 if student.id == id do 7 %Student{student | grade: new_grade} 8 else 9 student 10 end 11 end) 12 end 13end 14 15defmodule Main do 16 def run do 17 students = [ 18 %Student{id: 1, name: "Alice", grade: 90}, 19 %Student{id: 2, name: "Bob", grade: 80}, 20 %Student{id: 3, name: "Charlie", grade: 70} 21 ] 22 23 tasks = Student.update_grade(students, 2, 85) 24 IO.inspect(tasks) 25 end 26end 27 28Main.run()

In this example, we define a list of Student structs, and then we use Enum.map to update the grade for a student with a specific ID. This action results in a new list with the updated grades. As a result students list will be updated with the new grade for the student with ID 2.

Let's understand the syntax for Enum.map and how it works in this example.

The Enum.map function takes two arguments: a list and a function. The function is applied to each element in the list, and the result is a new list with the transformed elements. In this example the first argument is the students list and the second argument is an anonymous function that takes a student as an argument and returns a new Student struct with the updated grade.

Now let's break down what Enum.map does in this example:

  • Enum.map iterates over each element in the students list and applies the anonymous function to each element (student).
  • In the anonymous function, we check if the id of the student matches the id we want to update which is passed as an argument to the update_grade function.
  • If the id matches, we create a new Student struct with the updated grade.
  • If the id does not match, we return the original student.

Thus, for each student in the list, we either return the original student or a new student with the updated grade. The result is a new list with the updated grades.

Why It Matters

Understanding Enum.map enables you to transform datasets in a concise and functional manner. This is essential when working with collections, as it allows you to perform operations across all elements efficiently. By mastering Enum.map, you'll gain the ability to manipulate data with precision and clarity, making your code more readable and maintainable.

Feeling ready to dive into practicing with Enum.map? Let's get started and see how transforming data becomes an exciting task with Elixir.

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