# [FUN] A bit of code golfing

 Classic List Threaded
37 messages
12
Reply | Threaded
Open this post in threaded view
|

## [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 endThe 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
Reply | Threaded
Open this post in threaded view
|

## Re: [FUN] A bit of code golfing

 On 28/09/16 06:20 PM, Egor Skriptunoff wrote: > 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 NVL=load"return...==''and({...})[2]or..." -- 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.
Reply | Threaded
Open this post in threaded view
|

## Re: [FUN] A bit of code golfing

 On Thu, Sep 29, 2016 at 12:28 AM, Soni L. wrote: NVL=load"return...==''and({...})[2]or..." Bravo!Lets note that my solutions are Lua 5.1 compatible, and using "load" instead of "function" can reduce their size.As for now, two tasks to solve:1) Find 41-byte solution compatible with Lua 5.1, 5.2 and 5.32) Find 36-byte solution for latest version of Lua
Reply | Threaded
Open this post in threaded view
|

## Re: [FUN] A bit of code golfing

 In reply to this post by Egor Skriptunoff-2 function NVL(x,y)return({y})[#x+1]or x end   (I assume you mean without shortening the function name to one letter)   From: [hidden email] 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 endThe 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
Reply | Threaded
Open this post in threaded view
|

## Re: [FUN] A bit of code golfing

 In reply to this post by Egor Skriptunoff-2 On 28/09/16 07:06 PM, Egor Skriptunoff wrote: > On Thu, Sep 29, 2016 at 12:28 AM, Soni L. <[hidden email] > > wrote: > > >     NVL=load"return...==''and({...})[2]or..." > > Bravo! > > Lets note that my solutions are Lua 5.1 compatible, and using "load" > instead of "function" can reduce their size. > > As for now, two tasks to solve: > 1) Find 41-byte solution compatible with Lua 5.1, 5.2 and 5.3 > 2) Find 36-byte solution for latest version of Lua > NVL=load"return({...})[#...+2]or..." -- 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.
Reply | Threaded
Open this post in threaded view
|

## Re: [FUN] A bit of code golfing

 In reply to this post by Tony Papadimitriou On Thu, Sep 29, 2016 at 1:17 AM, Tony Papadimitriou wrote: function NVL(x,y)return({y})[#x+1]or x end  Yes!This is exactly my 42-byte solution.
Reply | Threaded
Open this post in threaded view
|

## Re: [FUN] A bit of code golfing

 In reply to this post by Soni "They/Them" L. On Thu, Sep 29, 2016 at 1:43 AM, Soni L. wrote: On 28/09/16 07:06 PM, Egor Skriptunoff wrote: As for now, two tasks to solve: 1) Find 41-byte solution compatible with Lua 5.1, 5.2 and 5.3 2) Find 36-byte solution for latest version of Lua NVL=load"return({...})[#...+2]or..."OK, 36-byte solution for latest Lua version has been found (exactly as mine one)!Only one task is remaining to solve.Try to find 41-byte solution for Lua 5.1My solution for the remaining task is based on some Lua feature which looks like a design bug for me.
Reply | Threaded
Open this post in threaded view
|

## Re: [FUN] A bit of code golfing

 On 28/09/16 07:53 PM, Egor Skriptunoff wrote: > On Thu, Sep 29, 2016 at 1:43 AM, Soni L. <[hidden email] > > wrote: > > >     On 28/09/16 07:06 PM, Egor Skriptunoff wrote: > > >         As for now, two tasks to solve: >         1) Find 41-byte solution compatible with Lua 5.1, 5.2 and 5.3 >         2) Find 36-byte solution for latest version of Lua > >     NVL=load"return({...})[#...+2]or..." > > OK, 36-byte solution for latest Lua version has been found (exactly as > mine one)! > > Only one task is remaining to solve. > Try to find 41-byte solution for Lua 5.1 > My solution for the remaining task is based on some Lua feature which > looks like a design bug for me. > Hmm... This is a 42-byte one, but I cannot seem to find a smaller one: function NVL(a,b)return(a:gsub("^\$",b))end Except for this one, but it returns an extra value: function NVL(a,b)return a:gsub("^\$",b)end (Altho still passes the test cases) -- 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.
Reply | Threaded
Open this post in threaded view
|

## Re: [FUN] A bit of code golfing

 On Thu, Sep 29, 2016 at 2:07 AM, Soni L. wrote: On 28/09/16 07:53 PM, Egor Skriptunoff wrote: Only one task is remaining to solve. Try to find 41-byte solution for Lua 5.1 My solution for the remaining task is based on some Lua feature which looks like a design bug for me. Hmm... This is a 42-byte one, but I cannot seem to find a smaller one: function NVL(a,b)return(a:gsub("^\$",b))end Except for this one, but it returns an extra value: function NVL(a,b)return a:gsub("^\$",b)end (Altho still passes the test cases)Nice trick with gsub!But NVL must return single value.
Reply | Threaded
Open this post in threaded view
|

## Re: [FUN] A bit of code golfing

 In reply to this post by Egor Skriptunoff-2 > Only one task is remaining to solve. > Try to find 41-byte solution for Lua 5.1 I found one: function NVL(...)return arg[#x+2]or x end Valentin
Reply | Threaded
Open this post in threaded view
|

## Re: [FUN] A bit of code golfing

 Valentin wrote: >> Only one task is remaining to solve. >> Try to find 41-byte solution for Lua 5.1 > > I found one: > > function NVL(...)return arg[#x+2]or x end Oops, my bad, obviously BS, had x and y defined as globals. Please just ignore ;)
Reply | Threaded
Open this post in threaded view
|

## Re: [FUN] A bit of code golfing

 In reply to this post by Valentin On Thu, Sep 29, 2016 at 2:59 AM, Valentin wrote:> Only one task is remaining to solve. > Try to find 41-byte solution for Lua 5.1 I found one: function NVL(...)return arg[#x+2]or x end It says "attempt to get length of global 'x' (a nil value)"
Reply | Threaded
Open this post in threaded view
|

## Re: [FUN] A bit of code golfing

 Egor Skriptunoff wrote: > On Thu, Sep 29, 2016 at 2:59 AM, Valentin <[hidden email]> wrote: > >> > Only one task is remaining to solve. >> > Try to find 41-byte solution for Lua 5.1 >> >> I found one: >> >> function NVL(...)return arg[#x+2]or x end >> >> It says "attempt to get length of global 'x' (a nil value)" Yes, realized directly after posting, sorry. This one works, but has 43 bytes, sigh ;) function NVL(x,...)return arg[#x+1]or x end
Reply | Threaded
Open this post in threaded view
|

## Re: [FUN] A bit of code golfing

 In reply to this post by Egor Skriptunoff-2 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.
Reply | Threaded
Open this post in threaded view
|

## Re: [FUN] A bit of code golfing

 In reply to this post by Egor Skriptunoff-2 Here comes the 41 bytes solution: function NVL(x,y)return(y..x):sub(-#x)end Cheers! --javatmnFrom: <[hidden email]> on behalf of Egor Skriptunoff <[hidden email]>Reply-To: Lua mailing list <[hidden email]>Date: Thursday, September 29, 2016 at 5:20 AMTo: Lua mailing list <[hidden email]>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 endThe 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
Reply | Threaded
Open this post in threaded view
|

## Re: [FUN] A bit of code golfing

 In reply to this post by Egor Skriptunoff-2 Hi,here is 41-byte solution:function NVL(x,y)return(y..x):sub(-#x)endCheers!--Yong WANG, [hidden email]2016-09-29 5:20 GMT+08:00 Egor Skriptunoff :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 endThe 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
Reply | Threaded
Open this post in threaded view
|

## Re: [FUN] A bit of code golfing

 On Thu, Sep 29, 2016 at 9:33 AM, Yong Wang wrote:here is 41-byte solution:function NVL(x,y)return(y..x):sub(-#x)endCongratulations!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 :-)
Reply | Threaded
Open this post in threaded view
|

## Re: [FUN] A bit of code golfing

 2016-09-29 20:26 GMT+02:00 Egor Skriptunoff <[hidden email]>: > This solution is based on strange behavior of string.sub. Not every one will agree that the behaviour is strange. For example, I disagree. The behaviour is clearly documented. > 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 :-) If 1 is the index at the first character, it is fully consistent that 0 is the index before the first character. Any other behaviour would be strange, unexpected, surprising and weird. My conclusion is rather that codegolf underlines the unremitting consistency in the design of the Lua string library :-)
Reply | Threaded
Open this post in threaded view
|

## Re: [FUN] A bit of code golfing

 In reply to this post by Egor Skriptunoff-2 On 29/09/16 03:26 PM, Egor Skriptunoff wrote: > On Thu, Sep 29, 2016 at 9:33 AM, Yong Wang <[hidden email] > > 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.
Reply | Threaded
Open this post in threaded view
|

## Re: [FUN] A bit of code golfing

 2016-09-29 21:16 GMT+02:00 Soni L. <[hidden email]>: > > > On 29/09/16 03:26 PM, Egor Skriptunoff wrote: >> 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) > 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? It would have to be -0.0 in Lua 5.3.
12