Any, All predicates (and vector operations for free)

Steven Taschuk staschuk at telusplanet.net
Mon Feb 17 20:24:54 EST 2003


Quoth Christos TZOTZIOY Georgiou:
> I have created a class that allows ANY and ALL predicates on iterables;
> it was easy to also allow vector operations (eg add something to every
> item on the list).

Interesting.  If I understand correctly, these let one write
	  (exists x in S)(x > 2)    as    Any(S) > 2
	and  (all x in S)(x > 2)    as    All(S) > 2
This is indeed a neat-o syntax, as Martelli says.

But I'm not sure I understand how these predicates can be
composed.  Suppose I have a list of lists of integers, and wish to
verify that each list of integers contains something greater than
2.  That is, I wish to check that
	(all x in L)(exists y in x)(y > 2)
So, I write
	Any(All(L)) > 2
Right?

I was not able to get this to work with the code as written
(though admittedly I did not spend much time on it), and am not
sure it could ever work sensibly.  For example, what should
All(L).__iter__ return?  An iterator over the elements of L?  An
iterator of L's elements' iterators?  A "flattened" iterator over
those elements' elements?

I think I'd prefer a scheme under which I'd write
	all(L, lambda x: exists(x, lambda y: y > 2))
which is more verbose, but (to me at least) easier to parse.  Also
the implementation is trivial, short-circuiting is easy, and no
difficult semantic questions arise.  (It's really just a
generalization of map, etc., to generic iterables.)

-- 
Steven Taschuk                          staschuk at telusplanet.net
"Its force is immeasurable.  Even Computer cannot determine it."
                            -- _Space: 1999_ episode "Black Sun"





More information about the Python-list mailing list