Created
December 21, 2022 14:26
-
-
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
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
#| 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