0Pricing
Elixir & Phoenix: Scalable Backend Development · บทเรียน

การทำงานกับคอลเลกชันที่แจกแจงได้

ค้นพบวิธีจัดการรายการ แผนที่ และคอลเลกชันที่แจกแจงได้ประเภทอื่น ๆ อย่างมีประสิทธิภาพด้วยโมดูล `Enum` ของ Elixir

การทำงานกับคอลเลกชันที่แจกแจงได้ เป็นบทเรียน Elixir & Phoenix: Scalable Backend Development ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Elixir & Phoenix: Scalable Backend Development และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Elixir & Phoenix: Scalable Backend Development มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

What are Enumerable Collections?

In Elixir, an enumerable is a data structure that you can iterate over, element by element. Think of them as containers for multiple values.

Elixir's powerful Enum module provides many functions to work with these collections efficiently.

  • Lists: Ordered collections of any data type.
  • Maps: Key-value stores.
  • Ranges: Sequences of numbers.

Understanding enumerables is key to functional programming in Elixir.

Working with Elixir Lists

Lists are perhaps the most common enumerable. They are ordered and can hold mixed data types.

Let's see a simple list:

defmodule ListExample do
  def run do
    my_list = [1, "hello", :atom, 4.5]
    IO.puts "My list: #{inspect my_list}"
  end
end

ListExample.run()

Exploring Maps as Enums

Maps store data in key-value pairs. While their primary use is lookup, they are also enumerable, meaning you can iterate over their key-value pairs.

When you iterate a map, each element is a tuple {key, value}.

defmodule MapExample do
  def run do
    my_map = %{name: "Alice", age: 30}
    IO.puts "My map: #{inspect my_map}"
  end
end

MapExample.run()

Introducing the `Enum` Module

The Enum module is Elixir's standard library for working with all types of enumerables. It provides a consistent interface for common operations.

Instead of writing loops, you use Enum functions to transform, filter, and aggregate collections in an immutable, functional way.

  • No changing data in place.
  • Functions return new collections.
  • Promotes clear and concise code.

Performing Actions with `Enum.each`

Enum.each/2 is used when you want to perform a side effect for each element in an enumerable, like printing to the console. It always returns :ok.

It's similar to a for-each loop in other languages, but it's purely for side effects.

defmodule EachExample do
  def run do
    numbers = [1, 2, 3]
    IO.puts "Using Enum.each:"
    Enum.each(numbers, fn num -> IO.puts "Number: #{num}" end)
  end
end

EachExample.run()

Transforming Data with `Enum.map`

Enum.map/2 applies a given function to each element of an enumerable and collects the results into a new list. This is perfect for transforming data.

The original enumerable remains unchanged, sticking to functional programming principles.

defmodule MapTransformExample do
  def run do
    numbers = [1, 2, 3]
    doubled_numbers = Enum.map(numbers, fn num -> num * 2 end)
    IO.puts "Original: #{inspect numbers}"
    IO.puts "Doubled: #{inspect doubled_numbers}"
  end
end

MapTransformExample.run()

Selecting Elements with `Enum.filter`

Enum.filter/2 creates a new list containing only the elements for which a given function returns a "truthy" value (anything not false or nil).

Use it when you need to select a subset of elements from a collection.

defmodule FilterExample do
  def run do
    numbers = [1, 2, 3, 4, 5, 6]
    even_numbers = Enum.filter(numbers, fn num -> rem(num, 2) == 0 end)
    IO.puts "All numbers: #{inspect numbers}"
    IO.puts "Even numbers: #{inspect even_numbers}"
  end
end

FilterExample.run()

Reducing Collections with `Enum.reduce`

Enum.reduce/3 (or Enum.reduce/2) is one of the most powerful Enum functions. It "reduces" an enumerable to a single value by applying a function iteratively.

You provide an initial accumulator value and a function that takes the current element and the accumulator, returning a new accumulator.

defmodule ReduceExample do
  def run do
    numbers = [1, 2, 3, 4]
    sum = Enum.reduce(numbers, 0, fn num, acc -> num + acc end)
    IO.puts "Numbers: #{inspect numbers}"
    IO.puts "Sum: #{sum}"
  end
end

ReduceExample.run()

More `Enum` Functions to Know

The Enum module is vast! Here are a few more functions you'll find useful:

  • Enum.all?(enum, fun): Checks if all elements satisfy a condition.
  • Enum.any?(enum, fun): Checks if any element satisfies a condition.
  • Enum.take(enum, n): Returns the first n elements.
  • Enum.drop(enum, n): Returns all elements except the first n.
  • Enum.sort(enum): Sorts the elements.

Explore the official Elixir documentation for the full list!

Test Your `Enum` Knowledge

Consider the following Elixir code snippet:

defmodule QuestionCode do
  def run do
    data = [1, 2, 3, 4, 5]
    result_a = Enum.map(data, fn x -> x * 10 end)
    result_b = Enum.filter(data, fn x -> rem(x, 2) == 0 end)
    result_c = Enum.reduce(data, 0, fn x, acc -> x + acc end)
    IO.puts "A: #{inspect result_a}"
    IO.puts "B: #{inspect result_b}"
    IO.puts "C: #{inspect result_c}"
  end
end

QuestionCode.run()

Recap: Enumerable Power

Great job! You've learned the fundamentals of working with enumerable collections in Elixir.

  • Enumerables are data structures you can iterate over (lists, maps, ranges).
  • The Enum module provides powerful functions for consistent, immutable operations.
  • Key functions include each (side effects), map (transform), filter (select), and reduce (aggregate).

These functions are cornerstones of functional programming in Elixir, enabling clean and efficient data manipulation.

คำถามที่พบบ่อย

บทเรียน “การทำงานกับคอลเลกชันที่แจกแจงได้” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การทำงานกับคอลเลกชันที่แจกแจงได้” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Elixir & Phoenix: Scalable Backend Development ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Elixir & Phoenix: Scalable Backend Development มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การทำงานกับคอลเลกชันที่แจกแจงได้”

ค้นพบวิธีจัดการรายการ แผนที่ และคอลเลกชันที่แจกแจงได้ประเภทอื่น ๆ อย่างมีประสิทธิภาพด้วยโมดูล `Enum` ของ Elixir คุณปฏิบัติ Elixir & Phoenix: Scalable Backend Development ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Elixir & Phoenix: Scalable Backend Development หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Elixir & Phoenix: Scalable Backend Development บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “การทำงานกับคอลเลกชันที่แจกแจงได้” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Elixir & Phoenix: Scalable Backend Development นี้ได้ไหม

ได้ บทเรียน Elixir & Phoenix: Scalable Backend Development ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. ฟังก์ชัน โมดูล และการส่งต่อข้อมูล
  2. การทำงานกับคอลเลกชันที่แจกแจงได้
  3. การเรียกซ้ำและฟังก์ชันลำดับสูง
  4. การประเมินแบบขี้เกียจด้วยโมดูล Stream
← กลับไปที่ Elixir & Phoenix: Scalable Backend Development