Groovy闭包

Groovy闭包


闭包是一种表示可执行代码块的方法。闭包也是对象,可以像方法一样传递参数。由于闭包是代码块,因此也可以在需要时执行。像方法一样,在定义的过程中,闭包也可以使用一个或者多个参数。
一、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
-----------

deffact = 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

由于闭包也是一个对象,因此它可以作为方法的参数。
例:闭包作为方法的参数
-----------

// Findthoseitemsthatqualify
def filter(list,predicate){
return list.findAll(predicate)
}
// Twopredicateclosure
def isEven = {x -> return (x % 2 == 0)}
def isOdd = {x -> return !isEven(x)}

def table = [ 11 , 12 , 13 , 14 ]

// Applyfilter
def evens = filter(table,isEven)
println
" evens:${evens} "
def odds = filter(table,isOdd)
println
" odds:${odds} "

-----------
结果:
evens: [12, 14]
odds: [11, 13]

例:闭包作为另一个闭包的参数
-----------

// Findinitiallistthatconformstopredicate
def takeWhile = {predicate,list ->
def result = []
for (element in list){
if (predicate(element)){
result
<< element
}
else {
return result
}
}
return result
}
// Twopredicateclosures
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 ]
// ApplytakeWhile
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)} "
// Closurereturningaclosure
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]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值