Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Lua is terrible. Don't use it.

Name: Anonymous 2011-09-30 22:32

For those of you demanding a reason Lua is shit,
consider the following code:


local fh = io.open(filename, "r")  
while true do
    line = fh:read("*l")
    if line == nil then break end
    f(line)
end


That is a fucking eyesore. The whole point of a while loop is to embed the exit condition in it. When you have to make an infinite loop and then use an if statement with a break to accomplish what you can normally do with something like:


fh = fopen(filename, "r");  
while(fgets(line, max, fh) != NULL) {
    f(line);
}


that's when you need to stop using that language. Also why do if statements have "then"s? Any programming language that uses "then" is awful.

Name: Anonymous 2011-10-01 0:10

You can use the

file:lines()

method to get an iterator that will return the lines in a file. So the code in the op can be written as:


local fh = io.open(filename, "r")
if fh == nil then
  error("I couldn't open "..filename.." for reading bro\n")
end
for line in fh:lines() do
  f(line)
end


And if you want to make a superduperawesome iterator utility library, you can make it more compact and unreadibly functionaly:


function iter_lib.foreach(iter, fn)
  for value in iter do
    fn(value)
  end
end

function iter_lib.mask(iter, fn)
  return function()
    local value = iter()
    if value == nil then return nil end
    return fn(value)
  end
end


iter_lib.foreach(iter_lib.mask(io.open(filename, "r"):lines(), f))

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List