Dönüşümler
map, filter, flatMap
Dönüşümler, CoddyKit'te ücretsiz bir Scala for Backend Engineering & Functional Programming dersidir. Bu, 4 dersinin 2. 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.
Transforming collections
Scala collections are immutable by default. Instead of mutating them, you transform them into new collections using methods like map, filter, and flatMap.
object Main {
def main(args: Array[String]): Unit = {
val nums = List(1, 2, 3, 4)
val doubled = nums.map(_ * 2)
println(nums)
println(doubled)
}
}map: transform every element
map applies a function to each element and returns a new collection of the same size. The element type may change.
object Main {
def main(args: Array[String]): Unit = {
val words = List("scala", "rocks")
val lengths = words.map(_.length)
println(lengths)
}
}The underscore shorthand
The underscore _ is shorthand for a single function parameter. _ * 2 means x => x * 2. It keeps simple transformations compact.
object Main {
def main(args: Array[String]): Unit = {
val nums = List(10, 20, 30)
println(nums.map(_ + 1))
println(nums.map(x => x + 1))
}
}filter: keep matching elements
filter keeps only the elements for which the predicate returns true. The result may be smaller than the original.
object Main {
def main(args: Array[String]): Unit = {
val nums = (1 to 10).toList
val evens = nums.filter(_ % 2 == 0)
println(evens)
}
}filterNot: the opposite
filterNot keeps elements where the predicate is false. It is the complement of filter.
object Main {
def main(args: Array[String]): Unit = {
val nums = List(1, 2, 3, 4, 5)
println(nums.filterNot(_ % 2 == 0))
}
}flatMap: map then flatten
flatMap applies a function that returns a collection for each element, then flattens the results into one collection.
object Main {
def main(args: Array[String]): Unit = {
val words = List("ab", "cd")
val chars = words.flatMap(_.toList)
println(chars)
}
}map vs flatMap
If your function returns a collection, map gives you nested collections while flatMap gives you a flat one. Choose based on the shape you want.
object Main {
def main(args: Array[String]): Unit = {
val nums = List(1, 2, 3)
println(nums.map(n => List(n, n)))
println(nums.flatMap(n => List(n, n)))
}
}flatMap with Option
flatMap over a list of Option drops the None values and unwraps the Some values, a handy way to filter and transform at once.
object Main {
def main(args: Array[String]): Unit = {
val raw = List("1", "x", "3", "y")
val parsed = raw.flatMap(_.toIntOption)
println(parsed)
}
}collect: filter and map together
collect takes a partial function: it keeps and transforms only the elements the function is defined for, combining filter and map in one pass.
object Main {
def main(args: Array[String]): Unit = {
val mixed: List[Any] = List(1, "two", 3, "four", 5)
val ints = mixed.collect { case i: Int => i * 10 }
println(ints)
}
}Chaining transformations
Because each method returns a new collection, you can chain them into a readable pipeline. Each step transforms the output of the previous one.
object Main {
def main(args: Array[String]): Unit = {
val result = List(1, 2, 3, 4)
.map(_ + 1)
.filter(_ % 2 == 0)
println(result)
}
}A clean pipeline
Here is a tidy transformation pipeline: filter even numbers, square them, then keep the large ones. Pipelines read like a description of the data flow.
object Main {
def main(args: Array[String]): Unit = {
val result = (1 to 10).toList
.filter(_ % 2 == 0)
.map(x => x * x)
.filter(_ > 10)
println(result)
}
}Quick Check
What does List("ab", "cd").flatMap(_.toList) return?
Recap
You learned the core transformations:
map— transform each element, same sizefilter/filterNot— keep or drop by predicateflatMap— map then flatten nested resultscollect— filter and map with a partial function- Chain them into readable, immutable pipelines
Sıkça Sorulan Sorular
“Dönüşümler” dersi ücretsiz mi?
Evet — “Dönüşümler” 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.
“Dönüşümler” dersinde ne öğreneceğim?
map, filter, flatMap 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 2. dersidir.
“Dönüşümler” 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.