[align=center]Groovy闭包[/align]
闭包是一种表示可执行代码块的方法。闭包也是对象,可以像方法一样传递参数。由于闭包是代码块,因此也可以在需要时执行。像方法一样,在定义的过程中,闭包也可以使用一个或者多个参数。
一、inject方法
inject方法可用于遍历集合,首先将需要传递的值和集合项目传给闭包,此时其传递的值将作为处理结果,然后再和下一个集合项目一起传给闭包,依此类推。方法原型为:
Object inject(Object value, Closure closure)
例:求5的阶乘值
结果:
Factorial(5): 120
结果:
fact: 120
结果:
Factorial(5): 120
结果:
Factorial(5): 120
由于闭包也是一个对象,因此它可以作为方法的参数。
例:闭包作为方法的参数
结果:
evens: [12, 14]
odds: [11, 13]
例:闭包作为另一个闭包的参数
结果:
evens: [12, 14]
odds: [11, 13, 15]
例:闭包作为返回值
结果:
twice(4): 8
quadruple(3): 12
例:选择性排序
结果:
sorted: [11, 12, 13, 14, 14]
闭包是一种表示可执行代码块的方法。闭包也是对象,可以像方法一样传递参数。由于闭包是代码块,因此也可以在需要时执行。像方法一样,在定义的过程中,闭包也可以使用一个或者多个参数。
一、inject方法
inject方法可用于遍历集合,首先将需要传递的值和集合项目传给闭包,此时其传递的值将作为处理结果,然后再和下一个集合项目一起传给闭包,依此类推。方法原型为:
Object inject(Object value, Closure closure)
例:求5的阶乘值
-----------
def factorial = [2,3,4,5].inject(1){previous, element -> previous * element}
println "Factorial(5): ${factorial}"
-----------
结果:
Factorial(5): 120
-----------
def fact = 1
[2,3,4,5].each{number -> fact *= number}
println "fact: ${fact}"
-----------
结果:
fact: 120
-----------
def list = [2,3,4,5]
factorial = list.inject(1){previous, element -> previous*element}
println "Factorial(5): ${factorial}"
-----------
结果:
Factorial(5): 120
-----------
list = [2,3,4,5]
def closure = {previous,element -> previous*element}
factorial = list.inject(1, closure)
println "Factorial(5): ${factorial}"
-----------
结果:
Factorial(5): 120
由于闭包也是一个对象,因此它可以作为方法的参数。
例:闭包作为方法的参数
-----------
//Find those items that qualify
def filter(list, predicate){
return list.findAll(predicate)
}
//Two predicate closure
def isEven = {x -> return (x%2 == 0)}
def isOdd = {x -> return !isEven(x)}
def table = [11,12,13,14]
//Apply filter
def evens = filter(table, isEven)
println "evens: ${evens}"
def odds = filter(table, isOdd)
println "odds: ${odds}"
-----------
结果:
evens: [12, 14]
odds: [11, 13]
例:闭包作为另一个闭包的参数
-----------
//Find initial list that conforms to predicate
def takeWhile = {predicate,list ->
def result = []
for(element in list){
if(predicate(element)){
result<<element
} else{
return result
}
}
return result
}
//Two predicate closures
def isEven = {x -> return x%2 == 0}
def isOdd = {x -> return !isEven(x)}
def table1 = [12,14,15,18]
def table2 = [11,13,15,16,18]
//Apply takeWhile
def evens = takeWhile.call(isEven, table1)
println "evens: ${evens}"
def odds = takeWhile.call(isOdd, table2)
println "odds: ${odds}"
------------
结果:
evens: [12, 14]
odds: [11, 13, 15]
例:闭包作为返回值
------------
def multiply(x){
return {y -> return x*y}
}
def twice = multiply(2)
println "twice(4): ${twice(4)}"
//Closure returning a closure
def multiplication = {x -> return {y -> return x*y}}
def quadruple = multiplication(4)
println "quadruple(3): ${quadruple(3)}"
------------
结果:
twice(4): 8
quadruple(3): 12
例:选择性排序
------------
def selectionSort = {list ->
def swap = {sList,p,q ->
def temp = sList[p]
sList[p] = sList[q]
sList[q] = temp
}
def minimumPosition = {pList,from ->
def mPos = from
def nextFrom = 1 + from
for(j in nextFrom..<pList.size()){
if(pList[j] < pList[mPos])
mPos = j
}
return mPos
}
def size = list.size() - 1
for(k in 0..size){
def minPos = minimumPosition(list, k)
swap(list, minPos, k)
}
return list
}
def table = [13,14,12,11,14]
def sorted = selectionSort(table)
println "sorted: ${sorted}"
------------
结果:
sorted: [11, 12, 13, 14, 14]