Run a regex on a Supply or other stream-like sequence?

Viewed 93

Suppose I have a Supply, Channel, IO::Handle, or similar stream-like source of text, and I want to scan it for substrings matching a regex. I can't be sure that matching substrings do not cross chunk boundaries. The total length is potentially infinite and cannot be slurped into memory.

One way this would be possible is if I could instantiate a regex matching engine and feed it chunks of text while it maintains its state. But I don't see any way to do that -- I only see methods to run the match engine to completion.

Is this possible?

1 Answers

After some more searching, I may have answered my own question. Specifically, it seems Seq.comb is capable of combining chunks and lazily processing them:

my $c = supply {
  whenever Supply.interval(1.0) -> $v {
    my $letter = do if ($v mod 2 == 0) { "a" } else { "b" };
    my $chunk = $letter x ($v + 1);
    say "Pushing {$chunk}";
    emit($chunk);
  }
};

my $c2 = $c.comb(/a+b+/);

react {
  whenever $c2 -> $v {
    say "Got {$v}";
  }
}

See also the concurrency features used to construct this example.

Related