当前位置:网站首页>[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
边栏推荐
- Sqoop安装
- Basic authorization command for Curl
- 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?
- Jd.com 2: how to prevent oversold in the deduction process of commodity inventory?
- Breaking the information cocoon - my method of actively obtaining information - 3
- qrcode:将文本生成二维码
- Pat class a 1160 forever (class B 1104 forever)
- Three line by line explanations of the source code of anchor free series network yolox (a total of ten articles, which are guaranteed to be explained line by line. After reading it, you can change the
- 问下,这个ADB mysql支持sqlserver吗?
- Une question est de savoir si Flink SQL CDC peut définir le parallélisme. Si le parallélisme est supérieur à 1, il y aura un problème d'ordre?
猜你喜欢
ELK日志分析系统
Flume configuration 4 - customize mysqlsource
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
Talk about the SQL server version of DTM sub transaction barrier function
Huawei MPLS experiment
Idea inheritance relationship
Voice chip wt2003h4 B008 single chip to realize the quick design of intelligent doorbell scheme
Pat class a 1160 forever (class B 1104 forever)
Leetcode42. connect rainwater
Multi person online anonymous chat room / private chat room source code / support the creation of multiple chat rooms at the same time
随机推荐
Multi person online anonymous chat room / private chat room source code / support the creation of multiple chat rooms at the same time
Asp+access campus network goods trading platform
1.五层网络模型
Acwing第 58 场周赛【完结】
Last words record
Zabbix
d3js小记
el-select,el-option下拉选择框
How to learn to get the embedding matrix e # yyds dry goods inventory #
This + closure + scope interview question
8. Commodity management - commodity classification
Asemi rectifier bridge 2w10 parameters, 2w10 specifications, 2w10 characteristics
VM in-depth learning (XXV) -class file overview
ASP. Net core 6 framework unveiling example demonstration [01]: initial programming experience
SPI and IIC communication protocol
001 chip test
Azkaban overview
Cut! 39 year old Ali P9, saved 150million
2021 Li Hongyi machine learning (1): basic concepts
Performance of calling delegates vs methods