Python - Lambda Functions
Currying is a functional programming concept where a function that takes multiple arguments is transformed into a sequence of functions, each taking a single argument. In the context of Python and lambda functions, currying allows you to create partially applied functions. Let's explore this concept with an example:
# Define a curried lambda function to add three numbers
curried_add = lambda x: lambda y: lambda z: x + y + z
# Partially apply the curried function to create a new function
add_partial = curried_add(1)(2)
# Call the partially applied function with the remaining argument
result = add_partial(3)
# Print the result
print("Result:", result)
The output of the program will be:
Result: 6
In this example, the curried_add
lambda function takes three arguments and returns a sequence of three lambda functions. Each lambda function takes a single argument and adds it to the previous sum. By partially applying the curried function, you create a new function (add_partial
) that expects the remaining arguments. When you call the partially applied function with the last argument, it calculates the sum of all three arguments.
Currying with lambda functions allows for a more modular and flexible approach to function application, enabling the creation of specialized functions with fewer arguments based on the context in which they are used.
In Python, lambda functions have access to variables in their enclosing scope, but the scope of these variables is limited. The variables are evaluated when the lambda function is created, and the function retains access to their values. Let's explore this concept with an example:
# Define a variable in the global scope
global_variable = 10
# Define a lambda function that uses the global variable
lambda_function = lambda x: x + global_variable
# Call the lambda function with an argument
result = lambda_function(5)
# Print the result
print("Result:", result)
The output of the program will be:
Result: 15
In this example, the lambda function lambda_function
takes an argument x
and adds it to the global variable global_variable
. The lambda function has access to the global variable because it is in the enclosing scope. When the lambda function is called with the argument 5, it adds 5 to the global variable (10) and produces a result of 15.
It's important to note that lambda functions do not create a new scope like functions defined with def
do. The scope of variables in lambda functions is limited to the variables in their enclosing scope at the time of creation.
In Python, lambda functions can be used to express conditional expressions using the syntax lambda arguments: true_value if condition else false_value
. This allows for concise representation of simple conditional logic within a lambda function. Let's explore this with an example:
# Define a lambda function with a conditional expression
conditional_lambda = lambda x: "Positive" if x > 0 else "Non-Positive"
# Test the lambda function with different values
result1 = conditional_lambda(5)
result2 = conditional_lambda(0)
result3 = conditional_lambda(-3)
# Print the results
print("Result 1:", result1)
print("Result 2:", result2)
print("Result 3:", result3)
The output of the program will be:
Result 1: Positive Result 2: Non-Positive Result 3: Non-Positive
In this example, the lambda function conditional_lambda
takes an argument x
and uses a conditional expression to determine whether x
is positive or non-positive. The lambda function is then tested with different values, producing results based on the condition specified in the conditional expression.
Using lambda functions for conditional expressions can be helpful when you need a concise and short-lived representation of logic, especially within higher-order functions or functional constructs.
The lambda x: x
idiom in Python is often used to create a simple identity function. The purpose of this idiom is to have a minimalistic function that returns its input unchanged. This can be useful in various scenarios, especially when a function is expected, and you want to provide a function that does nothing but pass the input through. Let's explore this with an example:
# Using the lambda x: x idiom to create an identity function
identity_function = lambda x: x
# Testing the identity function with different values
result1 = identity_function(5)
result2 = identity_function("Hello")
result3 = identity_function([1, 2, 3])
# Print the results
print("Result 1:", result1)
print("Result 2:", result2)
print("Result 3:", result3)
The output of the program will be:
Result 1: 5 Result 2: Hello Result 3: [1, 2, 3]
In this example, the identity_function
is a lambda function that takes an argument x
and simply returns it unchanged. The lambda lambda x: x
idiom is used to create this minimalistic identity function, allowing it to be applied to various types of inputs without modifying them.
This idiom is commonly used in functional programming and when passing functions as arguments to higher-order functions.