Skip to content

Instantly share code, notes, and snippets.

@librasteve
Last active June 1, 2023 09: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 librasteve/df48c92a3f91e061e5dd81a1871ae1eb to your computer and use it in GitHub Desktop.
Save librasteve/df48c92a3f91e061e5dd81a1871ae1eb to your computer and use it in GitHub Desktop.
#2023.05
role DataSlice does Iterable { # <-- Iterable role needed for container, but not Positional
#role DataSlice does Positional does Iterable {
has Any @.data;
#`[ # <-- the actual Iterable (and Positional) methods are not important
# Positional role support
# viz. https://docs.raku.org/type/Positional
method of {
Any
}
method elems {
@!data.elems
}
method AT-POS( $p ) {
@!data[$p]
}
method EXISTS-POS( $p ) {
0 <= $p < @!data.elems ?? True !! False
}
# Iterable role support
# viz. https://docs.raku.org/type/Iterable
method iterator {
@!data.iterator
}
method flat {
@!data.flat
}
method lazy {
@!data.lazy
}
method hyper {
@!data.hyper
}
#]
# Extra support for reduce via list and Numeric
method Numeric {
@!data.Numeric
}
method list {
@!data.list
}
}
#`[ fails
my \dss = $; # <-- thou shalt not use a scalar container
dss = DataSlice.new( data => [1, 3, 5, 42, 6, 8] );
#]
#[ works
my \dss = DataSlice.new( data => [1, 3, 5, 42, 6, 8] );
#]
say [+] dss.data; #65
say [+] dss; #65
say dss.data.VAR.^name;
say dss.VAR.^name;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment