How do you apply a list of lambda functions to a single element using an iterator?

I want to apply a list of lambda functions to a single element using an iterable that has to be created with yield.

The list of lambda functions would have something like:

[<function <lambda> at 0x1d310c8>, <function <lambda> at 0x1d355f0>]

And I want to apply every function, from left to right , to a single element using yield to construct an iterable to iterate the list

Answers


def apply_all(functions, item):
    for f in functions:
        yield f(item)

Example usage:

functions = [type, id, hex]
for result in apply_all(functions, 55):
    print result

gives

<type 'int'>
20326112
0x37

Need Your Help

what is the pred field in stl algorithms and how can i use it?

c++ algorithm stl predicate

Can somebody explain to me how is the pred field in stl algorithms exactly used?

Why does LIKE in SQLite3 work in this statement but = does not?

ruby sqlite3 md5

I use SQLite3 and have a table called blobs that stores content and *hash_value*.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.