تطبيق Currying وتركيب Closures
تعمّق في استخدام Closures: ثبّت المعاملات باستخدام curry، وركّب الدوال، واستخدم الحفظ التذكّري لبناء مسارات وظيفية قابلة لإعادة الاستخدام.
تطبيق Currying وتركيب Closures درس مجاني في Groovy & Gradle: JVM Automation and Build Engineering على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في 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_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
الأسئلة الشائعة
هل درس «تطبيق Currying وتركيب Closures» مجاني؟
نعم — نص درس «تطبيق Currying وتركيب Closures» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Groovy & Gradle: JVM Automation and Build Engineering، انتقل إلى CoddyKit PRO. تتضمن دورة Groovy & Gradle: JVM Automation and Build Engineering 4 دروس في المجموع.
ماذا ستتعلم في «تطبيق Currying وتركيب Closures»؟
تعمّق في استخدام Closures: ثبّت المعاملات باستخدام curry، وركّب الدوال، واستخدم الحفظ التذكّري لبناء مسارات وظيفية قابلة لإعادة الاستخدام. تتمرن على Groovy & Gradle: JVM Automation and Build Engineering مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Groovy & Gradle: JVM Automation and Build Engineering؟
لا تُشترط خبرة سابقة. Groovy & Gradle: JVM Automation and Build Engineering على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «تطبيق Currying وتركيب Closures»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Groovy & Gradle: JVM Automation and Build Engineering هذا؟
نعم. كل درس في Groovy & Gradle: JVM Automation and Build Engineering يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- تحسينات مجموعات Groovy
- فهم Closures في Groovy
- الأنماط الوظيفية باستخدام Groovy
- تطبيق Currying وتركيب Closures