lua-users home
lua-l archive

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


On 1 March 2016 at 16:59, Dirk Laurie <dirk.laurie@gmail.com> wrote:
> <boringalert> I'm returning to a topic that has been well chewed
> over on this list many times.
> </boringalert>
>
> Many if not all of us have small, almost trivial utilities that we include
> in most of our programs. Here is one of mine. It is given only to focus
> the discussion, not because I think it is in any way unusually good.
>
> local clone
> clone = function(tbl)
>   if type(tbl) ~= 'table' then return tbl end
>   local t={}
>   for k,v in pairs(tbl) do t[k]=clone(v) end
>   return t
> end
>
> Please do not point out particular defects of the code, such as that
> the original table may have had duplicate subtables but the clone
> does not. It is given _only to focus the discussion_, as an example
> of the size and nature of the little utilities that I am talking about.
>
> The questions that IMO bear revisiting every now and then are:
>
> 1. Is it a good thing for a newbie to write such utilities from scratch,
> in the process learning some subtleties of Lua, rather than grab
> them off a standardish package? (Niklaus Wirth omitted an
> exponentiation operator from Pascal for mainly this reason).

Yes. Many of these type of small functions have no wide-ly agreed upon
semantics.
e.g. for clone:
  - will it respect __pairs?
  - will it clone metatables?
  - will it clone keys?
  - what happens when cycles are encountered?

If there is more than one way to do something; it is the lua way to do
none of them.
As someone once said about python: "the standard library is where
functions go to die"
Not to mention that PHP added lots of util functions, and most new
developers consistently use the wrong ones (whether due to not knowing
limitations; not knowing the right keywords to google, or reading
outdated docs)

> 2. Should the Lua standard libraries supply any non-basic table
> routines?
> 2a. (This is a specific version of the previous point.) Why do you
> think did the Lua team supply none such beyond `pairs` and
> a library that operates on sequences?
>
> 3. Should we make any attempt to standardize the name and
> functionality of such routines, in the hope that one day a Lua
> standard library, widely accepted by the community, will emerge?
> 3a. Should libraries like stdlib, Penlight and org.conman make
> any effort to become compatible in terms of naming and
> functionality?

I don't think we need this standardisation around these routines at all.


If we *do* want to talk about standardisation, I'd like to take it in
the direction of common OS data structures being portable between
libraries (to pick on an example of mine from another thread:
sigset_t).