当前位置:网站首页>[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 
边栏推荐
- GFS distributed file system
- 001 chip test
- Flume配置4——自定义MYSQLSource
- LeetCode 237. Delete nodes in the linked list
- LeetCode 234. Palindrome linked list
- Class inheritance in C #
- Scientific research: are women better than men?
- Is there any way to change the height of the uinavigationbar in the storyboard without using the UINavigationController?
- Pdf things
- 2.常见的请求方法
猜你喜欢

Design and implementation of kindergarten management system

Tiny series rendering tutorial
![Moco V2 literature research [self supervised learning]](/img/bd/79b7b203ea064c65d143116c9f4dd0.jpg)
Moco V2 literature research [self supervised learning]

Pytest (4) - test case execution sequence

Elk log analysis system

Acwing第 58 场周赛【完结】

Voice chip wt2003h4 B008 single chip to realize the quick design of intelligent doorbell scheme

Sqoop命令

About MySQL database connection exceptions

Pdf things
随机推荐
When the low alcohol race track enters the reshuffle period, how can the new brand break the three major problems?
Pat class a 1160 forever (class B 1104 forever)
Devtools的简单使用
Kuboard
Breaking the information cocoon - my method of actively obtaining information - 3
LeetCode --- 1071. Great common divisor of strings problem solving Report
El select, El option drop-down selection box
端口,域名,协议。
问下,这个ADB mysql支持sqlserver吗?
Use UDP to send a JPEG image, and UPD will convert it into the mat format of OpenCV after receiving it
Returns the lowest common ancestor of two nodes in a binary tree
Performance of calling delegates vs methods
Mongodb common commands
返回二叉树中两个节点的最低公共祖先
Design and implementation of kindergarten management system
Anchor free series network yolox source code line by line explanation Part 2 (a total of 10, ensure to explain line by line, after reading, you can change the network at will, not just as a participan
51 independent key basic experiment
Last words record
Watch the online press conference of tdengine community heroes and listen to TD hero talk about the legend of developers
040. (2.9) relieved