Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A Raku class that implements all three of the Positional, Associative, and Callable roles
#| A class that implements the Positional, Associative, and Callable roles.
#| Accordingly, this type can be bound variables with any sigil
class Seuss does Positional does Associative does Callable does Iterable {
has @.pos handles <AT-POS EXISTS-POS DELETE-POS ASSIGN-POS BIND-POS elems>
= Nil, '🐠', '<°))))><';
has %.asc handles <AT-KEY EXISTS-KEY DELETE-KEY ASSIGN-KEY BIND-KEY>
= (:blue<🐟>, :red<🐡>);
method of { Mu }
method iterator(|c) {
(flat @!pos[1..2].pairs, %!asc<red blue>:p).iterator
}
method CALL-ME { say "What a lot of fish there are!" }
}
my $scalar-fish = Seuss.new;
my @positional-fish := Seuss.new;
my %asociative-fish := Seuss.new;
my &callable-fish := Seuss.new;
say %asociative-fish[1]; # OUTPUT: «🐠»
say @positional-fish<red>; # OUTPUT: «🐡»
for @positional-fish {
.say # OUTPUT: 0 => 🐠
# 1 => <°))))><
# red => 🐡
# blue => 🐟
}
callable-fish # OUTPUT: What a lot of fish there are!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment