You can write the following function:
def conjoin(*fns):
def conjoined(x):
for fn in fns:
if not fn(x): return False
return True
return conjoined
Then you call it like this:
ifilter(conjoined(lambda x: x not in block_list, lambda x: x in accept_list),
read_records(filename))
And you can implement a similar function disjoinfor simultaneous functions:
def disjoin(*fns):
def disjoined(x):
for fn in fns:
if fn(x): return True
return False
return disjoined
It might be better to implement them, but you need to be careful. You can try to apply each function to xand use allor any, but this is undesirable, since to use them you will need to evaluate each predicate in the argument. The solution presented here is a satisfactory short circuit.
In addition, just for fun, we implement the function invert
def invert(fn):
return lambda x: not fn(x)
:)