function-composition

Apply a list of Python functions in order elegantly

Apply a list of Python functions in order elegantly Question: I have an input value val and a list of functions to be applied in the order: funcs = [f1, f2, f3, …, fn] How to apply elegantly and not writing fn( … (f3(f2(f1(val))) … ) and also not using for loop: tmp = val …

Total answers: 2

How to multiply functions in python?

How to multiply functions in python? Question: def sub3(n): return n – 3 def square(n): return n * n It’s easy to compose functions in Python: >>> my_list [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> [square(sub3(n)) for n in my_list] [9, 4, 1, 0, 1, 4, 9, 16, 25, 36] Unfortunately, …

Total answers: 4

Composing functions in python

Composing functions in python Question: I have an array of functions and I’m trying to produce one function which consists of the composition of the elements in my array. My approach is: def compose(list): if len(list) == 1: return lambda x:list[0](x) list.reverse() final=lambda x:x for f in list: final=lambda x:f(final(x)) return final This method doesn’t …

Total answers: 16

python "multiple" combine/chain list comprehension

python "multiple" combine/chain list comprehension Question: I am quite new to python and I have been learning list comprehension alongside python lists and dictionaries. So, I would like to do something like: [my_functiona(x) for x in a] ..which works completely fine. However, now I’d want to do the following: [my_functiona(x) for x in a] && …

Total answers: 3

Pointfree function combination in Python

Pointfree function combination in Python Question: I have some predicates, e.g.: is_divisible_by_13 = lambda i: i % 13 == 0 is_palindrome = lambda x: str(x) == str(x)[::-1] and want to logically combine them as in: filter(lambda x: is_divisible_by_13(x) and is_palindrome(x), range(1000,10000)) The question is now: Can such combination be written in a pointfree style, such …

Total answers: 6