Currying & Composing Closures
Go deeper with closures: fix arguments using curry, compose functions, and use memoization to build reusable functional pipelines.
Currying & Composing Closures is a free Groovy & Gradle: JVM Automation and Build Engineering lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Groovy & Gradle: JVM Automation and Build Engineering learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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
Frequently asked questions
Is the “Currying & Composing Closures” lesson free?
Yes — the full text of “Currying & Composing Closures” is free to read here on the web, and the Groovy & Gradle: JVM Automation and Build Engineering course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Groovy & Gradle: JVM Automation and Build Engineering course, upgrade to CoddyKit PRO.
What will I learn in “Currying & Composing Closures”?
Go deeper with closures: fix arguments using curry, compose functions, and use memoization to build reusable functional pipelines. You practise Groovy & Gradle: JVM Automation and Build Engineering with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Groovy & Gradle: JVM Automation and Build Engineering?
No prior experience is required. Groovy & Gradle: JVM Automation and Build Engineering on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Currying & Composing Closures” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Groovy & Gradle: JVM Automation and Build Engineering lesson?
Yes. Every Groovy & Gradle: JVM Automation and Build Engineering lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Groovy Collections Enhancements
- Understanding Groovy Closures
- Functional Patterns with Groovy
- Currying & Composing Closures