IO Birleştirme
Etkileri sıralama
IO Birleştirme, CoddyKit'te ücretsiz bir Scala for Backend Engineering & Functional Programming dersidir. Bu, 4 dersinin 3. dersidir. Aşağıdan dersin tamamını ücretsiz okuyabilir, sonra tarayıcıda yerleşik kod editörü ve 7/24 yapay zeka koçu ile uygulamalı olarak pratik yapabilirsin. Bu, Scala for Backend Engineering & Functional Programming öğrenme yolunun bir parçasıdır ve ilerlemeniz web ve CoddyKit uygulaması arasında senkronize olur. Scala for Backend Engineering & Functional Programming kursu toplamda 4 dersten oluşur.
Bu dersin bazı bölümleri henüz çevrilmemiş olup İngilizce olarak gösterilmektedir.
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.
Sıkça Sorulan Sorular
“IO Birleştirme” dersi ücretsiz mi?
Evet — “IO Birleştirme” dersin tüm metni burada web'de ücretsiz olarak okunabilir. Etkileşimli olarak pratik yapmak (yerleşik kod editörü ve 7/24 yapay zeka koçu) ve Scala for Backend Engineering & Functional Programming kursunun geri kalanını açmak için CoddyKit PRO'ya yükselt. Scala for Backend Engineering & Functional Programming kursu toplamda 4 dersten oluşur.
“IO Birleştirme” dersinde ne öğreneceğim?
Etkileri sıralama Scala for Backend Engineering & Functional Programming ile uygulamalı kodu tarayıcıda doğrudan çalıştırarak pratik yaparsın ve 7/24 yapay zeka koçu dersi çalışırken sorularını yanıtlar.
Scala for Backend Engineering & Functional Programming öğrenmeye başlamak için deneyim gerekli mi?
Önceden deneyim gerekmez. CoddyKit'te Scala for Backend Engineering & Functional Programming, başlangıçtan ileri seviyeye kadar yapılandırıldığı için buradan başlayabilir veya başından başlayıp kendi hızında ilerleme yapabilirsin. Bu, 4 dersinin 3. dersidir.
“IO Birleştirme” dersi ne kadar sürer?
Çoğu CoddyKit dersi yaklaşık 5–10 dakika sürer. Her biri kısa ve etkileşimli olduğu için sabit ilerleme yaparsın ve web ile uygulama arasında tam olarak bıraktığın yerden devam edebilirsin.
Bu Scala for Backend Engineering & Functional Programming dersinde kod yazıp çalıştırabilir miyim?
Evet. Her Scala for Backend Engineering & Functional Programming dersi yerleşik bir kod editörü içerir, bu sayede tarayıcıda gerçek kod yazıp çalıştırabilir ve anlık yapay zeka geri bildirimi alırsın — yerel kurulum gerekli değildir.
Bu kursun tüm dersleri
- Cats Tür Sınıfları
- IO Monad'ı
- IO Birleştirme
- IO'da Hata İşleme