Skip to content

Instantly share code, notes, and snippets.

@codesections
Created December 21, 2022 14:26
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 codesections/8cd163196d19b6c603058f4ad30ab11d to your computer and use it in GitHub Desktop.
Save codesections/8cd163196d19b6c603058f4ad30ab11d to your computer and use it in GitHub Desktop.
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