[PEP] matching and mismatching

Luke Kenneth Casson Leighton luke.leighton at aspextechnology.com
Fri May 3 06:23:04 EDT 2002


On Thu, May 02, 2002 at 11:10:07AM -0500, Mark McEahern wrote:

> [Luke Kenneth Casson Leighton]
> > the idea: a function/operator similar to map and reduce _or_ a keyword
> > similar to "in", such that:
> > 
> > for x in ([1,3,2,5,9] matching lambda x:x > 2):
> > 	print x
> > will output:
> > 3
> > 5
> > 9
> 
> What's wrong with using list comprehensions?  E.g.,
 
 great!  thanks!

> >>> nums = range(1, 11)
> >>> odds = [x for x in nums if x % 2]
> >>> evens = [x for x in nums if not (x % 2)]
> >>> nums
> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
> >>> odds
> [1, 3, 5, 7, 9]
> >>> evens
> [2, 4, 6, 8, 10]

interesting that you raise something that aspex's chip can optimise.

the x % 2 and the not (x % 2) operations, apart from being able
to perform the test in parallel, can do separate offloading of all
numbers that "match" or "mismatch" the test.

i.e. the match or mismatch is stored as a flag in every processing
element, such that, well, it's best if i try to express it in
pseudo-python:


(odds, evens) = [x for x in nums (if x % 2, if not x % 2)]

so, you performed the test on every element in the list.
you offload into "odds".

whilst the data is still loaded into the parallel array, you
perform a second test (which happens to be the opposite of
the first test, so you don't _actually_ need to issue a second
instruction - that's the optimisation), and do a second offload
(into "evens").

hm.  you could also even extend this idea further:

(odds, evens, triples) = [x for x in nums (if x % 2, if not x % 2, if x % 3)]

what do think?

worth a PEP?

l.

This email and any files transmitted with it, including replies and
forwarded copies subsequently transmitted from Aspex Technology
Limited, are confidential and solely for the use of the intended
recipient.  Any opinions expressed in this email are those of the
individual and not necessarily those of Aspex Technology Limited.  If
you are not the intended recipient, be advised that you have received
this email in error and that any use is strictly prohibited.  If you
have received this email in error, please notify us immediately by
e-mail and delete the original message without keeping any copies.

Aspex Technology Limited is registered in England and Wales No.3469577.
The registered office for Aspex Technology Limited is
York House, Cottingley Business Park, Bradford, BD16 1PF.





More information about the Python-list mailing list