Quick actions

cmd+k|ctrl+k

Navigation

Languages

Pattern

Snippet info

Language

Raku

Visibility

public

Author

fernando-correa

Created

2024-08-28T20:24:35.960219Z

Updated

2024-08-29T01:42:49.352989Z

class Step is Method does Positional {
    has $.name = "bla";
    has Callable @.steps handles <AT-POS elems>;
    
    method add-step(&callable) {
        @!steps.push: &callable
    }
    
    method next-step(@pos) {
        #say @pos;
        my @data := self;
        my @qtt;
        for @pos -> $i {
            @qtt.push: @data.elems;
            try @data := @data[$i]
        }
        #say @qtt;
        my @next = @pos;
        for (@pos Z @qtt.map: * - 1).kv.reverse -> ($pos, $max), $i {
            if $pos < $max {
                @next[$i]++;
                last
            } else {
                @next.splice: $i, 1
            }
        }
        my $data := self;
        for @next -> UInt $i {
            $data := $data[$i];
        }
        for 0 xx Inf -> UInt $i {
            last unless $data ~~ Positional;
            @next.push: $i;
            $data := $data[$i];
        }
        #say @next;
        @next
    }
    
    method first-pos {
        my $data := self;
        my UInt @i;
        for 0 xx Inf -> UInt $i {
            @i.push: $i;
            $data := $data[$i];
            last unless $data ~~ Positional;
        }
        @i
    }
    
    method CALL-ME($match is copy) {
        my @pos := $match.pos;
        unless @pos {
            $match .= clone: :pos(@pos := $.first-pos);
        }
        #say self[||@pos](match);
        my $call = (self, |@pos).reduce: -> $data, UInt $i {
            $data[$i]
        }
        $.next($match) if $call($match)
    }
    
    method next(\match) {
        my @pos = match.pos;
        self.(match.clone: :pos($.next-step: @pos))
    }
}

class Event {
    has UInt @.pos;
}

my $sub  = Step.bless;
$sub.add-step: { say "1: {.gist}" }
my $step = Step.bless;
$step.add-step: $sub;
$step.add-step: { say "2: {.gist}"; False }
say $step(Event.new)
INFO