Skip to content

Commit

Permalink
Expose new R:I:While/Until in .head/.tail
Browse files Browse the repository at this point in the history
- .head taking a Callable:D: block should return trueish while values
  should be passed on.  The first falsish value will end the iterator.
- .tail taking a Callable:D: block should return falsish while values
  are being skipped.  The first trueish value will start passing on
  values until iterator is exhausted.
- no breakage in spectest seen.

Please note that I feel these semantics are a bit in conflict with the
"* - x" semantics meaning "so many from end".  So this would be the first
case where a WhateverCode has a different semantic from a generic Callable.
Perhaps we should expose this functionality in separate .while/.until
methods.  Please discuss :-)
  • Loading branch information
lizmat committed Nov 22, 2017
1 parent 2c1a286 commit 76fcea1
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/core/Any-iterable-methods.pm
Expand Up @@ -1823,6 +1823,9 @@ Did you mean to add a stub (\{...\}) or did you mean to .classify?"
Rakudo::Iterator.AllButLastNValues(self.iterator,-($w(0).Int))
)
}
multi method head(Any:D: Callable:D $cond) {
Seq.new(Rakudo::Iterator.While(self.iterator,$cond))
}
multi method head(Any:D: $n) {
Seq.new(Rakudo::Iterator.NextNValues(self.iterator,$n))
}
Expand All @@ -1838,19 +1841,24 @@ Did you mean to add a stub (\{...\}) or did you mean to .classify?"
$pulled
)
}
multi method tail(Any:D: $n) {
multi method tail(Any:D: WhateverCode $w) {
Seq.new(
nqp::if(
nqp::istype($n,WhateverCode)
&& nqp::isgt_i((my $skip := -($n(0).Int)),0),
nqp::isgt_i((my $skip := -($w(0).Int)),0),
nqp::stmts(
(my $iterator := self.iterator).skip-at-least($skip),
$iterator
),
Rakudo::Iterator.LastNValues(self.iterator,$n,'tail')
self.iterator
)
)
}
multi method tail(Any:D: Callable:D $w) {
Seq.new(Rakudo::Iterator.Until(self.iterator,$w))
}
multi method tail(Any:D: $n) {
Seq.new(Rakudo::Iterator.LastNValues(self.iterator,$n,'tail'))
}

proto method minpairs(|) {*}
multi method minpairs(Any:D:) {
Expand Down

0 comments on commit 76fcea1

Please sign in to comment.