Guest User

Untitled

a guest
Jul 18th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. #use MONKEY-TYPING;
  2.  
  3. class TakeIgnoreIterator does Iterator {
  4. has Seq $.original-seq;
  5. has Iterator $!original;
  6. has Iterator $.handling;
  7. has Iterator $!not-handled;
  8. has $.condition = {True};
  9. has Bool $.taking = False;
  10. method pull-one {
  11. my $response;
  12. with $!original-seq {
  13. $!original = $!original-seq.iterator;
  14. $!original-seq = Nil
  15. }
  16. with $!original {
  17. if $!original !~~ ::?CLASS {
  18. $!handling = $!original;
  19. $!original = Nil
  20. } else {
  21. $response := .pull-one;
  22. if $response =:= IterationEnd {
  23. with $!original.not-handled {
  24. $!handling = $_
  25. }
  26. }
  27. }
  28. }
  29. with $!handling {
  30. $response := .pull-one;
  31. if $!taking {
  32. unless $response !=:= IterationEnd and $response ~~ $!condition {
  33. $!not-handled = $!handling;
  34. $!handling = Nil;
  35. $response := IterationEnd
  36. }
  37. } else {
  38. until $response =:= IterationEnd {
  39. last unless $response ~~ $!condition;
  40. $response := .pull-one;
  41. }
  42. $!not-handled = $!handling;
  43. $!handling = Nil;
  44. $response := IterationEnd
  45. }
  46. }
  47. return $response
  48. }
  49. method not-handled {
  50. with $!not-handled {
  51. my $nh = $!not-handled;
  52. $!not-handled = Nil;
  53. return $nh
  54. }
  55. }
  56. }
  57. role TakeIgnoreSeq[$parent = Nil] {
  58. has Seq $.ignored = $parent;
  59. method take-while($condition, Seq :$ignored) {
  60. return .take-while: $condition with $!ignored;
  61. Seq.new(TakeIgnoreIterator.new: :original-seq(self), :$condition, :taking) but TakeIgnoreSeq[$ignored];
  62. }
  63. method take-until($condition, Seq :$ignored) {
  64. return .take-until: $condition with $!ignored;
  65. Seq.new(TakeIgnoreIterator.new: :original-seq(self), :condition{ $_ !~~ $condition }, :taking) but TakeIgnoreSeq[$ignored];
  66. }
  67. method ignore-while($condition) {
  68. my $ignored = Seq.new(TakeIgnoreIterator.new: :original-seq(self), :$condition, :!taking) but TakeIgnoreSeq;
  69. $ignored.take-while(True, :$ignored)
  70. }
  71. method ignore-until($condition) {
  72. my $ignored = Seq.new(TakeIgnoreIterator.new: :original-seq(self), :condition{ $_ !~~ $condition }, :!taking) but TakeIgnoreSeq;
  73. $ignored.take-while(True, :$ignored)
  74. }
  75. }
  76.  
  77. #augment class Seq { method TWEAK (|) { self does TakeIgnoreSeq } }
Add Comment
Please, Sign In to add comment