lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]




2011/2/11 Dirk Laurie <dpl@sun.ac.za>
On Fri, Feb 11, 2011 at 11:20:08AM +0200, Miles Bader wrote:
[about the following exchange]
>>> So, maybe in e.g. Lua 6, we may complete remove the implicit convert in Lua.
>> And break 95% of happily working Lua programs.
> >
> > I'd be very surprised if it actually caused much breakage at all
> > (... there's an easy way to find out, of course...)
>
> ... and of course, even if it does, it's not really a problem -- the
> whole point of Lua's major-version upgrade policy is to allow desirable,
> but non-backward-compatible, changes.

Among the many things that I love about Lua is that there is no
"thought policing".  "Good" habits are not rammed down your throat.

At present I can read in a file containing student grades, with
lines like
   1.5     3.8  9.1    10.0
and say:

for l in io.lines("filename") do
   total=0; for x in string.gmatch(l,"%s*(%S+)") do total=total+x end
   print(total)
   end

The '+' symbol is a clear, legible indication that 'total' and
'x' are expected to be coercible to numbers.  If any of my
whitespace-delimited fields contains something that is invalid as
a number, I'll get an "arithmetic on a string value" error.

I totally fail to see in what way my program will be improved if
in Lua 6 I will be forced to write 'tonumber(x)' explicitly.
If I really like to do that, I can do it now, it's fully legal.

Dirk


for line in io.lines("filename") do
    local total = 0
    for x in map(tonumber, string.gmatch(line, "%s*(%S+)")) do
        total = total + x
    end
    print(total)
end

you can implement map easily -- but notice when the second argument is function, now you may treat it as a iterator.