Currying e composizione delle closure
Approfondisca le closure: fissi gli argomenti con curry, componga le funzioni e utilizzi la memoization per costruire pipeline funzionali riutilizzabili.
Currying e composizione delle closure è una lezione Groovy & Gradle: JVM Automation and Build Engineering gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Groovy & Gradle: JVM Automation and Build Engineering, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Groovy & Gradle: JVM Automation and Build Engineering include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
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_FIRSTQuick Check
Test your closure transformation knowledge.
Recap
You learned advanced closure techniques:
curry/rcurry/ncurrypre-bind arguments>>and<<compose closures into pipelinesmemoizecaches results of pure functions- Closures stored in maps act as runtime strategies
trampolinemakes deep recursion stack-safe
Impara Groovy con un tutor IA — gratis
Scrivi ed esegui vero codice nel tuo browser, ricevi aiuto istantaneo da un tutor IA disponibile 24/7, e riprendi da dove hai lasciato sul web o nell'app.
- Corsi
- 12
- Lezioni
- 48
Domande Frequenti
La lezione «Currying e composizione delle closure» è gratuita?
Sì — il testo completo di «Currying e composizione delle closure» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Groovy & Gradle: JVM Automation and Build Engineering, passa a CoddyKit PRO. Il corso Groovy & Gradle: JVM Automation and Build Engineering include 4 lezioni in totale.
Cosa imparerò in «Currying e composizione delle closure»?
Approfondisca le closure: fissi gli argomenti con curry, componga le funzioni e utilizzi la memoization per costruire pipeline funzionali riutilizzabili. Eserciti Groovy & Gradle: JVM Automation and Build Engineering con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Groovy & Gradle: JVM Automation and Build Engineering?
Non è richiesta alcuna esperienza precedente. Groovy & Gradle: JVM Automation and Build Engineering su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.
Quanto tempo richiede la lezione «Currying e composizione delle closure»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Groovy & Gradle: JVM Automation and Build Engineering?
Sì. Ogni lezione Groovy & Gradle: JVM Automation and Build Engineering include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Miglioramenti alle collezioni Groovy
- Comprendere le closure di Groovy
- Pattern funzionali con Groovy
- Currying e composizione delle closure