What techniques that everyone should know?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
User avatar
tio
Citizen
Posts: 61
Joined: Thu Dec 12, 2013 1:04 pm
Location: BR BR?
Contact:

Re: What techniques that everyone should know?

Post by tio »

I don't know if it's a good or a bad practice at all, but I really miss closure-based class behavior in Lua (http://lua-users.org/wiki/ObjectOrienta ... reApproach).

It looks SO better to use since I don't have to deal with '.' or ':' operators (just '.' for everything), and it can have private members and functions as well.
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: What techniques that everyone should know?

Post by davisdude »

Oh, knowing string captures are very important. It would be a good idea to learn them. Here's something I put together as a reference for myself. Feel free to use it if you like.
Attachments
main.lua
(3.18 KiB) Downloaded 88 times
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
User avatar
Ref
Party member
Posts: 702
Joined: Wed May 02, 2012 11:05 pm

Re: What techniques that everyone should know?

Post by Ref »

davisdude wrote:Oh, knowing string captures are very important. It would be a good idea to learn them. Here's something I put together as a reference for myself. Feel free to use it if you like.
Really haven't used them often enough to have figured them all out.
Just created a love from your reference and still have not figured them all out.
Please correct where need.
Attachments
stringFind.love
Just messed up davisdude's code.
(2.1 KiB) Downloaded 90 times
User avatar
Inny
Party member
Posts: 652
Joined: Fri Jan 30, 2009 3:41 am
Location: New York

Re: What techniques that everyone should know?

Post by Inny »

tio wrote:I don't know if it's a good or a bad practice at all, but I really miss closure-based class behavior in Lua (http://lua-users.org/wiki/ObjectOrienta ... reApproach).

It looks SO better to use since I don't have to deal with '.' or ':' operators (just '.' for everything), and it can have private members and functions as well.
I'd use the colon anyway, just because of the semantics of it, i.e. invoking a function versus sending a message.

As for good vs bad practice, closures are a good practice. Great practice even. However you'll notice a big shot to memory usage.
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: What techniques that everyone should know?

Post by davisdude »

Ref, your .love is good, but I have some corrections (no biggies, though. Just little things):
Line 47: %w gets alphanumeric characters, not only upper case letters.
Lines 58 & 60: Correct, but needs emphasis that these can be anything, not just letters.
Line 71: Returns the last capture of that appears 0 or more times.
Line 82: Returns the first capture.

Like I said, no huge problems. They're probably my fault anyway, for not being clear enough.

String captures do have a lot of applicable uses, especially if you're handling user input. Here are some examples:

Code: Select all

function CapitalizeFirstLetter( String, ConditionFunction )
	ConditionFunction = ConditionFunction or function( String ) return String end
	
	local function Function( First, Rest ) 
		if ConditionFunction( First .. Rest ) then 
			return First:upper() .. Rest:lower()	
		end 
	end
	
	return String:gsub( '(%a)([%w_\']*)', Function )
end

CapitalizeFirstLetter( 'this is a test text' ) --> This Is A Test Text
CapitalizeFirstLetter( 'this is a test text', function( String ) return #String > 3 and String end ) --> This is a Test Text
Now let's look at what's happening in the string-portion of this function:

Code: Select all

return String:gsub( '(%a)([%w_\']*)', Function )
So, Sting:gsub( ... ) is the same as string.gsub( String, ... ). Putting parenthesis around the capture makes it an argument for the function (First, Rest). So, what happens is it captures the first letter, then captures the first word, non-alphanumeric, underscore, or " ' " character, and gets all of those. It then passes those to the function. The function makes the first upper-case, and the rest lower case.

Here's another. This one removes white-spaces around the front and back of a text. Don't worry, it's a lot shorter.

Code: Select all

function RemoveEndWhiteSpace( String )
	return String:match( '^[%s]*(.-)[%s]*$' )
end
So here's what this does:
^[%s]* -is just fancy for: return all the spaces at the beginning (^ at the beginning, but not inside the brackets) of the string.
[%s]* -$ means the same, but for the end of the string (as indicated by the $ at the end).
(.-) -This returns the first words that come in-between the spaces at the beginning and end of the word. The - is necessary because if you didn't use it, you would get the spaces at the end as well. That wasn't too bad, was it?

Code: Select all

function RemoveExtraWhiteSpace( String, ConditionFunction )
	ConditionFunction = ConditionFunction or function( String ) return String end
	
	local function Function( First, Last )
		if ConditionFunction( First .. Last ) then
			if #Last > 0 then
				return ' '
			end
			
			return First .. Last
		end
	end
	
	String = String:gsub( '(%s)(%s+%w-)', Function )
	
	return RemoveEndWhiteSpace( String )
end
Here's the string part:

Code: Select all

String = String:gsub( '(%s)(%s+%w-)', Function )
This captures the first space as the first argument, and then a space and the first alphanumeric character.

There are more, but those are some basic examples.

TL;DR- Just read it. You may learn something. :)
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
User avatar
Ref
Party member
Posts: 702
Joined: Wed May 02, 2012 11:05 pm

Re: What techniques that everyone should know?

Post by Ref »

Thanks Davisdude!
My biggest problem is that I've really not had to use these string functions often enough.
So, I have to look them up each time and relearn them again each time.
Having them in one place really helps.
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: What techniques that everyone should know?

Post by davisdude »

No problem! I have some trouble remembering them too (that's why I made the file). I usually keep all of my semi-complete things in a file to remember how I do things, or in most cases, how I shouldn't do things! :shock:
I think it's good to keep a record of your progress in programming. After a while, you won't need your initial Hello World! game anymore, but some things are still good to have around.
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
Zarty55
Citizen
Posts: 79
Joined: Thu Jul 25, 2013 2:36 am

Re: What techniques that everyone should know?

Post by Zarty55 »

I'm not sure if this would make a shallow copy of a table, but I will put it here:

Code: Select all

function copyTable(t)
  return {unpack(t)}
end
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: What techniques that everyone should know?

Post by Robin »

Yes, but it will make a shallow copy of a sequence. If your table contains any non-sequence key/value pairs and you want to copy those too, you'd use:

Code: Select all

function shallowCopy(t)
    local n = {}
    for k, v in pairs(t) do
        n[k] = v
    end
    return n
end
Help us help you: attach a .love.
User avatar
tio
Citizen
Posts: 61
Joined: Thu Dec 12, 2013 1:04 pm
Location: BR BR?
Contact:

Re: What techniques that everyone should know?

Post by tio »

Zarty55 wrote:I'm not sure if this would make a shallow copy of a table, but I will put it here:

Code: Select all

function copyTable(t)
  return {unpack(t)}
end
Robin wrote:Yes, but it will make a shallow copy of a sequence. If your table contains any non-sequence key/value pairs and you want to copy those too, you'd use:

Code: Select all

function shallowCopy(t)
    local n = {}
    for k, v in pairs(t) do
        n[k] = v
    end
    return n
end
Works considering that the table doesn't have any inner tables ;) (It will work, but will point to the tables of the first list, which may not be the desirable behavior).

One option to duplicate these tables too would be a recursive call, like this:

Code: Select all

function shallowCopy(t)
    local n = {}
    for k, v in pairs(t) do
        if type(v) == "table" then
            n[k] = shallowCopy(v)
        else
            n[k] = v
        end
    end
    return n
end
Post Reply

Who is online

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