Grails 애플리케이션에서, 나는 유향 그래프에서 사이클의 생성을 막으려하고있다. 사용자는 노드에 부모를 지정할 수 있지만 노드 자체의 상위 노드는 상위 노드 여야합니다. 나는 무거운 수행하는 재귀 함수입니다 checkLineageForTarget를 호출하는 간단한 설정 기능, 작성했습니다 : 그에서이 (가) 인쇄 "작품"Groovy 클로저가 내가 예상 한 값을 반환하지 않는 이유는 무엇입니까?
boolean checkLineageForTarget(Integer target, Collection<Node>stillToProcess){
// true means that this is a safe addition
// false means that this addition creates a cycle
boolean retVal = stillToProcess.each {
Collection<Node> itsParents = getParentNodes(it)
if (it.id == target){
println("found a loop on " + target);
return false; // loop detected!
}
if (itsParents.empty){ return true; } // end of the line
return checkLineageForTarget(target, itsParents)
}
// at this point, retVal is always true, even when the "found a loop [...]" condition is met
return retVal;
}
이를 "루프를 발견 [...]"메시지 , 클로저 외부에서는 retVal이 true이고, 호출 함수가 새로운 부모/자식 관계를 추가하려고 시도하고 스택이 끝났습니다.
오해는 무엇입니까?
당신이 .each''사용할 수 있습니다 그렇게? 나는 .every를 사용하고있다. – zoran119
그것은 Grails 다. "Grails on Grails"가 아니다. –
@Burt oops, my bad. –