Skip to content

Instantly share code, notes, and snippets.

@tobyink
Last active September 25, 2020 21:22
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 tobyink/0dc5ac592d4a749cbf5c88699f87b07a to your computer and use it in GitHub Desktop.
Save tobyink/0dc5ac592d4a749cbf5c88699f87b07a to your computer and use it in GitHub Desktop.
Linked lists, Zydeco example
package SLL {
use Zydeco;
class Node {
has value ( type => Int );
has nextnode ( type => Maybe[Object] );
method printvalue () {
print $self->value, " ";
}
method go_next = $self->nextnode;
factory new_node ( Int $value, Object $next? ) {
return $class->new(
value => $value,
nextnode => $next
);
}
}
}
my $node_a = SLL->new_node(30);
my $node_b = SLL->new_node(40, $node_a);
my $node_c = SLL->new_node(70, $node_b);
my $node_head = SLL->new_node(0, $node_c);
my $node = $node_head;
while ( $node = $node->go_next ) {
$node->printvalue;
}
print "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment