0Pricing
Scala for Backend Engineering & Functional Programming · Lektion

Currying

Partielle Anwendung

Currying ist eine kostenlose Scala for Backend Engineering & Functional Programming-Lektion auf CoddyKit. Dies ist Lektion 2 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Scala for Backend Engineering & Functional Programming-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Scala for Backend Engineering & Functional Programming-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

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
  • .curried and Function.uncurried convert forms
  • Helps reuse, specialization, and type inference

Häufig gestellte Fragen

Ist die Lektion „Currying“ kostenlos?

Ja — der vollständige Text von „Currying“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Scala for Backend Engineering & Functional Programming-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Scala for Backend Engineering & Functional Programming-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Currying“?

Partielle Anwendung Du übst Scala for Backend Engineering & Functional Programming mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Scala for Backend Engineering & Functional Programming zu starten?

Keine Vorkenntnisse erforderlich. Scala for Backend Engineering & Functional Programming auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 2 von 4.

Wie lange dauert die Lektion „Currying“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Scala for Backend Engineering & Functional Programming-Lektion Code schreiben und ausführen?

Ja. Jede Scala for Backend Engineering & Functional Programming-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Funktionen als Werte
  2. Currying
  3. Komposition
  4. Closures
← Zurück zu Scala for Backend Engineering & Functional Programming