Skip to content

Instantly share code, notes, and snippets.

@colomon
Created April 18, 2010 15:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save colomon/370287 to your computer and use it in GitHub Desktop.
Save colomon/370287 to your computer and use it in GitHub Desktop.
class Tree {
has $.node;
has @.children;
method new($node, *@children) { self.bless(*, :$node, :@children) }
method Str {
@.children ?? "$.node: [@.children.join(", ")]" !! $.node.Str
}
method iterator() {
my sub traverse(Tree $t) {
take $t.node;
for $t.children {
traverse($_);
}
}
gather {
traverse(self);
}
}
}
multi map(&c, Tree $t) {
Tree.new(c($t.node), map { map(&c, $_) }, $t.children)
}
my $t = Tree.new("H2O", Tree.new("H"), Tree.new("O"), Tree.new("O"));
say $t;
say map { .lc }, $t;
$t.iterator.eager.perl.say;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment