Currying i komponowanie closures
Pogłębiaj pracę z closures: ustalaj argumenty za pomocą curry, komponuj funkcje i używaj memoizacji do budowania wielokrotnego użytku potoków funkcyjnych.
Currying i komponowanie closures to bezpłatna lekcja Groovy & Gradle: JVM Automation and Build Engineering na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Groovy & Gradle: JVM Automation and Build Engineering, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Groovy & Gradle: JVM Automation and Build Engineering zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
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
Często zadawane pytania
Czy lekcja „Currying i komponowanie closures” jest bezpłatna?
Tak — pełny tekst „Currying i komponowanie closures” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Groovy & Gradle: JVM Automation and Build Engineering, przejdź na CoddyKit PRO. Kurs Groovy & Gradle: JVM Automation and Build Engineering zawiera 4 lekcji w sumie.
Co nauczysz się w „Currying i komponowanie closures”?
Pogłębiaj pracę z closures: ustalaj argumenty za pomocą curry, komponuj funkcje i używaj memoizacji do budowania wielokrotnego użytku potoków funkcyjnych. Ćwiczysz Groovy & Gradle: JVM Automation and Build Engineering z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć Groovy & Gradle: JVM Automation and Build Engineering?
Nie wymagamy żadnego doświadczenia. Groovy & Gradle: JVM Automation and Build Engineering w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.
Ile czasu zajmuje lekcja „Currying i komponowanie closures”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji Groovy & Gradle: JVM Automation and Build Engineering?
Tak. Każda lekcja Groovy & Gradle: JVM Automation and Build Engineering zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- Rozszerzenia kolekcji Groovy
- Zrozumienie domknięć Groovy
- Wzorce funkcyjne w Groovy
- Currying i komponowanie closures