Lua Book

Roberto Ierusalimschy roberto at inf.puc-rio.br
Wed Mar 3 07:37:33 EST 2004


> Provocative comparison; perhaps Roberto will even join in
> and comment here.

As I was invited, I will give a few short comments:

>I suppose Python's recent introduction of generators makes this rather
>trivial.

You should read the book. There you would learn that Lua offers
coroutines (the real ones). With them, you can write a generator quite
similar to the piece you wrote:

  function allwords ()
    return coroutine.wrap(function ()
             for line in io.lines() do
               for word in string.gfind(line, "%w+") do
                 coroutine.yield(word)
               end
             end
           end)
  end

  for word in allwords() do
    print(word)
  end

You can also write it in a Ruby-iterator style (using anonymous
functions instead of blocks):

  function allwords (yield)
    for line in io.lines() do
      for word in string.gfind(line, "%w+") do
        yield(word)
      end
    end
  end

  -- usage example
  local count = 0
  allwords(function (word) count = count + 1 end)
  print("number of words: " .. count)



> The full text [of the book] was available on the net

Please notice that the text available on the net is an old version; it
is quite different from the final book.


> having finished the book I was left feeling that the core language
> offered nothing useful over Python, in fact it was rather lacking
> in many areas. [...] it's unlikely to ever come close to Python for
> general purpose coding.

This is a sensible opinion. (And apparently well based, as you read the
book :) Lua and Python have different goals; and different people have
different opinions.

-- Roberto




More information about the Python-list mailing list