lua-users home
lua-l archive

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




On 29/09/16 03:26 PM, Egor Skriptunoff wrote:
On Thu, Sep 29, 2016 at 9:33 AM, Yong Wang <javatmn@gmail.com <mailto:javatmn@gmail.com>> wrote:

    here is 41-byte solution:

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


Congratulations!
You've found it!
I believe it was not easy.

P.S.
This solution is based on strange behavior of string.sub.

IMO, "string.sub(index_from, index_to)" will be more handy if "index_from = 0'
would mean "the index after the last character"
instead of "the index before the first character".
("index_to = 0" should mean "index before the first character", as it is currently implemented in Lua)

The unexpected result of "str:sub(0)" makes programming a bit harder,
as this "feature" is actually a trap, and you are compelled
to use additional "if" to make things right.
But surprisingly, this weird logic can be beneficial in codegolf :-)


Don't you mean to say Lua should add a special-case for -0 and go against the rule of treating 0 and -0 as equal?

--
Disclaimer: these emails may be made public at any given time, with or without reason. If you don't agree with this, DO NOT REPLY.