[stringtemplate-interest] List comprehensions Was Re: Working with filtered list
Terence Parr
parrt at cs.usfca.edu
Thu Jun 28 16:02:22 PDT 2007
On Jun 26, 2007, at 7:11 PM, John Snyders wrote:
> Here is what it might look like
> $[x for x in data if x.IsSmart]:{ $it.CustomerName$ }$
>
> No comparison expressions have been introduced. The program is still
> responsible for implementing the logic by setting the IsSmart
> property.
> It is just a combination of list iteration, if evaluation (the same if
> already supported), and list construction.
That is the same as
$data:{ d | $if(d.isSmart)$$d.CustomerName$$endif$}$
right? The list comprehension syntax is nicer though.
> List construction already allows a comma separated list of
> expressions.
> So something like this is possible:
> $[items, decorate("foo"), "bar"]: { x | ($x$)};separator="; "$
>
> If it allowed templatesExpresson (this includes the syntax for
> iterating) then something like this would be allowed:
> $[items:decorate(it), decorate("foo"), "bar"]: { x | ($x
> $)};separator="; "$
Ooops...that doesn't work? It should handle any expression.
> All that would be needed to have the power of Python list
> comprehensions
> is fitting in the "if" expression. Perhaps:
> $items if it.isFancy :decorate(it)$
> or
> $items : { x | x.isFancy | Fancy: $x$. }$
Well, we can already do this now, just less beautifully as we need
the if/end.
> On the other hand I wonder if the Python list comprehension syntax is
> more power full.
Technically, probably not. I would have to think harder.
Ter
More information about the stringtemplate-interest
mailing list