What techniques that everyone should know?
Re: What techniques that everyone should know?
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.
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?
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 108 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
Re: What techniques that everyone should know?
Really haven't used them often enough to have figured them all out.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.
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 110 times
Re: What techniques that everyone should know?
I'd use the colon anyway, just because of the semantics of it, i.e. invoking a function versus sending a message.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.
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?
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:
Now let's look at what's happening in the string-portion of this 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.
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?
Here's the string part:
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.
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
Code: Select all
return String:gsub( '(%a)([%w_\']*)', Function )
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
^[%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
Code: Select all
String = String:gsub( '(%s)(%s+%w-)', Function )
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
Re: What techniques that everyone should know?
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.
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?
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!
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.
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
Re: What techniques that everyone should know?
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
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: What techniques that everyone should know?
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.
Re: What techniques that everyone should know?
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
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).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
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
Who is online
Users browsing this forum: No registered users and 6 guests