lua-users home
lua-l archive

Re: Numeric for loop with rationals

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


It was thus said that the Great Dirk Laurie once stated:
> 2018年01月30日 14:23 GMT+02:00 Soni "They/Them" L. <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>
> >> wrote:
> >> ...
> >>>
> >>> Also, rationals are still numbers. They're just not "Lua numbers"
> >>> (objects
> >>> with type(x) == "number"). Any language with operator overloading (e.g.
> >>> 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
 Oh, you mean something like:
	function range(low,high,inc)
	 inc = inc or 1
	
	 local function bump() 
	 local r = low
	 low = low + inc
	 if r <= high then
	 return r
	 end
	 end
	
	 return bump 
	end
which respects __add and __le so something like
	for x in range(r(1,10),r(10,10),r(1,10)) do
	 print(x)
	end
will work? Neat!
 -spc

AltStyle によって変換されたページ (->オリジナル) /