• Subject: Re: Numeric for loop with rationals
• From: "Soni \"They/Them\" L." <fakedme@...>
• Date: Tue, 30 Jan 2018 12:55:18 -0200

```

On 2018-01-30 12:48 PM, Charles Heywood wrote:
```
```I am confused on why you think you can't:

Lua 5.3.4  Copyright (C) 1994-2017 Lua.org, PUC-Rio
> function one() return 1 end
> function two() return 2 end
> function ten() return 10 end
> for i=one(), ten(), two() do print(i) end
1
3
5
7
9
>
```
```
Lua 5.3.4  Copyright (C) 1994-2017 Lua.org, PUC-Rio
```
> meta = {__add=function(x, y) return setmetatable({i=x.i + y.i}, meta) end, __lt=function(x, y) return x.i < y.i end}
```> function one() return setmetatable({i=1}, meta) end
> function two() return setmetatable({i=2}, meta) end
> function ten() return setmetatable({i=10}, meta) end
> for i=one(), two(), ten() do print(i) end
stdin:1: 'for' limit must be a number
stack traceback:
stdin:1: in main chunk
[C]: in ?
> local i,j,k=one(), ten(), two() while i < j do print(i) i = i + k end
table: 0x558de8c74080
table: 0x558de8c74200
table: 0x558de8c747b0
table: 0x558de8c74890
table: 0x558de8c74970
> local i,j,k=one(), ten(), two() while i < j do print(i.i) i = i + k end
1
3
5
7
9

Rationals don't work. Only floats and ints do. This sucks.

```
```
```
On Tue, Jan 30, 2018 at 7:08 AM Soni "They/Them" L. <fakedme@gmail.com <mailto:fakedme@gmail.com>> wrote:
```

On 2018-01-30 11:04 AM, Dirk Laurie wrote:
> 2018-01-30 14:55 GMT+02:00 Soni "They/Them" L.
<fakedme@gmail.com <mailto:fakedme@gmail.com>>:
>>
>> On 2018-01-30 10:51 AM, Dirk Laurie wrote:
>>> 2018-01-30 14:23 GMT+02:00 Soni "They/Them" L.
<fakedme@gmail.com <mailto:fakedme@gmail.com>>:
>>>>
>>>> On 2018-01-30 06:58 AM, Francisco Olarte wrote:
>>>>> On Tue, Jan 30, 2018 at 1:11 AM, Soni "They/Them" L.
<fakedme@gmail.com <mailto:fakedme@gmail.com>>
>>>>> wrote:
>>>>> ...
>>>>>> Also, rationals are still numbers. They're just not "Lua
numbers"
>>>>>> (objects
>>>>>> with type(x) == "number"). Any language with operator
>>>>>> C++)
>>>>>> lets me have numeric for with rationals. Except Lua.
(Python doesn't
>>>>>> have
>>>>>> numeric for at all so it doesn't count.)
>>>>> C++ does not have numeric for, so it doesn't count either.
>>>>>
>>>>> Francisco Olarte.
>>>>>
>>>> for (int i = 0; i < 100; i++) { printf("%d\n", i); }
>>>>
>>>> Looks like it does, it's just more flexible than Lua's.
>>> No 'for' is more flexible than Lua's.
>>>
>>> for a,b,c,as_many_as_you_like in myiter(anything) do
>>> end
>>>
>> Sorry, C++'s numeric for is more flexible than Lua, because it
gives you
>> more control over the iteration.
>>
>> You can also use rational objects which overload < and ++ in
C++ numeric
>> fors, while you can't in Lua even with __add and __lt.
> C/C++ does not have a numeric for, it only has a generic for.
>
> for (statement_list_1;statement_list_2;statement_list_3) do
statement;
>
>
> No restriction anywhere, although if statement_list_2 is just an
> assignment most compilers will recommend that you put it in
> parentheses.
>

for (type i = min; i < max; i += step) {
}

Why can't I do

for i=min,max,step do
end

in Lua? They should be equivalent.

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

--
--
Ryan | Charles <vandor2012@gmail.com <mailto:vandor2012@gmail.com>>
```