Skip to content

Commit

Permalink
Make smartmatching Iterables against Lists use same semantics as Arrays
Browse files Browse the repository at this point in the history
S03-smartmatch/array-array.t tests smartmatching against Lists.
TimToady++ suggests adding some pattern type for smart matching against
lists. However, we're trying to get to the same functional level as nom
now. We can always create new semantics later.

Makes array-array.t pass.
  • Loading branch information
niner committed Aug 28, 2015
1 parent c68db93 commit 51b313b
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion src/core/List.pm
Expand Up @@ -378,7 +378,37 @@ my class List does Iterable does Positional { # declared in BOOTSTRAP
}.new(self)
}

multi method ACCEPTS(List:D: $topic) { self }
multi method ACCEPTS(List:D: $topic) {
return ?self unless nqp::istype($topic, Iterable);
my $sseq = self;
my $tseq = $topic;

my int $spos = 0;
my int $tpos = 0;
while $spos < +$sseq {
# if the next element is Whatever
if nqp::istype($sseq[$spos], Whatever) {
# skip over all of the Whatevers
$spos = $spos + 1
while $spos <= +$sseq && nqp::istype($sseq[$spos], Whatever);
# if nothing left, we're done
return True if !($spos < +$sseq);
# find a target matching our new target
$tpos = $tpos + 1
while ($tpos < +$tseq) && $tseq[$tpos] !== $sseq[$spos];
# return false if we ran out
return False if !($tpos < +$tseq);
}
elsif $tpos >= +$tseq || $tseq[$tpos] !=== $sseq[$spos] {
return False;
}
# skip matching elements
$spos = $spos + 1;
$tpos = $tpos + 1;
}
# If nothing left to match, we're successful.
$tpos >= +$tseq;
}

multi method list(List:D:) { self }

Expand Down

0 comments on commit 51b313b

Please sign in to comment.