0Pricing
Scala for Backend Engineering & Functional Programming · Ders

Birden Çok Üretici

İç içe yineleme

Birden Çok Üretici, 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.

Nested iteration made flat

A for-comprehension can have several generators. Each one is a <- line that iterates over a collection. Together they produce every combination, a kind of nested loop written flatly.

object Main {
  def main(args: Array[String]): Unit = {
    val pairs = for {
      x <- List(1, 2)
      y <- List('a', 'b')
    } yield (x, y)
    println(pairs)
  }
}

It is a cartesian product

Two generators of sizes m and n produce m × n results, the full cartesian product. The first generator is the outer loop, the second the inner loop.

object Main {
  def main(args: Array[String]): Unit = {
    val grid = for {
      row <- 1 to 3
      col <- 1 to 3
    } yield s"r$row-c$col"
    println(grid.mkString(", "))
  }
}

Inner generator can depend on outer

A later generator can use values bound by earlier ones. This is impossible with a plain cartesian product and is one reason for-comprehensions are so flexible.

object Main {
  def main(args: Array[String]): Unit = {
    val upperTriangle = for {
      i <- 1 to 4
      j <- i to 4
    } yield (i, j)
    println(upperTriangle)
  }
}

Three or more generators

You can stack as many generators as you like. With three generators you get a triple-nested loop, producing tuples or computed values.

object Main {
  def main(args: Array[String]): Unit = {
    val triples = for {
      a <- 1 to 2
      b <- 1 to 2
      c <- 1 to 2
    } yield (a, b, c)
    println(triples.size + " combinations")
    triples.foreach(println)
  }
}

The classic Pythagorean example

A famous use: find Pythagorean triples. We iterate three numbers and keep only those where a*a + b*b == c*c, combining multiple generators with a guard.

object Main {
  def main(args: Array[String]): Unit = {
    val triples = for {
      a <- 1 to 20
      b <- a to 20
      c <- b to 20
      if a * a + b * b == c * c
    } yield (a, b, c)
    triples.foreach(println)
  }
}

How it desugars

Multiple generators become nested flatMap calls with a final map:

  • xs.flatMap(x => ys.flatMap(y => zs.map(z => ...)))

Each extra generator adds one more nested level.

object Main {
  def main(args: Array[String]): Unit = {
    val sugar = for {
      x <- List(1, 2)
      y <- List(3, 4)
    } yield x * y
    val desugared = List(1, 2).flatMap(x => List(3, 4).map(y => x * y))
    println(sugar == desugared)
  }
}

Mixing generators and guards

You can interleave generators and guards freely. A guard placed between two generators prunes outer values before the inner loop even runs.

object Main {
  def main(args: Array[String]): Unit = {
    val result = for {
      x <- 1 to 5
      if x % 2 == 1
      y <- 1 to 5
      if y > x
    } yield (x, y)
    println(result)
  }
}

Flattening nested collections

When you have a collection of collections, two generators flatten it naturally: the first iterates the outer, the second iterates each inner.

object Main {
  def main(args: Array[String]): Unit = {
    val matrix = List(List(1, 2), List(3, 4), List(5, 6))
    val flat = for {
      row <- matrix
      value <- row
    } yield value
    println(flat)
  }
}

Building a string grid

Multiple generators are great for generating structured output, like coordinates or a multiplication table.

object Main {
  def main(args: Array[String]): Unit = {
    val table = for {
      i <- 1 to 3
      j <- 1 to 3
    } yield s"$i*$j=${i * j}"
    println(table.grouped(3).map(_.mkString("  ")).mkString("\n"))
  }
}

Combining different collection sizes

Generators need not be the same length. Each combination of one element from each is produced.

object Main {
  def main(args: Array[String]): Unit = {
    val sizes = List("S", "M", "L")
    val colors = List("red", "blue")
    val products = for {
      s <- sizes
      c <- colors
    } yield s"$c-$s"
    println(products)
  }
}

Order affects the output sequence

Swapping generator order keeps the same set of combinations but changes their order, because the last generator varies fastest.

object Main {
  def main(args: Array[String]): Unit = {
    val ab = for { x <- List(1, 2); y <- List(9, 8) } yield (x, y)
    val ba = for { y <- List(9, 8); x <- List(1, 2) } yield (x, y)
    println(ab)
    println(ba)
  }
}

Quick Check

How many tuples does for { x <- List(1,2,3); y <- List('a','b') } yield (x, y) produce?

Recap

You learned about multiple generators:

  • Each <- line adds a level of nested iteration
  • Together they produce the cartesian product
  • Inner generators can depend on outer bound values
  • They desugar to nested flatMap + a final map
  • Generator order changes the result ordering

Sıkça Sorulan Sorular

“Birden Çok Üretici” dersi ücretsiz mi?

Evet — “Birden Çok Üretici” 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.

“Birden Çok Üretici” dersinde ne öğreneceğim?

İç içe yineleme 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.

“Birden Çok Üretici” 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

  1. for Yapısını Açma
  2. if ile Süzme
  3. Birden Çok Üretici
  4. Option ve Future ile Kullanım
← Scala for Backend Engineering & Functional Programming Sayfasına Dön