Lua As Assembler
I’ve said it before: Let’s use Lua as our new “assembler”.
Meaning: Let’s package algorithms in LUA to make them portable… safely (Lua code runs in an actually tight “sandbox”. There really is no way out of that — other than by returning stuff.)
Here’s an example: Image recognition (pictures) in Lua. I can embed this code directly in a JavaX program, JavaX supports that everywhere.
-- Input:
-- iw = image of word to recognize
-- chars = images of characters & description
-- Output:
-- what is recognized (a string)
-- helper function
function preciseMatch(ic, iw, x1)
local w, h = iw.w, iw.h
if ic.h ~= h then return false end
local x2 = x1+ic.w
if x2 > w then return false end
for x = x1, x2-1 do
for y = 0, h-1 do
if ic.getBrightness(x-x1, y) ~= iw.getBrightness(x, y) then
return false
end
end
end
return true
end
-- main algorithm
local buf = {}
for x=0, iw.w-1 do
for ic, c in pairs(chars) do
if preciseMatch(ic, iw, x) then
table.insert(buf, c)
x = x + ic.w-1
goto xloop
end
end
::xloop::
end
return table.concat(buf)-- done
Quite short too, innit? Sadly only scores “4 out of 9”. :-) (Do better!!!!)