[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Omaha.pm] Re: [oma-python] mapping lambdas and other things




On Jun 13, 2004, at 1:22 PM, thehaas@binary.net wrote:
On Fri, Jun 11, 2004 at 08:39:46PM -0500, Jay Hannah wrote:

On Jun 10, 2004, at 6:30 AM, Mike Hostetler wrote:
Here is an example of real working code that decodes a list of Unicode
strings into latin-1:
   goodProcRef = map(lambda x: x.decode("latin-1"), goodProcRef)

How do you use that? (Can I get some context code?)

The map function applies a function to every item in a list, returning
of list of the result.  You can do the same thing with a for-loop, but
using the map function is much faster.

In Perl, it would be something like:

@decoded = [];
for $p in (@goodProcRef) {
   push(@decoded,$p->decode());
}
@goodProcRef=@decoded;

Ah. Thanks. Perl has a map function too, BTW (perldoc -f map). I didn't realize Python's map does the exact same thing. for loops also create references to each element in an array and place it in the default variable $_, so you can do things like

   for (@objects) { $_->decode("latin-1") }
or
   for (@strings) { s/foo/bar/ }

Ooo... I just discovered "pydoc map"...

j