lua-users home
lua-l archive

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


Here comes the 41 bytes solution:

 

function NVL(x,y)return(y..x):sub(-#x)end

 

Cheers!

 

--javatmn

From: <lua-l-bounces@lists.lua.org> on behalf of Egor Skriptunoff <egor.skriptunoff@gmail.com>
Reply-To: Lua mailing list <lua-l@lists.lua.org>
Date: Thursday, September 29, 2016 at 5:20 AM
To: Lua mailing list <lua-l@lists.lua.org>
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