Curryfication
Application partielle
Curryfication est une leçon Scala for Backend Engineering & Functional Programming gratuite sur CoddyKit. Ceci est la leçon 2 sur 4. Tu peux lire la leçon complète ci-dessous gratuitement — puis la pratiquer en direct dans le navigateur avec un éditeur de code intégré et un tuteur IA 24/7. Elle fait partie du parcours d'apprentissage Scala for Backend Engineering & Functional Programming, et ta progression se synchronise sur le web et l'application CoddyKit. Le cours Scala for Backend Engineering & Functional Programming comprend 4 leçons au total.
Certaines parties de cette leçon n'ont pas encore été traduites et s'affichent en anglais.
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
Questions Fréquemment Posées
La leçon « Curryfication » est-elle gratuite ?
Oui — le texte complet de « Curryfication » est gratuit à lire ici sur le web. Pour la pratiquer de manière interactive (un éditeur de code intégré et un tuteur IA 24/7) et déverrouiller le reste du cours Scala for Backend Engineering & Functional Programming, passe à CoddyKit PRO. Le cours Scala for Backend Engineering & Functional Programming comprend 4 leçons au total.
Qu'est-ce que j'apprendrai dans « Curryfication » ?
Application partielle Tu pratiques Scala for Backend Engineering & Functional Programming avec du code pratique que tu exécutes directement dans le navigateur, et un tuteur IA 24/7 répond à tes questions au fur et à mesure que tu avances dans la leçon.
Dois-je avoir de l'expérience pour commencer Scala for Backend Engineering & Functional Programming ?
Aucune expérience préalable n'est requise. Scala for Backend Engineering & Functional Programming sur CoddyKit est structuré pour les débutants jusqu'aux apprenants avancés, donc tu peux commencer ici ou depuis le début et avancer à ton rythme. Ceci est la leçon 2 sur 4.
Combien de temps prend la leçon « Curryfication » ?
La plupart des leçons CoddyKit prennent environ 5–10 minutes. Chacune est courte et interactive, tu progresses régulièrement et tu repiques exactement où tu t'es arrêté sur le web et l'app.
Peux-tu écrire et exécuter du code dans cette leçon Scala for Backend Engineering & Functional Programming ?
Oui. Chaque leçon Scala for Backend Engineering & Functional Programming inclut un éditeur de code intégré, tu écris et exécutes du vrai code directement dans ton navigateur et tu reçois des retours IA instantanés — aucune configuration locale requise.
Toutes les leçons de ce cours
- Fonctions comme valeurs
- Curryfication
- Composition
- Fermetures