[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Random thought: short lambda syntax (no patching required!)
- From: Rena <hyperhacker@...>
- Date: Sun, 25 May 2014 20:39:10 -0400
Just hacked this up on a whim:
function L(c)
return assert(loadstring(
"local args={...}; return " .. c:gsub('%$(%d+)', 'args[%1]')
))
end
local t={'bananas', 'watermelons', 'pears', 'grapes'}
table.sort(t, L"$1 > $2")
for k,v in pairs(t) do print(k,v) end
Maybe it's not the most elegant (hope your function never uses a $ in a string) but I thought it was a neat little hack and a bit nicer looking than:
table.sort(t, function(a, b) return a > b end)
--
Sent from my Game Boy.