[Zope-dev] Functional programming

Tres Seaver tseaver@palladion.com
Thu, 24 Feb 2000 11:04:19 -0600


Lalo Martins <lalo@hackandroll.org> asks:
> To: zope-dev@zope.org
> Subject: 
> 
> I know it probably is on the archives, but I didn't find it.
> 
> Is there a good reason why I can't use map, filter and lambda
> in DTML code? (They're not in the namespace.)

Duncan Booth <duncan@rcp.co.uk> replies:
> 
> lambda is available, but you have to specify an extra default
> argument _vars=_vars
> > Is there a good reason why I can't use map, filter and lambda
> > in DTML code? (They're not in the namespace.)
> >
> Even without map and filter this can be useful on occasion if you
> find yourself otherwise repeating an expression a lot of times. On
> the other hand it isn't very pleasant to look at.
> 
> I think the answer is probably that if you find you need to use map
> and filter then you should consider using Python instead. Besides
> you can get the effect of both of these with dtml-in.

Lalo volleys:
> >
> > I think the answer is probably that if you find you need to use map
> > and filter then you should consider using Python instead.
> 
> Not really. Practical example:
> 
> <dtml-in "map (Catalog.get_object, Catalog(...))">
>    perform some operation, like listing the objects, without
>    need to bload the Catalog with all properties in the objects
> </dtml-in>

The downside here is that you activate every object found, which can be deadly
for a really large result set.  The rationale for using meta_data is to allow
presentation of a limited view of the catalogued objects without pulling them
into memory from the ZODB.

> 
> And even if I _should_ use Python, then it should at least be
> supported in PythonMethods.

PythonMethods have no more access than DTML Methods/Documents (by design).

> 
> > Besides you can get the effect of both of these with dtml-in.
> 
> But the code you need for that looks mightly ugly, with
> temporary variables, lots of REQUEST.set... let's not go there
> :-)

Agreed.  filter() is particularly nice, as it eliminates the nesting of the
<dtml-if> inside the <dtml-in>.  For instance, to grab all contained objects
which have a "browseable" property, set to true:

  <dtml-in "_.filter( lambda x: x.hasattr( 'browseable' )
                            and x.browseabe, objectValues() )">
   ...
  </dtml-in>

I will admit that I can't think of a particular use for map() offhand (but then,
I'm not a lambda-head, either).
-- 
=========================================================
Tres Seaver         tseaver@palladion.com    713-523-6582
Palladion Software  http://www.palladion.com