우리는 현재 트리를 탐색하여 특정 장치 (및 하위 장치)의 모든 운영자를 캡처해야합니다. 장치에는 특정 연산자가있는 하위 장치가있을 수 있습니다.Groovy/traverse 트리에서 재귀 사용에 신참?
저는 Groovy에서 재귀를 처음 사용하기 때문에 제가 일을 제대로하고 있는지 궁금합니다. 더 나은 방법을 배우는 데 도움이되는 포인터는 무엇입니까?
def listOperators(device) {
// list with all operator id's
def results = []
// closure to traverse down the tree
def getAllOperators = { aDevice->
if(aDevice) {
aDevice.operators.each { it ->
results << it.id
}
}
if (aDevice?.children) {
aDevice.children.each { child ->
results << owner.call(child)
}
}
}
// call the closure with the given device
getAllOperators(device)
// return list with unique results
return results.unique()
}