1

I want to make a lambda function of x such that takes the maximum between the results of all lambda functions in a list for that given x.

Any ideas on how to make this functional for any size of list?

a = lambda(x): someprocess0(x)
b = lambda(x): someprocess1(x)
c = lambda(x): someprocess2(x)

funcList = [a, b, c]

function = lambda(x): max(funcList[0](x), funcList[1](x), funcList[2](x))

2 Answers 2

3

You can use a generator expression to apply x to each function:

function = lambda x: max(f(x) for f in funcList)

This avoids keeping all function results in memory in a list, when you only need to track the highest value found so far.

You might be interested in the Python Functional Programming HOWTO included in the Python documentation, which teaches about generator expressions and other functional techniques.

Sign up to request clarification or add additional context in comments.

Comments

0

Using the following function:

def computeResults(functions, value):
    res = []
    for f in functions:
        res.append(f(value))
    return res

function = lambda x: max(computeResults(funcList, x))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.