当前位置:网站首页>[groovy] loop control (number injection function implements loop | times function | upto function | downto function | step function | closure can be written outside as the final parameter)

[groovy] loop control (number injection function implements loop | times function | upto function | downto function | step function | closure can be written outside as the final parameter)

2022-07-05 03:12:00 Programmer community

List of articles

  • Preface
  • One 、times Circular function
  • Two 、upto Circular function
  • 3、 ... and 、downto Circular function
  • Four 、step Circular function
    • 1、step Loop function increment operation
    • 2、step Loop function decrement operation
  • 5、 ... and 、 Rules for using closures as parameters
    • 1、 Closures can be written outside parentheses as the last argument
    • 2、 Function parameter parentheses can be omitted 、 Parameters are separated by commas
  • 6、 ... and 、 Complete code example

Preface

Groovy by Number Class to implement the injection function , It can also realize circulation , By passing closure parameters to the injected function , Then the circular operation can be realized ;

One 、times Circular function


Number The injection function of : stay times Function , Incoming closure , In the closure is the loop content ;

    /** *  Execute closures multiple times from scratch . Pass the current index to the closure every time . * Example: * <pre>10.times { * println it * }</pre> * Prints the numbers 0 through 9. * * @param self a Number * @param closure  Closures need to be called multiple times  * @since 1.0 */    public static void times(Number self, @ClosureParams(value=SimpleType.class,options="int")  Closure closure) 

Code example :

        //  loop  10  Time  ,  Get here of the current loop each time  ,  Value  0 ~ 9        // Groovy  towards  Number  Class  times  Method         println ""        print "( 7 ) : "        10.times { 
                // Integer it  Is the number of cycles each time             print it + " "        }

Execution results :

( 7 ) : 0 1 2 3 4 5 6 7 8 9 

Two 、upto Circular function


upto Circular function : Pass in a greater than Number The numerical , Self increasing cycle ;

    /** *  Iterate from this number to a given number ( contain ), Increment one at a time . * * @param self a Number * @param to another Number to go up to * @param closure the closure to call * @since 1.0 */    public static void upto(Number self, Number to, @ClosureParams(FirstParam.class) Closure closure) 

Code example :

        // Groovy  towards  Number  Class  upto  Method         println ""        print "( 8 ) : "        10.upto(20, { 
                // Integer it  Is the number of cycles each time             print it + " "        })

Execution results :

( 8 ) : 10 11 12 13 14 15 16 17 18 19 20 

3、 ... and 、downto Circular function


downto Circular function : Pass in a less than Number The numerical , Self decreasing cycle ;

    /** *  Iterate from this number to a given number , Decrease one at a time . * * @param self a Number * @param to another Number to go down to * @param closure the closure to call * @since 1.0 */    public static void downto(Number self, Number to, @ClosureParams(FirstParam.class) Closure closure) 

Code example :

        // Groovy  towards  Number  Class  downto  Method         println ""        print "( 9 ) : "        20.downto(10, { 
                // Integer it  Is the number of cycles each time             print it + " "        })

Execution results :

( 9 ) : 20 19 18 17 16 15 14 13 12 11 10 

Four 、step Circular function


step Circular function : Pass in a value to , With stepNumber Step by step ;

    /** *  Use step increments to iterate from this number to a given number . Each intermediate number is passed to a given closure . Example : * <pre>0.step( 10, 2 ) { * println it * }</pre> * Prints even numbers 0 through 8. * * @param self a Number to start with * @param to a Number to go up to, exclusive * @param stepNumber a Number representing the step increment * @param closure the closure to call * @since 1.0 */    public static void step(Number self, Number to, Number stepNumber, Closure closure)

1、step Loop function increment operation

Code example :

        // Groovy  towards  Number  Class  step  Method         println ""        print "( 10 ) : "        10.step(30, 2, { 
                // Integer it  Is the number of cycles each time             print it + " "        })

Execution results :

( 10 ) : 10 12 14 16 18 20 22 24 26 28 

2、step Loop function decrement operation

Code example :

        //  Decrement operation can also         println ""        print "( 13 ) : "        10.step(0, -2) { 
                // Integer it  Is the number of cycles each time             print it + " "        }

Execution results :

( 13 ) : 10 8 6 4 2 

5、 ... and 、 Rules for using closures as parameters


1、 Closures can be written outside parentheses as the last argument

Code example :

        //  If you call a function  ,  The last element of the function argument is the closure  ,  You can write the closure on the outside         println ""        print "( 11 ) : "        10.step(30, 2) { 
                // Integer it  Is the number of cycles each time             print it + " "        }

Execution results :

( 11 ) : 10 12 14 16 18 20 22 24 26 28 

2、 Function parameter parentheses can be omitted 、 Parameters are separated by commas

Code example :

        //  If you call a function  ,  The last element of the function argument is the closure  ,  You can write the closure on the outside         //  Brackets can also be removed  ,  However, the three parameters need to be separated by commas         println ""        print "( 12 ) : "        10.step 30, 2, { 
                // Integer it  Is the number of cycles each time             print it + " "        }

Execution results :

( 12 ) : 10 12 14 16 18 20 22 24 26 28 

6、 ... and 、 Complete code example


class Test { 
        static void main(args) { 
            // Java  Syntax style loop         println ""        print "( 0 ) : "        for (int j = 0; j <= 9; j++) { 
                print j + " "        }        // Groovy  loop  , 0 ~ 9  Cycle through         println ""        print "( 1 ) : "        for (i in new IntRange(0, 9)) { 
                print i + " "        }        // Groovy  loop  , 0 ~ 9  Cycle through         println ""        print "( 2 ) : "        for (i in new IntRange(0, 9, false)) { 
                print i + " "        }        // Groovy  loop  , 9 ~ 0  Cycle through         println ""        print "( 3 ) : "        for (i in new IntRange(0, 9, true)) { 
                print i + " "        }        // Groovy  loop  , 0 ~ 9  Cycle through  ,  Not including the last  to  Elements  ,  namely  9        //  Only print out  0 ~ 8  The number of         println ""        print "( 4 ) : "        for (i in new IntRange(false, 0, 9)) { 
                print i + " "        }        // Groovy  loop  , 0 ~ 9  Cycle through  ,  Contains the last  to  Elements  ,  namely  9        //  Only print out  0 ~ 9  The number of         println ""        print "( 5 ) : "        for (i in new IntRange(true, 0, 9)) { 
                print i + " "        }        // Groovy  loop  , 0 ~ 9  Cycle through         println ""        print "( 6 ) : "        for (i in 0..9) { 
                print i + " "        }        //  among  0..9  amount to  new IntRange(0, 9)        def range = 0..9        println ""        print "0..9 type : "        println range.class        //  loop  10  Time  ,  Get here of the current loop each time  ,  Value  0 ~ 9        // Groovy  towards  Number  Class  times  Method         println ""        print "( 7 ) : "        10.times { 
                // Integer it  Is the number of cycles each time             print it + " "        }        // Groovy  towards  Number  Class  upto  Method         println ""        print "( 8 ) : "        10.upto(20, { 
                // Integer it  Is the number of cycles each time             print it + " "        })        // Groovy  towards  Number  Class  downto  Method         println ""        print "( 9 ) : "        20.downto(10, { 
                // Integer it  Is the number of cycles each time             print it + " "        })        // Groovy  towards  Number  Class  downto  Method         println ""        print "( 9 ) : "        20.downto(10, { 
                // Integer it  Is the number of cycles each time             print it + " "        })        // Groovy  towards  Number  Class  step  Method         println ""        print "( 10 ) : "        10.step(30, 2, { 
                // Integer it  Is the number of cycles each time             print it + " "        })        //  If you call a function  ,  The last element of the function argument is the closure  ,  You can write the closure on the outside         println ""        print "( 11 ) : "        10.step(30, 2) { 
                // Integer it  Is the number of cycles each time             print it + " "        }        //  If you call a function  ,  The last element of the function argument is the closure  ,  You can write the closure on the outside         //  Brackets can also be removed  ,  However, the three parameters need to be separated by commas         println ""        print "( 12 ) : "        10.step 30, 2, { 
                // Integer it  Is the number of cycles each time             print it + " "        }        //  Decrement operation can also         println ""        print "( 13 ) : "        10.step(0, -2) { 
                // Integer it  Is the number of cycles each time             print it + " "        }                println ""    }}

Execution results :

( 0 ) : 0 1 2 3 4 5 6 7 8 9 ( 1 ) : 0 1 2 3 4 5 6 7 8 9 ( 2 ) : 0 1 2 3 4 5 6 7 8 9 ( 3 ) : 9 8 7 6 5 4 3 2 1 0 ( 4 ) : 0 1 2 3 4 5 6 7 8 ( 5 ) : 0 1 2 3 4 5 6 7 8 9 ( 6 ) : 0 1 2 3 4 5 6 7 8 9 0..9 type : class groovy.lang.IntRange( 7 ) : 0 1 2 3 4 5 6 7 8 9 ( 8 ) : 10 11 12 13 14 15 16 17 18 19 20 ( 9 ) : 20 19 18 17 16 15 14 13 12 11 10 ( 9 ) : 20 19 18 17 16 15 14 13 12 11 10 ( 10 ) : 10 12 14 16 18 20 22 24 26 28 ( 11 ) : 10 12 14 16 18 20 22 24 26 28 ( 12 ) : 10 12 14 16 18 20 22 24 26 28 ( 13 ) : 10 8 6 4 2 

 Insert picture description here

原网站

版权声明
本文为[Programmer community]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140806222406.html