lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

On 16-09-28 02:20 PM, Egor Skriptunoff wrote:
> Your task is to implement string-based function NVL(x,y) in Lua.
> Both arguments of NVL are always strings.
> NVL returns its first argument if it is not empty string, otherwise it
> returns its second argument.
> function NVL(x,y)return x==""and y or x end
> The length of source code of this implementation is 43 bytes.
> Your task is to fit your implementation in smaller size.

I've found somewhat funny but 1 and 2 chars longer variants:

function NVL(x,y)return({['']=y})[x]or x end
function NVL(x,y)return({[x]=x,['']=y})[x]end

On 16-09-28 02:28 PM, Soni L. wrote:
> NVL=load"return...==''and({...})[2]or..."

Nice solution, Soni.