lua-users home
lua-l archive

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


On 5 February 2011 17:58, Dirk Laurie <dpl@sun.ac.za> wrote:
> On Sat, Feb 05, 2011 at 01:33:17AM +0200, Andrew Lentvorski wrote:
>>
>> 3) The fact that I have to write a numeric "for" to iterate in this day
>> and age ticks me off. :)
>>
>
> Python goes out of its way to make the numeric "for" unattractive.
>
> Non-numeric:    z = [a*xi+yi for xi,yi in zip(x,y)]
> Numeric:        n = min(len(x),len(y))
>                z = n*[0]
>                for i in range(n) do z[i] = a*x[i]+b[i]
>
> Sure, that ticks me off too.
>
> But in Lua:
>
> Numeric:        z={}
>                for i=1,min(#x,#y) do z[i] = a*x[i]+b[i] end
> Non-numeric:    z={}
>                for k,xk in pairs(x) do
>                    local yk=y[k]
>                    if yk then z[k]=a*xk+yk end
>                    end
>
> Choose the tool for the job.  Full vectors, numeric; sparse vectors,
> non-numeric.
>
> Dirk
>
>
>

>                for k,xk in pairs(x) do
>                    local yk=y[k]
>                    if yk then z[k]=a*xk+yk end
>                    end
Can be expressed with:
for k , xk in pairs ( x ) do z [ k ] = a*xk + yk or 0 end

But I don't think thats what you wanted anyway :P

Daurnimator.