Re:Widely useful Python utilities by Peter Norvig
by kragen on Monday September 14, 2009 @06:12PM
(#822718)
You want to be able to say all(items, lambda x: x.is_cool()) instead of all(item.is_cool() for item in items)? The second one seems clearer to me and isn't substantially longer.
[ Parent
| on HN ]
Re:Widely useful Python utilities by Peter Norvig
by mccutchen on Monday September 14, 2009 @06:45PM
(#822788)
Good point. I guess usually crops up when I've already got a predicate function handy and I want to say `all(items, f)`. I always just expect that to work for some reason. Using a generator expression works just as well, though, and is probably clearer in the long run.
Besides, I guess if `any` had a signature like
def any(xs, pred=lambda x: bool(x))
or something, that would reverse the signatures of map, filter, reduce, etc.On that note, I always want the arguments of filter and reduce, but not those of map, to be reversed for some reason.
Re:Widely useful Python utilities by Peter Norvig
by euccastro on Monday September 14, 2009 @09:14PM
(#823014)
If you already have a predicate function, it is not hard to say
all(filter(f, items))
Composing calls like this looks cleaner than incorporating into the function interface all list manipulations that the implementor expects you to want.
Re:Widely useful Python utilities by Peter Norvig
by mattj on Tuesday September 15, 2009 @02:46AM
(#823415)
Said before, but any(pred(x) for x in xs) is too complex?