Page 5 of 6

Re: The "I Love Lua, but..." post

Posted: Sun May 01, 2011 9:40 pm
by BlackBulletIV
bartbes: Now that's a clever script! Good last resort for people who REALLY want those operators, but don't have an editor that supports snippets or the like.
ishkabible wrote:List of my issues with Lua(many of which have been said):
*no += like operators, and no ++/-- operators either
*no control over for loops, sometimes i want a different condition than <= or a more complex increment than addition
*globals by default is really annoying, locals by defualt would be equally annoying. i can't tell you how many bugs i have had becuase i mistyped a variable
*no bit-wise operators, this doesn't effect me very often but when it dose i hate it.
*arrays start at 1, WTF!! they should start at 0!!
*no default OO mechanisms, i would like to have a means to literally write a class
*no type dispatching, i hate writing all those type checked functions that do different things for different types. i want function overloads
*'tonumber' dose not support a __tonumber meta method. what if you want to make a number class of sorts. i made a ratio class a while back and i wanted to be able to convert to number, i settled for a method instead.
*~=...WTF what happened to !=?
*and, or, not ... i don't like them i prefer &&, ||, ! instead
however all and all i love Lua, it's my second favorite language. it basically rocks. meta-methods are sexy, Lua is fast, Lua is cool.

edit: wtf how do you use the List BBcode?
About arrays starting at 0, many times I find it convenient and logical to start at 1, it's just a matter of switching < to <= and adjusting some of the addition and subtraction we do on indicies. It's all a matter of what you're used to.

Yeah, I'd love a __tonumber metamethod, it would be great for things like fuzzy booleans and the like.

Personally I love the use of and, or, not, it makes things look so much cleaner and clearer for me, but again it's all preference. I also like how Lua doesn't have both sets with different precedences like Ruby does, meaning I keep having to mix the two sets up in my code.

Re: The "I Love Lua, but..." post

Posted: Mon May 02, 2011 10:43 pm
by ishkabible
vrld wrote:
ishkabible wrote:*no control over for loops, sometimes i want a different condition than <= or a more complex increment than addition
Uhm.. what? Generic for.
yes thats just it, it's a "Generic" for loop. do this in Lua just as cleanly

Code: Select all

for(int i=p*p;i>j;j*=p) {
    //some code here
}
sure you can do it using while loops, it's just not as clean.

Code: Select all

local i = p*p
while i>j do
   --some code here
   j*=p
end
now do you see what i mean?
ishkabible wrote:*arrays start at 1, WTF!! they should start at 0!!
Says who? :P
convention among every other language i have used. yes it's just convention, i realize that but it's what im used to. how many other languages by convention start there arrays at 1?

Re: The "I Love Lua, but..." post

Posted: Tue May 03, 2011 9:08 am
by Robin
ishkabible wrote:yes thats just it, it's a "Generic" for loop.
I don't think you know what generic for loop means. Have you read the page behind the link?
ishkabible wrote:do this in Lua just as cleanly

Code: Select all

for(int i=p*p;i>j;j*=p) {
    //some code here
}

Code: Select all

function multiply(start, stop, step)
  return function()
        start = start * step
        if start < stop then -- I assume you meant this
            return start
        end
   end
end

for i in multiply(p, j, p) do
  -- some code here
end
It's a lot more code, but it is a function, which means it is reusable, unlike your Lua-while and C-for examples. I would argue this is cleaner.
ishkabible wrote:
ishkabible wrote:*arrays start at 1, WTF!! they should start at 0!!
Says who? :P
convention among every other language i have used. yes it's just convention, i realize that but it's what im used to. how many other languages by convention start there arrays at 1?
Being the same as or different than other languages does not make something better or worse persé. Starting at 0 is more useful in some domains, starting at 1 in others.

Re: The "I Love Lua, but..." post

Posted: Tue May 03, 2011 9:20 am
by BlackBulletIV
Robin wrote:I would argue this is cleaner.
Due in a large measure to spacing IMHO. :P
Robin wrote:Being the same as or different than other languages does not make something better or worse persé. Starting at 0 is more useful in some domains, starting at 1 in others.
From my procedural cave generation experiment, I've found that for me, 0 is more useful for coordinate indexing, and 1 is more useful for counting (arrays of items).

Re: The "I Love Lua, but..." post

Posted: Tue May 03, 2011 11:49 am
by vrld
ishkabible wrote:yes thats just it, it's a "Generic" for loop. do this in Lua just as cleanly

Code: Select all

for(int i=p*p;i>j;j*=p) {
    //some code here
}
I don't find that very clean: Why do you increase j, but have initialized only i? And more importantly: What is the code supposed to do?

Anyway, in addition to Robins code, here is a stateless version:

Code: Select all

function m(p,j) return p*p > j and j * p or nil end
for j in m, <p>, <j> do -- you could replace m with an anonymous function here
    -- some code
end
But that example is rather artificial. On the other hand, do this in C++:

Code: Select all

for key, value in pairs(tbl) do --[[ stuff --]] end
The shortest I can come up with is this:

Code: Select all

for (std::map<key_type, value_type>::iterator it = tbl.begin(); it != tbl.end(); ++it) {
    key_type &key = it->first;
    value_type &value = it->second;
    // stuff
}
Not very readable.

The same can be said for iterating over lines in a file:

Code: Select all

file = assert(io.open(<filename>, 'r'))
for line in file:lines() do --[[ stuff --]] end
With C++ the same feels hackish:

Code: Select all

std::ifstream file(<filename>);
assert(file.good());
for (std::string line; getline(file, line); /*nothing*/) { /* stuff */ }

Re: The "I Love Lua, but..." post

Posted: Tue May 03, 2011 12:08 pm
by kikito

Code: Select all

# tbl is an array
tbl.each do |v|
...
end

# tbl is a hash
tbl.each do |k,v|
...
end

#one-liners
tbl.each {|v| ... } #array
tbl.each {|k,v| ... } #hash
This language exists today. You do stuff like this on it every time. It is awesome.

Re: The "I Love Lua, but..." post

Posted: Tue May 03, 2011 1:16 pm
by vrld
You can do higher order functions with Lua, too:

Code: Select all

function map(func, tbl)
    local out = {}
    for k,v in pairs(tbl) out[k] = func(v, k)
    return out
end
map(function(x) return x*x end, {1,2,3,4,5,6}) --> {1, 4, 9, 16, 25, 36}

function filter(pred, tbl)
    local out = {}
    for k,v in pairs(tbl) do if pred(v,k) out[k] = v end end
    return out
end
filter(function(x) return x%2 == 0 end, {1,2,3,4,5,6}) --> {2, 4, 6}

function fold(func, init, tbl)
    for k,v in pairs(tbl) do init = func(init, v,k) end
    return init
end
fold(function(a, x) return a+x end, 0, {1,2,3,4,5,6}) --> 21
But nothing beats the 70ies:

Code: Select all

(loop for i in my-list (do-stuff i))  ; iterate over a list

(loop for k being the hask-keys in my-hash using (hash-value v) ; iterate over a hash
     (do-stuff k v))

(loop for num in my-numbers   ; do all kinds of stuff over a list: 
     collecting (evenp num) into evens  ; filter by a condition
     counting t into count   ; count by a contition (always true here)
     summing num into sum
     maximizing num into max
     minimizing num into min
     finally (return (list :count count :evens evens :sum sum :mean (/ sum count) :min min :max max)))
[/size]

Re: The "I Love Lua, but..." post

Posted: Tue May 03, 2011 7:17 pm
by pekka
ishkabible wrote:how many other languages by convention start there arrays at 1?
Well, Pascal and FORTRAN are two major languages that did this a long time before Lua. They're not yet dead languages either. Pascal has several descendants in wide use and what would kill FORTRAN off? Maybe nuking all FORTRAN programmers from orbit, but I'm not advocating such severe measures :)

Re: The "I Love Lua, but..." post

Posted: Tue May 03, 2011 9:04 pm
by BlackBulletIV
kikito wrote:This language exists today. You do stuff like this on it every time. It is awesome.
Yep, and to iterate over the lines of a file:

Code: Select all

IO.foreach('filename.txt') { |line| puts line }
Or for bigger blocks:

Code: Select all

IO.foreach('filename.txt') do |line|
  # ...
end
But this is the stuff I really like:

Code: Select all

-- take the first argument, remove all quotes, split words into an array, capitalize each word, join them back together into a string
ARGV[0].gsub(/'|"/, '').split.map {|v| v.capitalize }.join
-- generates a random 7 character strings with characters from 0-z
('0'..'z').to_a.shuffle[0..7].join
Gotta love it. :D

Re: The "I Love Lua, but..." post

Posted: Tue May 03, 2011 9:23 pm
by kikito
vrld wrote: But nothing beats the 70ies:
<code>
An elegant weapon from a more ... civilized age.