[Tutor] check for nested sequences & flatten [functional programming/lambda]

Remco Gerlich scarblac@pino.selwerd.nl
Thu, 14 Feb 2002 09:42:18 +0100


On  0, Danny Yoo <dyoo@hkn.eecs.berkeley.edu> wrote:
> We can pretend to be pathologically addicted to functional programming,

(snip)

> Here's a function that checks to see if a certain condition is true
> anywhere in the list:
> 
> ###
> def any(condition_function, seq):
>     """Returns 1 if any element in our sequence 'seq'
>     responds favorably to our 'condition_function'."""
>     for element in seq:
>         if condition_function(element):
>             return 1
>     return 0
> ###

If we're addicted, surely then this should read

any = lambda condition, seq: reduce(lambda a,b: a or b, map(condition, seq))

:)

(and why isn't the 'or' operator in the operator module?)

(If you don't understand what that line means - that's great! 
Avoid that style!)

-- 
Remco Gerlich