当前位置:网站首页>[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
边栏推荐
- this+闭包+作用域 面试题
- 2. Common request methods
- Avoid material "minefields"! Play with super high conversion rate
- 打破信息茧房-我主动获取信息的方法 -#3
- 問下,這個ADB mysql支持sqlserver嗎?
- Cette ADB MySQL prend - elle en charge SQL Server?
- Sqoop installation
- Avoid material "minefields"! Play with super high conversion rate
- Devtools的簡單使用
- LeetCode 234. Palindrome linked list
猜你喜欢
SQL performance optimization skills
Design and practice of kubernetes cluster and application monitoring scheme
Share the newly released web application development framework based on blazor Technology
Master Fur
腾讯云,实现图片上传
this+闭包+作用域 面试题
Design and implementation of kindergarten management system
Tiny series rendering tutorial
IPv6 experiment
Problem solving: attributeerror: 'nonetype' object has no attribute 'append‘
随机推荐
Devtools的簡單使用
FBO and RBO disappeared in webgpu
Acwing第 58 场周赛【完结】
Six stone programming: advantages of automated testing
How to make OS X read bash_ Profile instead of Profile file - how to make OS X to read bash_ profile not . profile file
Design and implementation of kindergarten management system
Clean up PHP session files
GFS distributed file system
Single box check box
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
有个疑问 flink sql cdc 的话可以设置并行度么, 并行度大于1会有顺序问题吧?
Structure of ViewModel
Logstash、Fluentd、Fluent Bit、Vector? How to choose the appropriate open source log collector
El tree whether leaf node or not, the drop-down button is permanent
单项框 复选框
Class inheritance in C #
Linux安装Redis
Design and implementation of community hospital information system
Basic authorization command for Curl
Ask, does this ADB MySQL support sqlserver?