lua-users home
lua-l archive

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


On 14/12/2012 07:19, Dirk Laurie wrote:
merge(...): returns a new table containing all the pairs from all the
    tables given as arguments. In particular, if there is only one argument,
    returns a shallow copy. In case of duplicate keys, later values
    replace earlier ones. Nil arguments are treated as empty tables.

The documentation is much longer than the code!

That's python dict.update ;-)

There is a general sheme behind, which is at times called 'combine'. For any kind of data structure, combine creates a new one (or modifies the first one in row) as if each item (whatever items are, can be pairs) in every input structure was _put_ into the combination, one after the other. For a sequence, this amounts to catenation, for a set to union, for a mapping to what you describe (a variant of union),... Object-like tables behave in combination like mappings. I find this idea quite nice, and with this simple, while (meaningfully & coherently) differenciated semantics, there can be a single operator for it.

The complement of combination is composition:
	(1 2 3) (4 5 6) -- combine -> (1 2 3 4 5 6)
	(1 2 3) (4 5 6) -- compose -> ((1 2 3) (4 5 6))

Denis