Welcome back! We've already explored how to transform datasets using Enum.map
. Now, let's take the next logical step in mastering list manipulations in Elixir — by introducing Enum.filter
and Enum.reject
. Both are powerful tools when you want to work with specific elements of a dataset. Before we dive in, remember that building a TodoList application is just around the corner. Understanding these tools will greatly enhance your capability to manipulate collections as we proceed.
In today's lesson, you'll learn how to streamline your data processing using Enum.filter
and Enum.reject
. Here's what each one does:
Enum.filter
: Allows you to select elements from a list that satisfy a particular condition.Enum.reject
: Helps you remove elements from a list based on a condition.
Here's a snippet from our lesson to illustrate these functions:
Elixir1defmodule Student do 2 defstruct id: nil, name: "", grade: nil 3 4 def remove_student(students, id) do 5 Enum.reject(students, fn student -> student.id == id end) 6 end 7 8 def get_students_by_grade(students, grade) do 9 Enum.filter(students, fn student -> student.grade == grade end) 10 end 11end 12 13defmodule Main do 14 def run do 15 students = [ 16 %Student{id: 1, name: "Alice", grade: :A}, 17 %Student{id: 2, name: "Bob", grade: :B}, 18 %Student{id: 3, name: "Charlie", grade: :A_plus}, 19 %Student{id: 4, name: "David", grade: :A_plus}, 20 %Student{id: 5, name: "Eve", grade: :E} 21 ] 22 23 students = Student.remove_student(students, 1) 24 IO.inspect students 25 26 a_plus_students = Student.get_students_by_grade(students, :A_plus) 27 IO.inspect a_plus_students 28 end 29end 30 31Main.run()
In this example, we use Enum.reject
to remove a student by their ID
and Enum.filter
to find students with a specific grade. These functions make your code shorter, clearer, and more efficient.
-
Enum.reject/2
: This function takes a list and a function as arguments. The function iterates over each element in the list, removing elements for which the provided function returnstrue
. In this example,Enum.reject(students, fn student -> student.id == id end)
removes the student whoseid
matches the specifiedid
. If there are no matches, the original list is returned. -
Enum.filter/2
: Similarly, this function also takes a list and a function. It iterates through the list, keeping elements for which the provided function returnstrue
. In the example,Enum.filter(students, fn student -> student.grade == grade end)
returns a list of students whosegrade
matches the specifiedgrade
. If there are no matches, an empty list is returned.
Both functions leverage anonymous functions to evaluate each element against a condition, ensuring that the list is processed according to the defined logic — in this case, either removing or filtering elements based on specific criteria.
Knowing how to efficiently filter and exclude items from lists is crucial in data processing and application development. Whether you are cleaning up data or making particular selections, understanding Enum.filter
and Enum.reject
equips you with the skills to handle data selectively. This makes your applications both performant and easier to maintain — key ingredients in any successful project. Let's get started with the practice section to see these tools in action!