From 76fcea1e18455fac9e71ab4ecc2ef42789d7035e Mon Sep 17 00:00:00 2001 From: Elizabeth Mattijsen Date: Wed, 22 Nov 2017 23:43:06 +0100 Subject: [PATCH] Expose new R:I:While/Until in .head/.tail - .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 :-) --- src/core/Any-iterable-methods.pm | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/core/Any-iterable-methods.pm b/src/core/Any-iterable-methods.pm index 6de8bdbd18a..2810157f14b 100644 --- a/src/core/Any-iterable-methods.pm +++ b/src/core/Any-iterable-methods.pm @@ -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)) } @@ -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:) {