0Pricing
Scala for Backend Engineering & Functional Programming · درس

أنواع البيانات الجبرية

نمذجة نطاقكم

أنواع البيانات الجبرية درس مجاني في Scala for Backend Engineering & Functional Programming على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Scala for Backend Engineering & Functional Programming، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Scala for Backend Engineering & Functional Programming 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

What Is an ADT?

An algebraic data type (ADT) is a type built by combining other types. In Scala you model ADTs with a sealed trait plus case class and case object variants.

ADTs let you precisely describe the shape of your domain data.

Sum Types

A sum type means a value is one of several alternatives (an OR). A traffic light is Red OR Yellow OR Green.

You express this with a sealed trait and several variants.

sealed trait Light
case object Red extends Light
case object Yellow extends Light
case object Green extends Light
object Main {
  def main(args: Array[String]): Unit = {
    val l: Light = Yellow
    println(l)
  }
}

Product Types

A product type means a value combines several fields (an AND). A point has an x AND a y.

Case classes are product types.

case class Point(x: Int, y: Int)
object Main {
  def main(args: Array[String]): Unit = {
    val p = Point(3, 4)
    println(s"${p.x} and ${p.y}")
  }
}

Combining Sum and Product

Real ADTs mix both: a sealed trait (sum) whose variants are case classes (products).

Here a Shape is a Circle (with radius) OR a Rectangle (with width and height).

sealed trait Shape
case class Circle(radius: Double) extends Shape
case class Rectangle(w: Double, h: Double) extends Shape
object Main {
  def main(args: Array[String]): Unit = {
    val s: Shape = Rectangle(3, 4)
    println(s)
  }
}

Operating on an ADT

You process an ADT by pattern matching on its variants. Each branch handles one shape.

sealed trait Shape
case class Circle(radius: Double) extends Shape
case class Rectangle(w: Double, h: Double) extends Shape
object Main {
  def area(s: Shape): Double = s match {
    case Circle(r)       => 3.14159 * r * r
    case Rectangle(w, h) => w * h
  }
  def main(args: Array[String]): Unit = {
    println(area(Circle(2)))
    println(area(Rectangle(3, 4)))
  }
}

Modeling a Domain

ADTs make invalid states unrepresentable. Model a payment method as a closed set of valid options.

sealed trait PaymentMethod
case class CreditCard(number: String) extends PaymentMethod
case class BankTransfer(iban: String) extends PaymentMethod
case object Cash extends PaymentMethod
object Main {
  def main(args: Array[String]): Unit = {
    val m: PaymentMethod = Cash
    println(m)
  }
}

Recursive ADTs

An ADT can refer to itself, which is how you model trees and lists. Here is a simple binary tree.

sealed trait Tree
case object Leaf extends Tree
case class Node(value: Int, left: Tree, right: Tree) extends Tree
object Main {
  def main(args: Array[String]): Unit = {
    val t: Tree = Node(1, Leaf, Node(2, Leaf, Leaf))
    println(t)
  }
}

Folding Over a Recursive ADT

Recursion plus matching lets you process the whole structure. Here we sum every value in a tree.

sealed trait Tree
case object Leaf extends Tree
case class Node(value: Int, left: Tree, right: Tree) extends Tree
object Main {
  def sum(t: Tree): Int = t match {
    case Leaf            => 0
    case Node(v, l, r)   => v + sum(l) + sum(r)
  }
  def main(args: Array[String]): Unit = {
    val t = Node(1, Node(2, Leaf, Leaf), Node(3, Leaf, Leaf))
    println(sum(t))
  }
}

Why ADTs?

ADTs are powerful because they:

  • Describe data precisely as sums and products
  • Make illegal states impossible to construct
  • Pair perfectly with exhaustive pattern matching
  • Scale to recursive structures like trees and lists

ADTs vs Inheritance

Traditional OOP would scatter behavior across subclasses. ADTs keep data definitions closed and put behavior in functions that pattern match.

This separation of data and behavior is a hallmark of functional design.

Putting It Together

A complete small ADT with a function that handles every variant.

sealed trait Json
case class JNum(n: Double) extends Json
case class JStr(s: String) extends Json
case object JNull extends Json
object Main {
  def render(j: Json): String = j match {
    case JNum(n) => n.toString
    case JStr(s) => "\"" + s + "\""
    case JNull   => "null"
  }
  def main(args: Array[String]): Unit = {
    List(JNum(3.0), JStr("hi"), JNull).foreach(j => println(render(j)))
  }
}

Quick Check

Test your understanding of ADTs.

Recap

You learned to build algebraic data types:

  • Sum types: sealed trait with multiple variants (OR)
  • Product types: case classes with fields (AND)
  • Combine them to model domains precisely
  • ADTs can be recursive (trees, lists)
  • Process them with pattern matching

الأسئلة الشائعة

هل درس «أنواع البيانات الجبرية» مجاني؟

نعم — نص درس «أنواع البيانات الجبرية» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Scala for Backend Engineering & Functional Programming، انتقل إلى CoddyKit PRO. تتضمن دورة Scala for Backend Engineering & Functional Programming 4 دروس في المجموع.

ماذا ستتعلم في «أنواع البيانات الجبرية»؟

نمذجة نطاقكم تتمرن على Scala for Backend Engineering & Functional Programming مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Scala for Backend Engineering & Functional Programming؟

لا تُشترط خبرة سابقة. Scala for Backend Engineering & Functional Programming على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.

كم من الوقت يستغرق درس «أنواع البيانات الجبرية»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Scala for Backend Engineering & Functional Programming هذا؟

نعم. كل درس في Scala for Backend Engineering & Functional Programming يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. Case Classes
  2. Traits مختومة
  3. أنواع البيانات الجبرية
  4. المطابقة الشاملة
← العودة إلى Scala for Backend Engineering & Functional Programming