[Tutor] map() practice

Alan Gauld alan.gauld at btinternet.com
Sun Oct 21 09:53:49 CEST 2012


On 21/10/12 02:09, Malcolm Newsome wrote:
> Hello all,
>
> I looked at map() tonight.  I think I have a decent understanding now of
> "how" it works.  However, I'm wondering when it is most commonly used in
> the real world and if you could provide some examples (I like to do a
> lot of web stuff...it that helps with regards to providing a context for
> real world application).

Do you use JQuery in your web programming?
If so you should be familiar with the kind of things map() can do.
You use it to make a change to a collection of objects.
So you can write a function to select all the objects that need changing 
and then another function to change those objects.
Then call map on the two functions:

def getcollection():...
def change(item):...

map(change, getCollection())

So in a web context you might want to change the header level of every 
header following a div tag.

In network programming I might want to change the destination address of 
every packet coming into the server for a particular message.

map() is not used as much these days since you can often do the same 
thing more easily using list comprehensions or generator expressions, 
but before those were introduced map() was a useful convenience function.


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list