ชนิดข้อมูลและตัวดำเนินการพื้นฐาน
สำรวจชนิดข้อมูลพื้นฐานของ Elixir เช่น อะตอม รายการ ทูเพิล แผนที่ และเรียนรู้การใช้ตัวดำเนินการพื้นฐาน
ชนิดข้อมูลและตัวดำเนินการพื้นฐาน เป็นบทเรียน Elixir & Phoenix: Scalable Backend Development ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Elixir & Phoenix: Scalable Backend Development และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Elixir & Phoenix: Scalable Backend Development มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Data Types & Operators Intro
Every program rests on data types (what you can store) and operators (the symbols that act on it). Let us meet Elixir core.
Numbers: Integers & Floats
Elixir has two number types: integers like 42 and floats like 3.14. Standard math works on both, as this snippet shows.
defmodule NumbersDemo do
def run() do
integer_num = 42
float_num = 3.14159
IO.puts "Integer: #{integer_num}"
IO.puts "Float: #{float_num}"
IO.puts "Sum: #{integer_num + 10.0}"
end
end
NumbersDemo.run()Booleans: True, False, Nil
Truth values are true and false. Note nil for "no value" — and that both false and nil are falsy; everything else is truthy.
defmodule BooleansDemo do
def run() do
is_active = true
has_error = false
no_value = nil
IO.puts "Is Active? #{is_active}"
IO.puts "Has Error? #{has_error}"
IO.puts "No Value? #{no_value}"
if no_value do
IO.puts "This won't print."
else
IO.puts "nil is falsy!"
end
end
end
BooleansDemo.run()Atoms: Unique Identifiers
An atom is a constant whose name is its own value, like :ok. They are fast to compare and ideal as status codes and map keys.
defmodule AtomsDemo do
def run() do
status = :ok
type = :user
IO.puts "Current status: #{status}"
IO.puts "User type: #{type}"
if status == :ok do
IO.puts "Operation successful!"
end
end
end
AtomsDemo.run()Strings: Text Data
An Elixir string is a UTF-8 binary in double quotes, so it handles any language. Join strings with the <> operator.
defmodule StringsDemo do
def run() do
greeting = "Hello"
name = "CoddyKit"
message = greeting <> ", " <> name <> "!"
IO.puts message
IO.puts "String length: " <> to_string(String.length(message))
end
end
StringsDemo.run()Lists: Ordered Collections
A list is an ordered, square-bracketed collection that can mix types. It is the functional workhorse, fast to prepend to at the head.
defmodule ListsDemo do
def run() do
my_list = [1, 2, 3, :hello, "world"]
another_list = [4, 5]
IO.puts "Original list: " <> inspect(my_list)
IO.puts "Concatenated list: " <> inspect(my_list ++ another_list)
IO.puts "Head of list: " <> inspect(hd(my_list))
IO.puts "Tail of list: " <> inspect(tl(my_list))
end
end
ListsDemo.run()Tuples: Fixed-Size Collections
A tuple is a fixed-size collection in curly braces. You see it everywhere for grouped returns like {:ok, result} or {:error, reason}.
defmodule TuplesDemo do
def run() do
person_info = {"Alice", 30, :female}
result_tuple = {:ok, "Data fetched"}
IO.puts "Person: " <> inspect(person_info)
IO.puts "Result: " <> inspect(result_tuple)
# Accessing elements (0-indexed)
IO.puts "Name: " <> elem(person_info, 0)
end
end
TuplesDemo.run()Maps: Key-Value Pairs
A map holds key-value pairs in %{} — usually atom keys for speed. Reach for it whenever you model structured records or config.
defmodule MapsDemo do
def run() do
user = %{name: "Bob", age: 25, city: "New York"}
IO.puts "User: " <> inspect(user)
IO.puts "User's name: #{user.name}"
# Updating a map
updated_user = %{user | age: 26}
IO.puts "Updated age: #{updated_user.age}"
end
end
MapsDemo.run()Arithmetic Operators
Elixir gives you the usual arithmetic operators, plus div and rem for integers. Heads up: / always returns a float.
defmodule ArithmeticDemo do
def run() do
IO.puts "5 + 3 = #{5 + 3}"
IO.puts "10 - 4 = #{10 - 4}"
IO.puts "6 * 7 = #{6 * 7}"
IO.puts "10 / 3 = #{10 / 3}" # Float division
IO.puts "10 div 3 = #{10 div 3}" # Integer division
IO.puts "10 rem 3 = #{10 rem 3}" # Remainder
end
end
ArithmeticDemo.run()Comparison Operators
Comparison operators return a boolean. Use == for value equality, but === when type matters too — 1 === 1.0 is false.
defmodule ComparisonDemo do
def run() do
IO.puts "5 == 5: #{5 == 5}"
IO.puts "5 != 10: #{5 != 10}"
IO.puts "5 < 10: #{5 < 10}"
IO.puts "10 >= 10: #{10 >= 10}"
IO.puts "1 == 1.0: #{1 == 1.0}" # True (value equality)
IO.puts "1 === 1.0: #{1 === 1.0}" # False (type strictness)
end
end
ComparisonDemo.run()Check Your Understanding
Consider the following Elixir code snippet:
result = 10 div 3
status = :ok
message = "Value is #{result}"
comparison = (result > 3) == true
IO.puts "#{message}, Status: #{status}, Comparison: #{comparison}"What will be the final output of this code?
Check Your Understanding (Revised)
Consider the following Elixir code snippet:
result = 10 div 3
status = :ok
message = "Value is #{result}"
comparison = (result > 3)
IO.puts "#{message}, Status: #{status}, Comparison: #{comparison}"What will be the final output of this code?
Recap: Data Types & Operators
Recap: integers and floats, booleans plus nil, atoms, strings, lists, tuples, maps, and the operators that drive them. These are your Elixir foundation.
คำถามที่พบบ่อย
บทเรียน “ชนิดข้อมูลและตัวดำเนินการพื้นฐาน” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “ชนิดข้อมูลและตัวดำเนินการพื้นฐาน” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Elixir & Phoenix: Scalable Backend Development ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Elixir & Phoenix: Scalable Backend Development มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ชนิดข้อมูลและตัวดำเนินการพื้นฐาน”
สำรวจชนิดข้อมูลพื้นฐานของ 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 ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- บทนำสู่ภาษา Elixir
- ชนิดข้อมูลและตัวดำเนินการพื้นฐาน
- การจับคู่รูปแบบและลำดับการควบคุม
- สตริง ไบนารี และซิกิลใน Elixir