lua-users home
lua-l archive

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


2012/12/18 Peter Aronoff <telemachus@arpinum.org>:
>
> I'm curious about the end: "I find it more readable to write the equivalent
> in plain Lua." I've been using Lua more and more lately (instead of Ruby or
> Bash or previously Perl), and I already have my own general-purpose utility
> module that includes `map`, `filter`, `partition`, etc. I understand the
> idea that they are simple to write in plain Lua, but why would you ever
> want to write them *twice* in plain Lua? (Just curious, not trolling.)
>

More readable. That is the key phrase.

What I mean is that I can at first glance, with no need to refer to
documentation, see what

s={}; for k=1,#a do s[k]=-a[k] end

does. No helper function is needed. Whereas

s = map(a, function(x) return -x end)

forces me to make sure that map operates on the ipairs,
not the pairs.  Oops, should have been imap.  (Not to
mention waking up the agonists for lambda notation.)
Not much to choose, though.

Next time the task is a little different, I write equally legibly

s={}; for k=2,#a-1 do s[k-1]=-a[k] end

but the map user has to resort to all sorts of fancy
footwork, maybe creating temporary arrays, or slice
helpers, or involving optional arguments to map, etc.