Re: Small extra functions
Posted: Tue Apr 22, 2014 2:29 pm
Substitute541, you might want to consider add table.sort(a) to median function - just nit picking.
Code: Select all
chars = function(text)
local i=0
local n = #text
--print(text)
return function()
i=i+1
if i <= n then return string.sub(text,i,i),i end
end
end
words = function(text,spaces)
local i=0
local wordss = {}
local onspace = false
local word = {}
for l,p in chars(text) do
if l == ' ' then
if not onspace then
if #word > 0 then
wordss[#wordss+1] = table.concat(word)
end
if spaces then
word = {' '}
else
word = {}
end
onspace = true
else
if spaces then
word[#word+1] = ' '
end
end
else
if onspace then
if #word > 0 then
wordss[#wordss+1] = table.concat(word)
end
word = {l}
onspace = false
else
word[#word+1] = l
end
end
end
if #word > 0 then
wordss[#wordss+1] = table.concat(word)
end
return function()
i = i + 1
if i <= #wordss then return wordss[i] end
end
end
Code: Select all
for l,p in chars('hello') do
print('Character, "'..l..'" at position '..p)
end
Code: Select all
Character, "h" at position 1
Character, "e" at position 2
Character, "l" at position 3
Character, "l" at position 4
Character, "o" at position 5
Code: Select all
for w,p in words('hello there you') do
print('word, "'..l..'"')
end
Code: Select all
word, "hello"
word, "there"
word, "you"
Code: Select all
for w in words('hello there you',true) do
print('word, "'..l..'"')
end
Code: Select all
word, "hello"
word, " "
word, "there"
word, " "
word, "you"
Code: Select all
local str = 'hello'
for char in str:gmatch('.') do
print('character '..char)
end
Code: Select all
local str = 'HeLlO'
for uppercase_char in str:gmatch('%u') do
print('character '..uppercase_char)
end
Code: Select all
local function gmatch(s,pat)
local i = 0
local match = string.gmatch(s,pat)
return function()
i = i+1
local capture = match()
if capture then return i, capture end
end
end
-- example, provide de the count of characters in a string
local str = 'hello'
for count, char in gmatch(str,'.') do
print('character '..char, 'count '..count)
end
-- Or count the lowercase characters
local str = 'HeLlO'
for count, char in gmatch(str,'%l') do
print('character '..char, 'count '..count)
end
Code: Select all
str = 'hello the world'
for w in str:gmatch('%w+') do
print('word '..w)
end
Code: Select all
str = 'hello the world'
for w in str:gmatch('%s*([^%s]+)%s*') do
print('word '..w)
end
Code: Select all
function string:split( sep )
local sep, fields = sep or ",", {}
local pattern = string.format( "([^%s]+)", sep )
self:gsub( pattern, function(c) fields[ #fields + 1 ] = c end )
return fields
end
Code: Select all
for i, v in pairs(field) do
if string.sub(v, -1, -1) == "." then
v = string.sub(v, 1, -2)
end
end
Ref wrote:EDIT: Both this approach and Roland's has the problem that if there is a punctuation mark at the end of the string, it will be included in the last 'word'.
Code: Select all
local str = 'hello the world.'
for w in str:gmatch('(%w+)%p*') do
print('word '..w)
end
Ah, Great Ref is soo right...Ref wrote:I guess you can always come up with a problem if you really want.
Well, in that case, I can just wrap string.gmatch again in an interator function that will trim any ending punctuation character on each capture before returning it.Ref wrote: Roland, your last version can't handle "The sample was 12.56 cm long!".
It breaks up 12.56 into 12 and 56.
Code: Select all
local function gmatch(s,pat)
local match = string.gmatch(s,pat)
return function()
local capture = match()
if capture then return (capture:gsub(('%p$'),'')) end
end
end
local str = 'The sample was 12.56 cm long!'
for w in gmatch(str,'([^%s]+)') do
print(w)
end
Code: Select all
function love.graphics.shape(mode, x, y, rd, sides)
local points = {}
for i = 1, math.ceil(sides) do
local px = x
local py = y
local a = math.pi*2/sides*i
px = px + math.cos(a)*rd
py = py + math.sin(a)*rd
table.insert(points, px)
table.insert(points, py)
end
love.graphics.polygon(mode, points)
return points
end
Code: Select all
local TPI = math.pi*2
local PI = math.pi
local math_abs = math.abs
local math_floor = math.floor
function fastSin(x)
local tpi = TPI
local pi = PI
local t = math_abs(-2*math_floor(x/tpi+0.75) + x/pi + 0.5)
return 2*t*t*(3-t*2)-1
end
function fastCos(x)
local tpi = TPI
local pi = PI
local t = math_abs(-2*math_floor(x/tpi+1) + x/pi + 1)
return 2*t*t*(3-t*2)-1
end