lua-users home
lua-l archive

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

On Sat, May 2, 2009 at 7:37 AM, Mark Hamburg <> wrote:
> On a potentially related note, I had previously been thinking about ways to
> do chaining/pipelining for iterator construction. As I recall, that had lead
> to constructs like:
>        pipe( pairs, map( function( k, v ) return tostring( k ), tostring( v
> ) end ), filter( function( k, v ) return #k == 3 end ) ) )( t )
> Those had never felt as clean as one might like, however, though they did
> encourage saving the pipe for reuse. Perhaps there's something to be
> explored in your chain solution.

As an example of convergent thinking, I've been looking at this very
issue. This was inspired by a recent comment of David, and attempts to
make pl.seq more useful and intuitive:

The basic insight is that we are culturally conditioned to prefer
chains of operations to move from left to right, which is perhaps one
of the hidden strengths of OOP notation.

For an example, this gets all the unique lines in the file and prints them out:

S(io.lines(fname)):unique():printall '\n'

And this puts all the unique identifiers in a Lua code string into a
table result:

ls = S(lexer.lua(str)):filter(Eq(_1,'iden')):map(_2):unique():copy()

Any methods which are not recognized are looked up using the first
value of the sequence:

S{'[one]','[two]','[three]'}:sub(2,-2):upper():printall() ---> ONE TWO THREE

steve d.