Function 为什么这些等价函数会产生不同的结果?
这是最初的方法,我的目的是抽象掉代码的许多细节,通过将细节放在执行相同操作的函数中,提高可读性,但使用更可读的名称 使用第一种方法,我实现了所需的行为,行[x]被正确地添加到行中Function 为什么这些等价函数会产生不同的结果?,function,loops,groovy,while-loop,equivalent,Function,Loops,Groovy,While Loop,Equivalent,这是最初的方法,我的目的是抽象掉代码的许多细节,通过将细节放在执行相同操作的函数中,提高可读性,但使用更可读的名称 使用第一种方法,我实现了所需的行为,行[x]被正确地添加到行中 def getAllRowsForLine( rows, index ) { def lineRows = [rows[index]] def newOperatorNotFound def x = index + 1 if ( x <= rows.size() - 1 ) {
def getAllRowsForLine( rows, index ) {
def lineRows = [rows[index]]
def newOperatorNotFound
def x = index + 1
if ( x <= rows.size() - 1 ) {
newOperatorNotFound = true
while ( x <= ( rows.size() - 1 ) && newOperatorNotFound ) {
if ( rows[x].PGM_PROC_OPE.trim() == "" ) {
lineRows << rows[x]
} else if ( rows[x].PGM_PROC_TY == "AN" || rows[x].PGM_PROC_TY == "OR" ) {
lineRows << rows[x]
}
else {
newOperatorNotFound = false
}
x++
}
}
return lineRows
}
哎呀,我意识到我在operatorEmpty函数中使用了索引而不是I。如果我将索引更改为I,函数将按预期执行。是否可以删除该问题?
def getAllRowsForLine2( rows, index ) {
def lineRows = [rows[index]]
def newOperatorNotFound
def i = index + 1
if ( moreRows( rows, i ) ) {
newOperatorNotFound = true
while ( moreRows( rows, i ) && newOperatorNotFound ) {
if ( operatorEmpty( rows, index ) ) {
lineRows << rows[i]
}
else if ( procTypeAnd( rows, i ) || procTypeOr( rows, i ) ) {
lineRows << rows[i]
} else {
newOperatorNotFound = false
}
i++
}
}
return lineRows
}
def operatorEmpty( rows, index ) {
return rows[index].PGM_PROC_OPE.trim() == ""
}
def procTypeAnd( rows, index ) {
return rows[index].PGM_PROC_TY == "AN"
}
def procTypeOr( rows, index ) {
return rows[index].PGM_PROC_TY == "OR"
}
def moreRows( rows, index ) {
return index <= ( rows.size() - 1 )
}
println lineProcessor.getAllRowsForLine( rows, 0 ) == lineProcessor.getAllRowsForLine2( rows, 0 )
=> true