[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Modified allwords() function
- From: "Geo Massar" <kawageo@...>
- Date: Sat, 14 May 2005 14:46:05 -0700
> Geo Massar wrote:
> > As per the book written by R. Ierusalimschy, allwords() function
> > generates words which contain only letters and numbers. I'd like to
modify
> > the function so that words can be anything printable between spaces. The
> > mod is shown below, rather more complicated. Is there a way to simplify
> > the code?
>
> This is a perfect example for a coroutine iterator (see chapter 9.3):
>
> local function allmatches(pattern)
> return coroutine.wrap(function()
> for line in io.lines() do
> for word in string.gfind(line, pattern) do
> coroutine.yield(word)
> end
> end
> end)
> end
>
> for i in allmatches("%S+") do
> print(i)
> end
Thanks, Mike
I am not used to coroutine concept yet. But, I'll save your code in my
personal archive.