On Fri, Nov 05, 2021 at 03:53:56AM -0700, one last Day wrote:
Call two or more functions with one args.
[...]
This is like a version of map() except that instead of calling one function with a bunch of arguments, it calls many arguments with one set of arguments. It seems to be related to the "apply()" functional programming function too.
I haven't been able to find a standard name for this functional programming idiom, but it does seem to be a moderately common thing.
In Clojure:
https://stackoverflow.com/questions/67007139/how-do-i-run-multiple-functions...
Python:
https://stackoverflow.com/questions/35858735/apply-multiple-functions-with-m...
Apply multiple functions to the same argument in functional Python:
https://www.py4u.net/discuss/203479
I think the simplest eager implementation would be:
def apply(functions, *args, **kwargs): results = [None]*len(functions) for i, f in enumerate(functions): results[i] = f(*args, **kwargs) return results
If the functions have no side-effects, this could be done in parallel, using threads, async, or process. Otherwise, it can be turned into a list comprehension:
[f(*args, **kwargs) for f in functions]
If the arguments include an iterator, then the first function call will consume the iterator. For example:
# Works as expected >>> from math import prod >>> data = [1, 2, 3, 4] >>> [f(data, start=100) for f in (sum, prod)] [110, 2400]
# Surprise! >>> data = iter([1, 2, 3, 4]) >>> [f(data, start=100) for f in (sum, prod)] [110, 100]
So an iterator version would need to be written differently.