Grieve (grieve) wrote,
Grieve
grieve

Continuing Education

Yesterday I was reading about text processing in python, excitement for all. When I ran across a discussion of higher order functions, one in particular was the following.
apply_each = lambda fns, args=[]: map(apply, fns, [args]*len(fns))

What this did was not obvious to me, since I didn't know about map, apply, and multiplying a list by a cardinal. After some study, and playing around in the python interpreter I learned It has the following affect. Invoke each function in fns and use the arguments in the list args, and return a list with the result of each function. It turns out that 'apply' has been deprecated in python. In favor of the function(*args, **kw) syntax. What this essentially means is that apply_each should now be written as follows:
def apply_each(fns, *args, **kw):
    return [f(*args, **kw) for f in fns]


Thanks to veloso for helping me with the *,** syntax.
Tags: programming, python
Subscribe
  • Post a new comment

    Error

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.
  • 0 comments