haskell - Defining multiplication over functions in Python? -


i'm trying define multiplication on functions in python, in pseudocode is:

this should return function in x, given f(x), g(x)

multiply_two_functions((f(x), g(x)) = f(x) * g(x) 

i can in haskell so:

mult :: (num b) => (a -> b) -> (a -> b) -> (a -> b)   mult f g = h     h x = (f x) * (g x) 

you might ask why i'd want - have list of functions [f], , want reduce these multiplication. again, in haskell:

reduce_mult list = foldl mult 1 list 

edit: how i'm using in python, completeness:

def prod_reduce(f_list):     def identity(x):         return 1     def f_mult(f, g):         def multiplied(x):             return f(x) * g(x)         return multiplied     prod = identity     f in f_list:         prod = f_mult(prod, f)     return prod 

does have tips python implementation?

just write function returns new function returns product of results of other functions:

def multiply_funcs(f, g):     def multiplied(x):         return f(x) * g(x)     return multiplied 

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -