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

闭包的柯里化与组合

深入理解闭包:使用 curry 固定参数、组合函数,并利用记忆化构建可复用的函数式流水线。

闭包的柯里化与组合 是 CoddyKit 上的免费 Groovy & Gradle: JVM Automation and Build Engineering 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Groovy & Gradle: JVM Automation and Build Engineering 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Groovy & Gradle: JVM Automation and Build Engineering 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

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

常见问题解答

「闭包的柯里化与组合」课时是免费的吗?

是的 — 「闭包的柯里化与组合」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Groovy & Gradle: JVM Automation and Build Engineering 课程的其余内容,请升级到 CoddyKit PRO。 Groovy & Gradle: JVM Automation and Build Engineering 课程共包含 4 节课。

「闭包的柯里化与组合」这节课中我会学到什么?

深入理解闭包:使用 curry 固定参数、组合函数,并利用记忆化构建可复用的函数式流水线。 你通过在浏览器中直接运行的动手代码来练习 Groovy & Gradle: JVM Automation and Build Engineering,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Groovy & Gradle: JVM Automation and Build Engineering 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Groovy & Gradle: JVM Automation and Build Engineering 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「闭包的柯里化与组合」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Groovy & Gradle: JVM Automation and Build Engineering 课中编写并运行代码吗?

能。每节 Groovy & Gradle: JVM Automation and Build Engineering 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. Groovy 集合增强功能
  2. 理解 Groovy 闭包
  3. 使用 Groovy 实现函数式模式
  4. 闭包的柯里化与组合
← 返回 Groovy & Gradle: JVM Automation and Build Engineering