# x < y < z

15 messages
Open this post in threaded view
|

## x < y < z

 This is something I've often wished to have in various languages, and I accidentally discovered that it's valid syntax in Lua: if x < y < z then... Of course, it doesn't actually work, because it evaluates as "(x < y) < z", and "(x < y)" evaluates to a boolean. (I couldn't find a way to make clever metamethods that would allow it to work either, since __le's result is always converted to a boolean, so you lose the values of x and y.) (The idea of course is that it evaluates as "x < y and y < z".) Just something I noticed and thought was interesting. -- Sent from my Game Boy.
Open this post in threaded view
|

## Re: x < y < z

 This is one of those areas where languages without infix notation, but variable-length arguments (like lisp) smile.  You can just do (< x y z) and it will do what you expect or (< (
Open this post in threaded view
|

## Re: x < y < z

 On Thu, Jul 16, 2015 at 4:00 PM, Tim Caswell <[hidden email]> wrote: > This is one of those areas where languages without infix notation, but > variable-length arguments (like lisp) smile.  You can just do (< x y z) and > it will do what you expect or (< ( > But then you have to give up infix notation and/or have crazy conventions > like {x < y < z} means to transform to (< x y z) if curly braces are used > and every odd symbol is the same. It's also possible to do it at the grammar level, defining the comparison operators as being optionally ternary. /s/ Adam
Open this post in threaded view
|

## Re: x < y < z

 In reply to this post by Rena > On Jul 16, 2015, at 3:56 PM, Rena <[hidden email]> wrote: > > This is something I've often wished to have in various languages, and > I accidentally discovered that it's valid syntax in Lua: > > if x < y < z then... > > Of course, it doesn't actually work, because it evaluates as "(x < y) > < z", and "(x < y)" evaluates to a boolean. (I couldn't find a way to > make clever metamethods that would allow it to work either, since > __le's result is always converted to a boolean, so you lose the values > of x and y.) > > (The idea of course is that it evaluates as "x < y and y < z".) > > Just something I noticed and thought was interesting. > > -- > Sent from my Game Boy. > For me this is one where clarity wins over terseness. You aren’t getting any run-time benefit, since the logic is just the same. Really this is just saving a tiny bit of typing, but making the language more complex. —Tim
Open this post in threaded view
|

## Re: x < y < z

 On Thursday, July 16, 2015 05:52:30 PM Tim Hill wrote: > You aren’t getting any run-time benefit, since the logic is just the same. There is a small advantage with  `minimum <= ComputeThisValue() <= maximum` in that the VM can store the result in a temporary register for both compares. The same thing would require declaring a local for the value and occupying that register until the end of the scope. -- tom <[hidden email]>
Open this post in threaded view
|

## Re: x < y < z

 > On Jul 16, 2015, at 8:20 PM, Tom N Harris <[hidden email]> wrote: > > On Thursday, July 16, 2015 05:52:30 PM Tim Hill wrote: >> You aren’t getting any run-time benefit, since the logic is just the same. > > There is a small advantage with  `minimum <= ComputeThisValue() <= maximum` in > that the VM can store the result in a temporary register for both compares. > The same thing would require declaring a local for the value and occupying > that register until the end of the scope. > > -- > tom <[hidden email]> > But is that small advantage worth the extra complexity in the language?
Open this post in threaded view
|

## Re: x < y < z

 In reply to this post by Coda Highland > On Thu, Jul 16, 2015 at 4:00 PM, Tim Caswell <[hidden email]> wrote: >> This is one of those areas where languages without infix notation, but >> variable-length arguments (like lisp) smile.  You can just do (< x y z) >> and >> it will do what you expect or (< (> behaviour. >> >> But then you have to give up infix notation and/or have crazy >> conventions >> like {x < y < z} means to transform to (< x y z) if curly braces are >> used >> and every odd symbol is the same. > > It's also possible to do it at the grammar level, defining the > comparison operators as being optionally ternary. If you allow that, you probably also want to allow x < y <= z and the like. I don't think the complexity is really worth it, but if a language is going to support such things it could go further and do something like:     if y in ]x,z] then which would also work with things like:     if y in {3, 5, 7, 9} then I don't think Lua is or should be that language though :) -- Pierre Chapuis
Open this post in threaded view
|

## Re: x < y < z

 On 07/17/2015 11:56 AM, Pierre Chapuis wrote: >> On Thu, Jul 16, 2015 at 4:00 PM, Tim Caswell <[hidden email]> wrote: >>> This is one of those areas where languages without infix notation, but >>> variable-length arguments (like lisp) smile.  You can just do (< x y z) >>> and >>> it will do what you expect or (< (>> behaviour. >>> >>> But then you have to give up infix notation and/or have crazy >>> conventions >>> like {x < y < z} means to transform to (< x y z) if curly braces are >>> used >>> and every odd symbol is the same. >> It's also possible to do it at the grammar level, defining the >> comparison operators as being optionally ternary. > If you allow that, you probably also want to allow x < y <= z and the like. > > I don't think the complexity is really worth it, but if a language is > going to support such things it could go further and do something like: > >      if y in ]x,z] then > > which would also work with things like: > >      if y in {3, 5, 7, 9} then > > I don't think Lua is or should be that language though :) > Lua is that language if you want to, you just need to set a metatable to type "number" and a ":" in the right place. if y :in (x,z) then if y :in {3, 5, 7, 9} then you just don't have the [] available :-( -- Thomas
Open this post in threaded view
|

## Re: x < y < z

 In reply to this post by Rena On Thu, Jul 16, 2015 at 06:56:08PM -0400, Rena wrote: > This is something I've often wished to have in various languages, and > I accidentally discovered that it's valid syntax in Lua: > > if x < y < z then... I know one language that allows that (Python). Chris
Open this post in threaded view
|

## Re: x < y < z

 In reply to this post by Pierre Chapuis 2015-07-17 11:56 GMT+02:00 Pierre Chapuis <[hidden email]>: >> On Thu, Jul 16, 2015 at 4:00 PM, Tim Caswell <[hidden email]> wrote: >>> This is one of those areas where languages without infix notation, but >>> variable-length arguments (like lisp) smile.  You can just do (< x y z) >>> and >>> it will do what you expect or (< (>> behaviour. >>> >>> But then you have to give up infix notation and/or have crazy >>> conventions >>> like {x < y < z} means to transform to (< x y z) if curly braces are >>> used >>> and every odd symbol is the same. >> >> It's also possible to do it at the grammar level, defining the >> comparison operators as being optionally ternary. > > If you allow that, you probably also want to allow x < y <= z and the like. APL has an operator "scan" over arrays so that    fct / x returns the value of a function that can be coded in Lua as function scan(fct,x)    result = unit(fct)    for _,v in ipairs(x) do result = fct(result,v) end    return result end There may well be som such function in Penlight and Microlight.
Open this post in threaded view
|

## Re: x < y < z

 On 17 July 2015 at 13:06, Dirk Laurie <[hidden email]> wrote: > > APL has an operator "scan" over arrays so that > >    fct / x > > returns the value of a function that can be coded in Lua as > > function scan(fct,x) >    result = unit(fct) >    for _,v in ipairs(x) do result = fct(result,v) end >    return result > end > https://en.wikipedia.org/wiki/Fold_(higher-order_function) -- -alex http://unendli.ch/
Open this post in threaded view
|

## Re: x < y < z

 In reply to this post by Dirk Laurie-2 Dirk Laurie <[hidden email]> wrote: > APL has an operator "scan" over arrays so that > >    fct / x > > returns the value of a function that can be coded in Lua as > > function scan(fct,x) >    result = unit(fct) >    for _,v in ipairs(x) do result = fct(result,v) end >    return result > end I'm sorry if I'm being dense, but what is `unit` there? > There may well be som such function in Penlight and Microlight. As Alex Q. says in another reply, scan looks like a fold/reduce. A number of utility libraries do provide such a function. Penlight and Underscore definitely do, for example. P -- We have not been faced with the need to satisfy someone else's requirements, and for this freedom we are grateful.     Dennis Ritchie and Ken Thompson, The UNIX Time-Sharing System
Open this post in threaded view
|

## Re: x < y < z

 2015-07-17 16:02 GMT+02:00 Peter Aronoff <[hidden email]>: > Dirk Laurie <[hidden email]> wrote: >> APL has an operator "scan" over arrays so that >> >>    fct / x >> >> returns the value of a function that can be coded in Lua as >> >> function scan(fct,x) >>    result = unit(fct) >>    for _,v in ipairs(x) do result = fct(result,v) end >>    return result >> end > > I'm sorry if I'm being dense, but what is `unit` there? If u = unit(fct), then fct(u,x)=x for all x. E.g. unit(__add) = 0, unit(__mul) = 1, unit(__min) = Inf, etc. It is only really needed when x is empty, but simplifies the coding.