تركيب IO
تسلسل الآثار
تركيب IO درس مجاني في 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 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Sequencing with flatMap
Because IO is a monad, you compose effects with flatMap. Each step runs in order, and a later step can use the result of an earlier one.
import cats.effect.IO
val program: IO[Unit] =
IO("Alice").flatMap(name => IO.println(s"Hello, $name"))for-comprehension
A for-comprehension desugars to flatMap/map and reads like an imperative script while staying pure.
import cats.effect.IO
val prog: IO[Unit] = for {
_ <- IO.println("What is your name?")
name <- IO("Bob")
_ <- IO.println(s"Hi $name")
} yield ()Order Is Preserved
Effects in a for-comprehension run top to bottom when the program is executed. The structure of your code mirrors execution order.
Combining Independent Effects
When two effects do not depend on each other, use mapN or (a, b).tupled to combine them. They still run sequentially by default but express independence.
import cats.effect.IO
import cats.syntax.apply._
val combined: IO[Int] = (IO(2), IO(3)).mapN(_ + _)Running Effects in Parallel
Use parMapN (from cats.syntax.parallel) to run independent effects concurrently on separate fibers and combine their results.
import cats.effect.IO
import cats.syntax.parallel._
val par: IO[Int] = (IO(2), IO(3)).parMapN(_ + _)Discarding Results
Use *> to run an effect and keep the second result, or <* to keep the first. void discards the result entirely, yielding IO[Unit].
import cats.effect.IO
val r1: IO[Int] = IO.println("log") *> IO(42)
val r2: IO[Unit] = IO(99).voidRepeating Effects
replicateA(n) runs an effect n times and collects results into a list. foreverM repeats indefinitely.
import cats.effect.IO
val three: IO[List[Unit]] = IO.println("tick").replicateA(3)Traversing a List
Turn a List[A] into an effect over a List[B] with traverse. Each element produces an IO, and the results are gathered.
import cats.effect.IO
import cats.syntax.traverse._
val names = List("a", "b", "c")
val prog: IO[List[Unit]] = names.traverse(n => IO.println(n))parTraverse
parTraverse is the parallel version of traverse — each element's effect runs on its own fiber, ideal for concurrent I/O like fetching many URLs.
import cats.effect.IO
import cats.syntax.parallel._
val ids = List(1, 2, 3)
val prog: IO[List[Int]] = ids.parTraverse(id => IO(id * 10))A Composed Program
This program reads a value, doubles it, and prints the result — all composed from small IO steps in a single description.
import cats.effect.{IO, IOApp}
object Doubler extends IOApp.Simple {
def run: IO[Unit] = for {
n <- IO(21)
d = n * 2
_ <- IO.println(s"Doubled: $d")
} yield ()
}Plain Scala Sequencing
For comparison, here is sequential console output in plain Scala — eager and impure, unlike the deferred IO version above.
object Main {
def main(args: Array[String]): Unit = {
val n = 21
val d = n * 2
println(s"Doubled: $d")
}
}Quick Check
Which method runs two independent effects concurrently and combines their results?
Recap
You composed IO programs with:
flatMapandforfor sequencingmapN/parMapNfor independent effects*>,<*,voidfor combining/discardingtraverse/parTraversefor collections
Next: handling errors inside IO.
الأسئلة الشائعة
هل درس «تركيب IO» مجاني؟
نعم — نص درس «تركيب IO» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Scala for Backend Engineering & Functional Programming، انتقل إلى CoddyKit PRO. تتضمن دورة Scala for Backend Engineering & Functional Programming 4 دروس في المجموع.
ماذا ستتعلم في «تركيب IO»؟
تسلسل الآثار تتمرن على Scala for Backend Engineering & Functional Programming مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Scala for Backend Engineering & Functional Programming؟
لا تُشترط خبرة سابقة. Scala for Backend Engineering & Functional Programming على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.
كم من الوقت يستغرق درس «تركيب IO»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Scala for Backend Engineering & Functional Programming هذا؟
نعم. كل درس في Scala for Backend Engineering & Functional Programming يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.