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

General discussion about LÖVE, Lua, game development, puns, and unicorns.
User avatar
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

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

Post by BlackBulletIV » Sun May 01, 2011 9:40 pm

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.

User avatar
ishkabible
Party member
Posts: 241
Joined: Sat Oct 23, 2010 7:34 pm
Location: Kansas USA

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

Post by ishkabible » Mon May 02, 2011 10:43 pm

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?

User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

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

Post by Robin » Tue May 03, 2011 9:08 am

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.
Help us help you: attach a .love.

User avatar
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

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

Post by BlackBulletIV » Tue May 03, 2011 9:20 am

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).

User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

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

Post by vrld » Tue May 03, 2011 11:49 am

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 */ }
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine

User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

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

Post by kikito » Tue May 03, 2011 12:08 pm

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.
When I write def I mean function.

User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

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

Post by vrld » Tue May 03, 2011 1:16 pm

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]
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine

pekka
Party member
Posts: 206
Joined: Thu Jan 07, 2010 6:48 am
Location: Oulu, Finland
Contact:

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

Post by pekka » Tue May 03, 2011 7:17 pm

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 :)

User avatar
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

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

Post by BlackBulletIV » Tue May 03, 2011 9:04 pm

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

User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

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

Post by kikito » Tue May 03, 2011 9:23 pm

vrld wrote: But nothing beats the 70ies:
<code>
An elegant weapon from a more ... civilized age.
When I write def I mean function.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 11 guests