0Pricing
Groovy & Gradle: JVM Automation and Build Engineering · レッスン

カリー化とクロージャの合成

クロージャをさらに深く学びます。curryで引数を固定し、関数を合成し、メモ化を使って再利用可能な関数型パイプラインを構築します。

「カリー化とクロージャの合成」はCoddyKit上の無料Groovy & Gradle: JVM Automation and Build Engineeringレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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

よくある質問

「カリー化とクロージャの合成」レッスンは無料ですか?

はい。「カリー化とクロージャの合成」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応の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を演習し、24時間対応の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に戻る