functional programming with map()

Joshua Muskovitz joshm at taconic.net
Sun Feb 24 22:41:55 EST 2002


> I know that
> map(f,items)
> is equivalent to:
> for x in items:
>     f(x)

Actually, map does more than that.  map returns the results of f(x) as a new
list, of the same length as the original items.  If f() returns no value,
then you get a list of Nones.

>>> map(string.upper, ['dfbdfg','sdfgberg','dfbsadg','dfbdfag'])
['DFBDFG', 'SDFGBERG', 'DFBSADG', 'DFBDFAG']

> But what is the functional equvalent of:
>
> for x in items:
>     x.f()

List comprehensions make easy work of this:

>>> [ x.upper() for x in ['dfbdfg','sdfgberg','dfbsadg','dfbdfag'] ]
['DFBDFG', 'SDFGBERG', 'DFBSADG', 'DFBDFAG']

so in your example, "[ x.f() for x in items ]".  Cool, eh?
--
# Joshua Muskovitz
# joshm at taconic.net
def lyyrs(sig): return '-'.join(sig.split()+["ly y'rs"])
lyyrs('Hire me!  I need the work!')




-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----==  Over 80,000 Newsgroups - 16 Different Servers! =-----



More information about the Python-list mailing list