Re: Recursive blocks
Re: Recursive blocks
- Subject: Re: Recursive blocks
- From: Glen Low <email@hidden>
- Date: Wed, 2 Feb 2011 09:48:45 +0800
Chris
On 30/01/2011, at 2:26 AM, Chris Suter wrote:
The use of a block here looks pretty ugly in my opinion. I don't see
returning more than one type as being a big deal. Also, in the
function case, I don't believe there's a need for the GKGraph *
parameter; you can use self.
Having said all that, does your algorithm need to be recursive?
Wouldn't this do the same job more efficiently without recursion:
- (GKGraph *)enclosingClusterForNodes:(NSSet *)someNodes
{
if (![someNodes isSubsetOfSet:nodes])
return nil;
GKGraph *cluster = self;
LOOP:
for (GKGraph *subcluster in cluster.subgraphs) {
if (subcluster.isCluster && [someNodes
isSubsetOfSet:subcluster.nodes]) {
cluster = subcluster;
goto LOOP;
}
}
return cluster;
}
/* Unlike enclosingClusterForNodes, this will only return a cluster
that directly contains the nodes, i.e. none of the nodes will
be found in any sub clusters. */
- (GKGraph*)clusterForNodes:(NSSet*)someNodes
{
GKGraph *cluster = [self enclosingClusterForNodes:someNodes];
for (GKGraph *subcluster in subgraphs)
if (subcluster.isCluster && [nodes intersectsSet:subcluster.nodes])
return nil;
return cluster;
}
(I actually wonder whether you meant clusterForNodes to do what
enclosingClusterForNodes does and given the uncertainty, it
certainly warrants a comment.)
I concede your non-recursive formulation looks cleaner, except that I
might change the goto into a while or do-while loop. Thanks for
pointing it out.
Anyway, I personally wouldn't use a block as you've used it and I'm
not convinced your algorithm needs to be recursive, so I've still
yet to see a legitimate use case for recursive blocks.
You've transformed this one to a non-recursive algorithm, but some
algorithms would look cleaner as a recursion. In those cases, if there
is a need to capture local variables, that would be a legitimate use
of a recursive block.
Cheers, Glen Low
---
pixelglow software | simply brilliant stuff
www.pixelglow.com
aim: pixglen
twitter: pixelglow
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden