Skip to content

Commit

Permalink
Implement .tail(*-N) for all but first N elements
Browse files Browse the repository at this point in the history
  • Loading branch information
lizmat committed Apr 8, 2017
1 parent d673ea7 commit 188b7b1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
10 changes: 9 additions & 1 deletion src/core/Any-iterable-methods.pm
Expand Up @@ -1878,7 +1878,15 @@ Did you mean to add a stub (\{...\}) or did you mean to .classify?"
}
multi method tail(Any:D: $n) {
Seq.new(
Rakudo::Iterator.LastNValues(self.iterator,$n,'tail')
nqp::if(
nqp::istype($n,Callable)
&& nqp::isgt_i((my $skip := -($n(0).Int)),0),
nqp::stmts(
(my $iterator := self.iterator).skip-at-least($skip),
$iterator
),
Rakudo::Iterator.LastNValues(self.iterator,$n,'tail')
)
)
}

Expand Down
11 changes: 8 additions & 3 deletions src/core/Array.pm
Expand Up @@ -1050,9 +1050,14 @@ my class Array { # declared in BOOTSTRAP
&& nqp::elems($reified),
nqp::stmts(
(my $iterator := Rakudo::Iterator.ReifiedArray(self)),
nqp::unless(
nqp::istype($n,Whatever) || $n == Inf,
$iterator.skip-at-least(nqp::elems($reified) - $n)
nqp::if(
nqp::istype($n,Callable)
&& nqp::isgt_i((my $skip := -($n(0).Int)),0),
$iterator.skip-at-least($skip),
nqp::unless(
nqp::istype($n,Whatever) || $n == Inf,
$iterator.skip-at-least(nqp::elems($reified) - $n)
)
),
$iterator
),
Expand Down
11 changes: 8 additions & 3 deletions src/core/List.pm
Expand Up @@ -1285,9 +1285,14 @@ my class List does Iterable does Positional { # declared in BOOTSTRAP
$!reified.DEFINITE && nqp::elems($!reified),
nqp::stmts(
(my $iterator := Rakudo::Iterator.ReifiedList(self)),
nqp::unless(
nqp::istype($n,Whatever) || $n == Inf,
$iterator.skip-at-least(nqp::elems($!reified) - $n)
nqp::if(
nqp::istype($n,Callable)
&& nqp::isgt_i((my $skip := -($n(0).Int)),0),
$iterator.skip-at-least($skip),
nqp::unless(
nqp::istype($n,Whatever) || $n == Inf,
$iterator.skip-at-least(nqp::elems($!reified) - $n)
)
),
$iterator
),
Expand Down

0 comments on commit 188b7b1

Please sign in to comment.