0Pricing
Groovy & Gradle: JVM Automation and Build Engineering · Lektion

Currying und Komposition von Closures

Gehen Sie tiefer in die Arbeit mit Closures ein: Fixieren Sie Argumente mit curry, komponieren Sie Funktionen und verwenden Sie Memoization für wiederverwendbare funktionale Pipelines.

Currying und Komposition von Closures ist eine kostenlose Groovy & Gradle: JVM Automation and Build Engineering-Lektion auf CoddyKit. Dies ist Lektion 4 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 Groovy & Gradle: JVM Automation and Build Engineering-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Groovy & Gradle: JVM Automation and Build Engineering-Kurs umfasst insgesamt 4 Lektionen.

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

Beyond Basic Closures

You already know closures as anonymous blocks of code. Groovy also lets you transform closures themselves — fixing arguments, chaining them, and caching results — for powerful functional patterns.

What Is Currying?

Currying creates a new closure with some arguments pre-filled. The result takes the remaining arguments. This turns a general function into a specialized one.

Using curry()

Call curry to bind the leftmost arguments.

def add = { a, b -> a + b }
def add5 = add.curry(5)
println add5(10)

Right and nth Curry

Use rcurry to fix the rightmost argument and ncurry to fix one at a specific index.

def power = { base, exp -> base ** exp }
def square = power.rcurry(2)
println square(9)

Composing Closures

The >> and << operators compose closures so the output of one feeds the next, forming a pipeline.

def doubleIt = { it * 2 }
def inc = { it + 1 }
def pipe = doubleIt >> inc
println pipe(5)

Order of Composition

f >> g means apply f first then g, while f << g applies g first. Pick the direction that reads naturally.

def pipe2 = doubleIt << inc
println pipe2(5)

Memoization

memoize wraps a closure so repeated calls with the same arguments return a cached result instead of recomputing — ideal for expensive pure functions.

def slow = { n -> Thread.sleep(0); n * n }.memoize()
println slow(4)
println slow(4)

Bounded Memoization

Variants like memoizeAtMost and memoizeAtLeast cap cache size so memory stays bounded.

def f = { it + 1 }.memoizeAtMost(100)

Closures as Strategy Objects

Because closures are first-class, you can store them in maps and select behaviour at runtime — a clean replacement for big switch statements.

def ops = [add: { a, b -> a + b }, mul: { a, b -> a * b }]
println ops.add(2, 3)

Trampolining Recursion

For deep recursion, trampoline converts recursive closure calls into a loop, avoiding stack overflow.

def fact
fact = { n, acc = 1 -> n <= 1 ? acc : fact.trampoline(n - 1, acc * n) }.trampoline()
println fact(5)

Controlling the Delegate

A closure resolves unqualified names against its delegate. Setting resolveStrategy to DELEGATE_FIRST lets you build DSL blocks where calls target a configured object — the basis of builders.

def c = { name "Alice"; age 30 }
c.resolveStrategy = Closure.DELEGATE_FIRST

Quick Check

Test your closure transformation knowledge.

Recap

You learned advanced closure techniques:

  • curry/rcurry/ncurry pre-bind arguments
  • >> and << compose closures into pipelines
  • memoize caches results of pure functions
  • Closures stored in maps act as runtime strategies
  • trampoline makes deep recursion stack-safe

Häufig gestellte Fragen

Ist die Lektion „Currying und Komposition von Closures“ kostenlos?

Ja — der vollständige Text von „Currying und Komposition von Closures“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Groovy & Gradle: JVM Automation and Build Engineering-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Groovy & Gradle: JVM Automation and Build Engineering-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Currying und Komposition von Closures“?

Gehen Sie tiefer in die Arbeit mit Closures ein: Fixieren Sie Argumente mit curry, komponieren Sie Funktionen und verwenden Sie Memoization für wiederverwendbare funktionale Pipelines. Du übst Groovy & Gradle: JVM Automation and Build Engineering 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 Groovy & Gradle: JVM Automation and Build Engineering zu starten?

Keine Vorkenntnisse erforderlich. Groovy & Gradle: JVM Automation and Build Engineering 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 4 von 4.

Wie lange dauert die Lektion „Currying und Komposition von Closures“?

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 Groovy & Gradle: JVM Automation and Build Engineering-Lektion Code schreiben und ausführen?

Ja. Jede Groovy & Gradle: JVM Automation and Build Engineering-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. Erweiterungen für Groovy-Collections
  2. Groovy-Closures verstehen
  3. Funktionale Muster mit Groovy
  4. Currying und Komposition von Closures
← Zurück zu Groovy & Gradle: JVM Automation and Build Engineering