lua-users home
lua-l archive

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


2011/11/13 David Manura <dm.lua@math2.org>:
>
> Here `traverse` is your typical call-back based recursive tree
> traversal (without any coroutines), and `traverse2` transforms
> it into an iterator by inverting the control with a single coroutine.
>
Beautiful!  I'll make sure I understand it, and change my code
accordingly, in that order.

> I also question how often the behavior "t1 is cloned into
> t2,t3,...,tn" would be used.
>
I can get away, in the intended application, with cloning the
structure into only one of them.  Problem is, I don't know in advance
which.

For example (changing the name 'traverse' of my original proposal to
'leaves'), here is matrix addition:

   a={{10,10,10},{20,20,20}}
   b={{1,2,3},{1,2,3}}
   c={}
   for k,sa,sb,sc in leaves(a,b,c) do sc[k]=sa[k]+sb[k] end

However, in that case the others must have the structure already.  The
way I do it, nothing happens then: only for c is the structure cloned.

The alternative is to separate the cloning of the structure from
visiting the leaves.  I.e. "leaves" will do no cloning, and instead
"c={}" is replaced by "c=clone(a)".  Probably neater.

Dirk