python - Combine functions to later use in map -


i have list of items, coming 1 object of type a, , library operates on object of type b.

i convert b, , later call b's function in pythonic way. have come far:

def convert_a_to_b(a):     return b(a.something, a.something_else)  def dostuff(a_list):     converted_to_b = list(map(convert_a_to_b, a_list)     return list(map(b.function, converted_to_b)) 

i create lambda combine these functions, feel there should easier way. like:

return list(map(combine(convert_a_to_b, b.function))) 

from functools import partial, reduce combine = lambda *xs: partial (reduce, lambda v,x: x(v), xs) 

the function usable such combine (a.foo, a.bar) (x) equivalent a.bar(a.foo (x)).

combine accept variadic number of functions, , return new function accepts single value. value passed through every mentioned function (in chain) until final result yield.


sample usage

map (combine (convert_a_to_b, b.function), a_list) 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -