Last active
September 25, 2020 21:22
-
-
Save tobyink/0dc5ac592d4a749cbf5c88699f87b07a to your computer and use it in GitHub Desktop.
Linked lists, Zydeco example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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