lua-users home
lua-l archive

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


2015-07-17 16:02 GMT+02:00 Peter Aronoff <telemachus@arpinum.org>:
> Dirk Laurie <dirk.laurie@gmail.com> 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.