Skip to content

Instantly share code, notes, and snippets.

@dr-kd
Created October 19, 2010 00:00
Show Gist options
  • Save dr-kd/633326 to your computer and use it in GitHub Desktop.
Save dr-kd/633326 to your computer and use it in GitHub Desktop.
sub _get_groups_data_structure {
my ($self, $groups ) = @_;
$groups ||= $self->groups;
## this implementation courtesy of ribasushi. Must acknowledge in the
## paper.
my $inject = {
-1 => 'leaf',
0 => { some_data => 'lvl0' },
1 => { some_data => 'lvl1' },
};
my ($visit, $v);
$v = $visit = sub { # get $visit in scope through a hack. Could use
# Sub::Current instead. Might want to do this in the
# situation that this code is called in a more complex
# situation.
$_[0] ||= 0;
+{ map
{
$_ => $inject->{$_[0]}
? { %{$inject->{$_[0]}}, children => $visit->($_[0]+1) }
: $_[0] == @$groups
? $visit->($_[0]+1)
: $inject->{-1}
}
@{$groups->[$_[0]]}
};
};
weaken($visit); # without this we have a memory leak
my $data_tree = $visit->();
return %$data_tree;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment