Created
July 18, 2018 16:40
-
-
Save FCO/88f9773eb3a4eab73dd2352362541186 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MacBook-Pro-de-Fernando:TakeIgnoreSeq fernando$ perl6 -I. -MTakeIgnoreSeq -e ' | |
say ^10 .Seq.take-while(* < 5).ignore-until(6).take-while(* < 9) | |
' | |
P6opaque: no such attribute '$!ignored' on type Seq in a Seq when trying to get a value | |
in method take-while at /Users/fernando/Documents/Projects/TakeIgnoreSeq/TakeIgnoreSeq.pm6 (TakeIgnoreSeq) line 56 | |
in block <unit> at -e line 5 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use MONKEY-TYPING; | |
use nqp; | |
class TakeIgnoreIterator does Iterator { | |
has Iterator $.original; | |
has Iterator $.handling; | |
has Iterator $!not-handled; | |
has $.condition = {True}; | |
has Bool $.taking = False; | |
method pull-one { | |
my $response; | |
with $!original { | |
if $!original !~~ ::?CLASS { | |
$!handling = $!original; | |
$!original = Nil | |
} else { | |
$response := .pull-one; | |
if $response =:= IterationEnd { | |
with $!original.not-handled { | |
$!handling = $_ | |
} | |
} | |
} | |
} | |
with $!handling { | |
if $!taking { | |
$response := .pull-one; | |
unless $response ~~ $!condition { | |
$!not-handled = $!handling; | |
$!handling = Nil; | |
$response := IterationEnd | |
} | |
} else { | |
$response := .pull-one; | |
until $response =:= IterationEnd { | |
last unless $response ~~ $!condition; | |
} | |
$!not-handled = $!handling; | |
$!handling = Nil; | |
$response := IterationEnd | |
} | |
} | |
return $response | |
} | |
method not-handled { | |
with $!not-handled { | |
my $nh = $!not-handled; | |
$!not-handled = Nil; | |
return $nh | |
} | |
} | |
} | |
role IgnoringSeq { | |
has Seq $!ignored; | |
method take-while($condition, Seq :$ignored) { | |
say $!ignored; | |
Seq.new: TakeIgnoreIterator.new: :original($.iterator), :$condition, :taking, :ignored; | |
} | |
method ignore-until($condition) { | |
my $ignored = Seq.new: TakeIgnoreIterator.new: :original($.iterator), :condition{ $_ !~~ $condition }, :!taking; | |
#$ignored.take-while: True, :$ignored | |
} | |
} | |
augment class Seq does IgnoringSeq { } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment