lua-users home
lua-l archive

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


On Wed, Jan 8, 2014 at 5:52 PM, Javier Guerra Giraldez
<javier@guerrag.com> wrote:
> On Wed, Jan 8, 2014 at 8:16 AM, Alexander Gladysh <agladysh@gmail.com> wrote:
>> I'd like to write this declaratively. Maybe like this:
>>
>> local result = T:map (data)
>> {
>>   sprite = "/img/" .. T(1);
>>   x = T(2), y = T(3);
>> }
>
> what's wrong with the classic map(t,f) transformation?
>
> function map(t,f)
>     local o={}
>     for i = 1,#t do
>         o[i] = f(t[i])
>     end
>     return o
> end
>
> local result = map(data, function(e) return {
>     sprite = '/img/'..e[1],
>     x = e[2], y = e[3]
> })

Well, my example is too simple, probably :-) I do not care about speed
at all in the most of the relevant applications — they all are
middleware tools, doing code (or rather data) generation.

I need to do much more complex transformations. Imperative (or
functional) code simply does not cut it — it is not readable.

The point here is not the "stunt" with that fancy T syntax. The point
is to make code declarative to enhance readability.

Compare a {{mustache}} template with the same text, created via
concatenations (or, ok, using ropes, doesn't matter) in imperative (or
even functional) Lua code.

Here is a more complex example:

https://gist.github.com/agladysh/6f99595fc289fc28e251

This is a part of a larger data transformation thing, maybe of 500 lines.

Yes, the data format design is rather awful here — both for incoming
data and for output. But one of the reasons of why it is awful is that
it is too obscure and unreadable to start with. It is hard to keep the
whole picture in the head when one has to organically grow the thing
due to changing requirements while the time is pressing.

Alexander.