当前位置:网站首页>[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
边栏推荐
- The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
- Yyds dry goods inventory embedded matrix
- 51 independent key basic experiment
- IPv6 experiment
- Design and implementation of high availability website architecture
- The database and recharge are gone
- When the low alcohol race track enters the reshuffle period, how can the new brand break the three major problems?
- [200 opencv routines] 99 Modified alpha mean filter
- Structure of ViewModel
- Mongodb common commands
猜你喜欢
VM in-depth learning (XXV) -class file overview
Pdf things
Master Fur
Asemi rectifier bridge 2w10 parameters, 2w10 specifications, 2w10 characteristics
2021 Li Hongyi machine learning (1): basic concepts
Apache build web host
Why are there fewer and fewer good products produced by big Internet companies such as Tencent and Alibaba?
Azkaban actual combat
Hot knowledge of multithreading (I): introduction to ThreadLocal and underlying principles
Single box check box
随机推荐
Six stone programming: advantages of automated testing
为什么腾讯阿里等互联网大厂诞生的好产品越来越少?
Pdf things
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
There is a question about whether the parallelism can be set for Flink SQL CDC. If the parallelism is greater than 1, will there be a sequence problem?
SFTP cannot connect to the server # yyds dry goods inventory #
Elfk deployment
Comparison of advantages and disadvantages between platform entry and independent deployment
Acwing game 58 [End]
This + closure + scope interview question
Returns the lowest common ancestor of two nodes in a binary tree
SPI and IIC communication protocol
Design and practice of kubernetes cluster and application monitoring scheme
Watch the online press conference of tdengine community heroes and listen to TD hero talk about the legend of developers
51 independent key basic experiment
El tree whether leaf node or not, the drop-down button is permanent
Anchor free series network yolox source code line by line explanation four (a total of ten, ensure line by line explanation, after reading, you can change the network at will, not just as a participan
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
2021 Li Hongyi machine learning (2): pytorch
Pat class a 1162 postfix expression