Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing AT-POS to return an object instead of a list of things

Tags:

object

slice

raku

I've the following class:

class Names {
    has @!names;

    method add-name( $name ) { @!names.push($name) }

    multi method AT-POS( ::?CLASS:D: $index ) {
        my $new-names-obj = Names.new;

        for @!names[$index] -> $name {
            $new-names-obj.add-name($name);
        }

        return $new-names-obj;
    }

    method gist {
        @!names.join("\n")
    }
}

I'd like to be able to slice a Names object and the returned value should be another Names object whose elements are sliced off from the original Names object. For instance:

my $original = Names.new;
$original.add-name($_) for <jonathan joseph jotaro josuke giorno>;
my $sliced-off = $original[0..2];

say $original.^name;   #=> Names
say $original;         #=> jonathan, joseph, jotaro, josuke, giorno
say $sliced-off.^name; #=> List
say $sliced-off;       #=> (jonathan joseph jotaro)

When a single argument is passed, it works as expected and as described in this answer but it's not the case with range since AT-POS ends up being called multiple times and collecting the results in a list. Thus I'm wondering if it's possible to return a single object $sliced-off, not a list of results, when using a range.

like image 881
Luis F. Uceta Avatar asked Feb 03 '20 16:02

Luis F. Uceta


2 Answers

The AT-POS method is intended to let an object act as a Positional object. This is not what you appear to want. You want object[slice] DWIM.

The best way to achieve that, is to create a postcircumfic:<[ ]> (multi) candidate for your object:

class A {
    method slice(@_) {
        say @_;  # just to show the principle
    }
}
sub postcircumfix:<[ ]>($object, *@indices) {
   constant &slicer = &postcircumfix:<[ ]>;
   $object ~~ A
     ?? $object.slice(@indices)
     !! slicer($object, @indices)
}

A.new[1,2,4,5];  # [1 2 4 5]

my @a = ^10;     # check if foo[] still works
say @a[1,2,4,5]; # (1 2 4 5)

To make sure that the common behaviour of @a[] is kept, we save the value of the system's postcircumfix:[ ]> at compile time (with a constant). Then at runtime, when the object is not of the right class, invoke the original version of postcircumfix:<[ ]> with the given parameters.

like image 173
Elizabeth Mattijsen Avatar answered Oct 31 '22 03:10

Elizabeth Mattijsen


Building on Liz's guidance:

class Names {
    has @.names;                                      # Make public so [] can access.
    method new (*@names) { nextwith :@names }         # Positional .new constructor.
    submethod BUILD (:@!names) {}                     # Called by nextwith'd Mu new.
    multi sub postcircumfix:<[ ]>                     # Overload [] subscript.
      ( Names $n, $index, *@indices )                 # Why `$index, *@indices`?
      is default is export                            # And why `is default`?
      { Names.new: |$n.names[ |$index, |@indices ] }  # Why? See my comment 
    method gist { @!names.join(', ') }                # below Liz's answer.
}
import Names;

my $original = Names.new: <jonathan joseph jotaro josuke giorno>;
my $sliced-off = $original[0..2];

say $original.^name;   #=> Names
say $original;         #=> jonathan, joseph, jotaro, josuke, giorno
say $sliced-off.^name; #=> Names
say $sliced-off;       #=> jonathan, joseph, jotaro

PLMK if the code or explanation is inadequate.

like image 37
raiph Avatar answered Oct 31 '22 03:10

raiph