lua-users home
lua-l archive

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


function NVL(x,y)return({y})[#x+1]or x end
 
(I assume you mean without shortening the function name to one letter)
 
Sent: Thursday, September 29, 2016 12:20 AM
Subject: [FUN] A bit of code golfing
 
Hi!

I have a small programming problem for you to solve, just for fun.

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.

assert(NVL("Hello", "World") == "Hello")
assert(NVL("", "Lua") == "Lua")


An example of implementation:

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.

NVL seems to be a simple stupid function, but nevertheless it can be optimized.
I have two different solutions: 42 and 41 bytes.  Try to find either of them.

-- Egor