x < y < z

classic Classic list List threaded Threaded
15 messages Options
Reply | Threaded
Open this post in threaded view
|

x < y < z

Rena
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.

Reply | Threaded
Open this post in threaded view
|

Re: x < y < z

Tim Caswell
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 (< (<x y) z) to simulate the lua 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.

On Thu, Jul 16, 2015 at 5: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.


Reply | Threaded
Open this post in threaded view
|

Re: x < y < z

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 (< (<x y) z) to simulate the lua 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.

/s/ Adam

Reply | Threaded
Open this post in threaded view
|

Re: x < y < z

Tim Hill
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


Reply | Threaded
Open this post in threaded view
|

Re: x < y < z

Tom N Harris-2
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]>

Reply | Threaded
Open this post in threaded view
|

Re: x < y < z

Tim Hill

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

Re: x < y < z

Pierre Chapuis
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 (< (<x y) z) to simulate the lua
>> 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


Reply | Threaded
Open this post in threaded view
|

Re: x < y < z

Thomas Jericke
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 (< (<x y) z) to simulate the lua
>>> 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

Reply | Threaded
Open this post in threaded view
|

Re: x < y < z

Chris Emerson
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

Reply | Threaded
Open this post in threaded view
|

Re: x < y < z

Dirk Laurie-2
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 (< (<x y) z) to simulate the lua
>>> 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.

Reply | Threaded
Open this post in threaded view
|

Re: x < y < z

Alex Queiroz
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/

Reply | Threaded
Open this post in threaded view
|

Re: x < y < z

Peter Aronoff
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

Reply | Threaded
Open this post in threaded view
|

Re: x < y < z

Dirk Laurie-2
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.

Reply | Threaded
Open this post in threaded view
|

Re: x < y < z

voidptr
In reply to this post by Pierre Chapuis


On 2015-07-17 5: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 (< (<x y) z) to simulate the lua
>>> 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 :)
>
   if y in ]x,z] then  is more saying   if y in include in aRangeObject

now introducing Range, can be alot more fun ...  and complex :o),
ranges can be use for ..

range tests, enumerator in some forloop, initialization, indexing array/table ... (check few with python)
beside be use for numbers, int, float, letters(remind pattern a-zA-Z) ...

can range be extent to be use a 2 dimensions (or n dimensions) ?
ex. range for complex number?  can be a line between the 2 numbers, or a rectangle of values between them (now this is more like a constrain domain)...

range or domain of constrain fct ? [ f(x), f(y) ]

etc...etc...

:o)









Reply | Threaded
Open this post in threaded view
|

Re: x < y < z

voidptr
In reply to this post by Pierre Chapuis

imagine combine range with a cute switch
(please please can we have a switch instead of elseif ... please please)

switch y
     case  ]x,z[  :   -- doit
     end
     case [x,z] :
     end
     case {1,2,3,f(4)}:  -- in {}
     end
     case !{1,2,3} and ]x,z[  :  --not in {} but in ]'[
     end
end

:o)