Python syntax in Lisp and Scheme
Terry Reedy
tjreedy at udel.edu
Tue Oct 7 11:23:11 EDT 2003
"Pascal Costanza" <costanza at web.de> wrote in message
news:blu4q0$1568$1 at f1node01.rhrz.uni-bonn.de...
> What about dealing with an arbitrary number of filters?
[macro snipped]
What about it? Using macros for somewhat simple functions stikes me
as overkill.
> An example:
>
> > (predicate-collect '(-5 -4 -3 -2 -1 0 1 2 3 4 5)
> (function evenp)
> (lambda (n) (< n 0))
> (lambda (n) (> n 3)))
> (-4 -2 0 2 4)
> (-5 -3 -1)
> (5)
> (1 3)
In Python:
def multisplit(seq, *preds):
predn = len(preds)
bins = [[] for i in range(predn+1)]
predpends = [(p,b.append) for (p,b) in zip(preds,bins)]
rpend = bins[predn].append
for item in seq:
for pred,pend in predpends:
if pred(item):
pend(item)
break
else: rpend(item)
return bins
multisplit(range(-5,6), lambda i: not i%2, lambda i: i<0, lambda i:
i>3)
[[-4, -2, 0, 2, 4], [-5, -3, -1], [5], [1, 3]]
Terry J. Reedy
More information about the Python-list
mailing list