Skip to content

Instantly share code, notes, and snippets.

@TimToady
Created January 5, 2014 05:18
Show Gist options
  • Save TimToady/8264706 to your computer and use it in GitHub Desktop.
Save TimToady/8264706 to your computer and use it in GitHub Desktop.
role BinaryTree[::T] {
has T $.value;
has BinaryTree[T] $.left;
has BinaryTree[T] $.right;
method replace-all(T $value) {
$!value = $value;
$!left.replace-all($value) if $!left.defined;
$!right.replace-all($value) if $!right.defined;
}
}
class IntTree does BinaryTree[Int] { }
my IntTree $it .= new(value => 1,
left => IntTree.new(value => 2),
right => IntTree.new(value => 3));
$it.replace-all(42);
say $it.perl;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment