Page 7 of 8

Re: What techniques that everyone should know?

Posted: Sat Mar 08, 2014 1:01 am
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.

Re: What techniques that everyone should know?

Posted: Sat Mar 08, 2014 3:46 am
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.

Re: What techniques that everyone should know?

Posted: Sat Mar 08, 2014 4:47 pm
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.

Re: What techniques that everyone should know?

Posted: Sat Mar 08, 2014 5:28 pm
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.

Re: What techniques that everyone should know?

Posted: Sat Mar 08, 2014 9:59 pm
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. :)

Re: What techniques that everyone should know?

Posted: Sat Mar 08, 2014 11:14 pm
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.

Re: What techniques that everyone should know?

Posted: Sat Mar 08, 2014 11:42 pm
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.

Re: What techniques that everyone should know?

Posted: Tue Apr 01, 2014 6:21 pm
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

Re: What techniques that everyone should know?

Posted: Tue Apr 01, 2014 6:42 pm
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

Re: What techniques that everyone should know?

Posted: Tue Apr 01, 2014 8:17 pm
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