当前位置:网站首页>[groovy] closure (closure parameter binding | curry function | rcurry function | ncurry function | code example)
[groovy] closure (closure parameter binding | curry function | rcurry function | ncurry function | code example)
2022-07-05 04:40:00 【Programmer community】
List of articles
- One 、 Closure parameter binding
- 1、 Closure parameter binding curry function
- 2、 Closure parameter binding rcurry function
- 3、 Closure parameter binding ncurry function
- Two 、 Complete code example
One 、 Closure parameter binding
Closure Closure Provides curry , ncurry , rcurry Method , this
3
3
3 There are two ways to Closure Conduct Parameter binding ;
- curry function : From left to right binding Closure parameters ;
- rcurry function : From right to left binding Closure parameters ;
- ncurry function : Specify from
n
n
n Start binding with two parameters Closure parameters ;
Above
3
3
3 Both methods will Create a new closure , Need to use The new variable receives the newly created closure , The original closure variable remains unchanged ;
1、 Closure parameter binding curry function
Binding parameters from left to right ;
Closure parameter binding curry The function prototype :
/** * Bind parameters from left to right * <p> * characteristic use : * <pre class="groovyTestCase"> * def multiply = { a, b {@code ->} a * b } * def doubler = multiply.curry(2) * assert doubler(4) == 8 * </pre> * notes : Pair closure vararg Types of functions are specially handled . * If you use vararg Parameters , The whole... Will not be used vararg Array , * But use vararg The first parameter of the array , * As shown in the following example : * <pre class="groovyTestCase"> * def a = { one, two, Object[] others {@code ->} one + two + others.sum() } * assert a.parameterTypes.name == ['java.lang.Object', 'java.lang.Object', '[Ljava.lang.Object;'] * assert a(1,2,3,4) == 10 * def b = a.curry(1) * assert b.parameterTypes.name == ['java.lang.Object', '[Ljava.lang.Object;'] * assert b(2,3,4) == 10 * def c = b.curry(2) * assert c.parameterTypes.name == ['[Ljava.lang.Object;'] * assert c(3,4) == 10 * def d = c.curry(3) * assert d.parameterTypes.name == ['[Ljava.lang.Object;'] * assert d(4) == 10 * def e = d.curry(4) * assert e.parameterTypes.name == ['[Ljava.lang.Object;'] * assert e() == 10 * assert e(5) == 15 * </pre> * * * @param arguments Closure parameters to bind * @return Return a new closure with bound parameters */ public Closure<V> curry(final Object... arguments) {
return new CurriedClosure<V>(this, arguments); }
Code example :
// Define closure variables , Declare two parameters a, b // And specify the default value for the closure def closure5 = {
a = 0, b = "Groovy" -> println "${a} : ${b}" } // Closures have default values , You can call without passing in parameters closure5() // Bind from left to right Closure parameters def closure6 = closure5.curry(1, "Gradle") // Closures have default values , You can call without passing in parameters closure6()
Execution results :
0 : Groovy1 : Gradle
2、 Closure parameter binding rcurry function
Closure parameter binding rcurry The function binds parameters from right to left , however The order of parameters is from left to right , This is something to be aware of ;
Closure parameter binding rcurry The function prototype :
/** * Bind closure parameters from right to left * According to ordinary curry() Method , Parameters are provided on the right instead of the left . * characteristic use : * <pre class="groovyTestCase"> * def divide = { a, b {@code ->} a / b } * def halver = divide.rcurry(2) * assert halver(8) == 4 * </pre> * * curried The position of the parameter will be delayed , * for example , If there are two overloaded doCall Method available , * Add... To the parameters provided curried Parameters will be connected , The results will be used for method selection . * * @param arguments Binding parameters * @return New closure after binding parameters * @see #curry(Object...) */ public Closure<V> rcurry(final Object... arguments) {
return new CurriedClosure<V>(-arguments.length, this, arguments); }
Code example :
// Define closure variables , Declare two parameters a, b // And specify the default value for the closure def closure5 = {
a = 0, b = "Groovy" -> println "${a} : ${b}" } // Closures have default values , You can call without passing in parameters closure5() // Bind from right to left Closure parameters def closure7 =closure5.rcurry(2, "Java") // Closures have default values , You can call without passing in parameters closure7()
Execution results :
0 : Groovy2 : Java
3、 Closure parameter binding ncurry function
From
n
n
n Start binding the parameters in the closure with two parameters ;
Notice that curry / ncurry / rcurry After the method , All default values are overwritten and cleared , If From
2
2
2 Start binding closure parameters with parameters , Then the first parameter has no default value , Invocation time , The first parameter must be passed in before you can only , Otherwise, the runtime will report an error ;
Closure parameter binding ncurry The function prototype :
/** * Bind closure parameters from the given index * * @param argument The closure of the parameter to be bound * @return the The newly created closure after binding parameters * @see #ncurry(int, Object...) */ public Closure<V> ncurry(int n, final Object argument) {
return ncurry(n, new Object[]{
argument}); }
Code example :
// Define closure variables , Declare two parameters a, b // And specify the default value for the closure def closure5 = {
a = 0, b = "Groovy" -> println "${a} : ${b}" } // Closures have default values , You can call without passing in parameters closure5() // From n Start binding closure parameters with parameters , // Notice that curry / ncurry / rcurry After the method , The previous default value overrides // At this time, the first parameter has no value // Invocation time , The first parameter must be passed in before you can only def closure8 =closure5.ncurry(1, "Kotlin") // The first default value of the closure is cancelled , At this point, the value of the first parameter must be passed in to execute the closure // Otherwise, the report will be wrong closure8(3)
Execution results :
0 : Groovy3 : Kotlin
Two 、 Complete code example
Complete code example :
import org.codehaus.groovy.ant.Groovyclass Test {
static void main(args) {
// I. Receive the thinning of a default parameter // Define closure variables def closure = {
println "Accept One Arguments : ${it}" } // Call closure closure.call("Hello"); closure("Hello"); // II. A closure that does not accept any parameters // Define closure variables , Parameters are not allowed to be passed in def closure2 = {
-> println "Not Accept Arguments" } // An error will be reported when the parameter is passed in //closure2("Hello") // Call closure , Cannot pass in parameters closure2.call(); closure2(); // III. Receive a closure of a custom parameter // Define closure variables , Declare a parameter a def closure3 = {
a -> println "${a}" } // Call closure , Cannot pass in parameters closure3.call(1); closure3(2); // IV. Receive the closure of two custom parameters // Define closure variables , Declare two parameters a, b // Print these two parameters in the closure def closure4 = {
a, b -> println "${a} : ${b}" } // Call closure , Cannot pass in parameters closure4.call(1, 2); closure4(3, 4); // V. Specify default values for closure parameters // Define closure variables , Declare two parameters a, b // And specify the default value for the closure def closure5 = {
a = 0, b = "Groovy" -> println "${a} : ${b}" } // Closures have default values , You can call without passing in parameters closure5() // Bind from left to right Closure parameters def closure6 = closure5.curry(1, "Gradle") // Closures have default values , You can call without passing in parameters closure6() // Bind from right to left Closure parameters def closure7 =closure5.rcurry(2, "Java") // Closures have default values , You can call without passing in parameters closure7() // From n Start binding closure parameters with parameters , // Notice that curry / ncurry / rcurry After the method , The previous default value overrides // At this time, the first parameter has no value // Invocation time , The first parameter must be passed in before you can only def closure8 =closure5.ncurry(1, "Kotlin") // The first default value of the closure is cancelled , At this point, the value of the first parameter must be passed in to execute the closure // Otherwise, the report will be wrong closure8(3) }}
Execution results :
Accept One Arguments : HelloAccept One Arguments : HelloNot Accept ArgumentsNot Accept Arguments121 : 23 : 40 : Groovy1 : Gradle2 : Java3 : Kotlin
边栏推荐
- 美国5G Open RAN再遭重大挫败,抗衡中国5G技术的图谋已告失败
- Components in protective circuit
- [PCL self study: feature9] global aligned spatial distribution (GASD) descriptor (continuously updated)
- Flutter 小技巧之 ListView 和 PageView 的各种花式嵌套
- Serpentine matrix
- 49 pictures and 26 questions explain in detail what is WiFi?
- Leetcode hot topic Hot 100 day 33: "subset"
- [phantom engine UE] only six steps are needed to realize the deployment of ue5 pixel stream and avoid detours! (the principles of 4.26 and 4.27 are similar)
- Fonction (sujette aux erreurs)
- [phantom engine UE] the difference between running and starting, and the analysis of common problems
猜你喜欢
[goweb development] Introduction to authentication modes based on cookies, sessions and JWT tokens
Key review route of probability theory and mathematical statistics examination
[Business Research Report] top ten trends of science and technology and it in 2022 - with download link
Official announcement! The third cloud native programming challenge is officially launched!
TPG x AIDU|AI领军人才招募计划进行中!
CUDA Programming atomic operation atomicadd reports error err:msb3721, return code 1
10 programming habits that web developers should develop
Flutter 小技巧之 ListView 和 PageView 的各种花式嵌套
3 minutes learn to create Google account and email detailed tutorial!
[PCL self study: feature9] global aligned spatial distribution (GASD) descriptor (continuously updated)
随机推荐
Invalid bound statement (not found) in idea -- problem solving
[uniapp] system hot update implementation ideas
Function overloading
MacBook installation postgresql+postgis
Decimal to hexadecimal
概率论与数理统计考试重点复习路线
Neural networks and deep learning Chapter 4: feedforward neural networks reading questions
计组笔记(1)——校验码、原补码乘除计算、浮点数计算
如何进行「小步重构」?
Leetcode hot topic Hot 100 day 33: "subset"
[crampon programming] lintcode decoding Encyclopedia - 1100 strange printer
Web开发人员应该养成的10个编程习惯
Learning MVVM notes (1)
可观测|时序数据降采样在Prometheus实践复盘
Decryption function calculates "task state and lifecycle management" of asynchronous task capability
Neural networks and deep learning Chapter 6: Circular neural networks reading questions
After the deployment of web resources, the navigator cannot obtain the solution of mediadevices instance (navigator.mediadevices is undefined)
Here comes the Lantern Festival red envelope!
Uncover the seven quirky brain circuits necessary for technology leaders
Qt蓝牙:搜索蓝牙设备的类——QBluetoothDeviceDiscoveryAgent