Currying
Kısmi uygulama
Currying, 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.
What is currying?
Currying transforms a function of several arguments into a chain of functions, each taking one argument. In Scala this is written with multiple parameter lists.
object Main {
def add(a: Int)(b: Int): Int = a + b
def main(args: Array[String]): Unit = {
println(add(3)(4))
}
}Multiple parameter lists
A curried method has several parameter lists: def f(a: Int)(b: Int). You call it by supplying each list in turn: f(1)(2).
object Main {
def combine(prefix: String)(value: Int): String = s"$prefix$value"
def main(args: Array[String]): Unit = {
println(combine("id-")(42))
}
}Partial application
You can apply only some argument lists and get back a function awaiting the rest. This is called partial application.
object Main {
def add(a: Int)(b: Int): Int = a + b
def main(args: Array[String]): Unit = {
val addFive = add(5) _
println(addFive(10))
println(addFive(20))
}
}Specializing a general function
Partial application lets you build specialized functions from a general one, reducing repetition.
object Main {
def power(exp: Int)(base: Int): Int = math.pow(base, exp).toInt
def main(args: Array[String]): Unit = {
val square = power(2) _
val cube = power(3) _
println(square(5))
println(cube(2))
}
}Currying a normal function
An ordinary function value has a .curried method that converts it into curried form.
object Main {
def main(args: Array[String]): Unit = {
val add = (a: Int, b: Int) => a + b
val curriedAdd = add.curried
println(curriedAdd(3)(4))
}
}Uncurrying back
The Function.uncurried helper reverses the process, turning a curried function back into one with a single parameter list.
object Main {
def main(args: Array[String]): Unit = {
val curried: Int => Int => Int = a => b => a + b
val normal = Function.uncurried(curried)
println(normal(3, 4))
}
}Why currying is useful
Currying enables reuse: fix the first arguments once, then call the result many times with different remaining arguments. It also helps the compiler infer types in later lists.
object Main {
def discount(rate: Double)(price: Double): Double = price * (1 - rate)
def main(args: Array[String]): Unit = {
val blackFriday = discount(0.3) _
println(blackFriday(100))
println(blackFriday(250))
}
}Type inference in the last list
A practical reason for multiple parameter lists: the compiler can infer the lambda's parameter types from earlier lists, so you can omit them.
object Main {
def fold[A](xs: List[A])(init: A)(op: (A, A) => A): A =
xs.foldLeft(init)(op)
def main(args: Array[String]): Unit = {
val total = fold(List(1, 2, 3))(0)((a, b) => a + b)
println(total)
}
}Currying with three lists
You can have as many parameter lists as you like. Each call supplies one list and returns a function for the next.
object Main {
def make(a: Int)(b: Int)(c: Int): Int = a + b + c
def main(args: Array[String]): Unit = {
println(make(1)(2)(3))
val step = make(10) _
println(step(20)(30))
}
}Building configurable validators
A common real use: curry a validation rule, fix the threshold, and reuse the resulting predicate across a dataset.
object Main {
def atLeast(min: Int)(value: Int): Boolean = value >= min
def main(args: Array[String]): Unit = {
val adult = atLeast(18) _
println(List(15, 21, 18, 12).map(adult))
}
}Currying vs partial application
Currying is the structural transformation into one-argument functions. Partial application is the act of supplying some arguments now and the rest later. Curried functions make partial application natural.
object Main {
def greet(greeting: String)(name: String): String = s"$greeting, $name!"
def main(args: Array[String]): Unit = {
val sayHi = greet("Hi") _
println(sayHi("Ann"))
println(sayHi("Bob"))
}
}Quick Check
How do you define a curried method in Scala?
Recap
You learned currying and partial application:
- Curried methods use multiple parameter lists:
f(a)(b) - Partially apply with
f(x) _to get a new function .curriedandFunction.uncurriedconvert forms- Helps reuse, specialization, and type inference
Sıkça Sorulan Sorular
“Currying” dersi ücretsiz mi?
Evet — “Currying” 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.
“Currying” dersinde ne öğreneceğim?
Kısmi uygulama 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.
“Currying” 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.