Chapter 2: Functions as First-Class Citizens
Chapter 2: Functions as First-Class Citizens
In functional programming, functions are treated as first-class citizens. This means that functions
can be assigned to variables,
passed as arguments to other functions, and returned as values from other functions. Python
supports this concept, enabling us
to write flexible and reusable code that can be easily composed and modified.
1. First-Class Functions in Python
Python functions are first-class objects, meaning they can be assigned to variables and
manipulated like any other object.
Here is an example:
def greet(name):
return f"Hello, {name}!"
say_hello = greet
print(say_hello("Alice")) # Output: Hello, Alice!
In this code, we assign the function `greet` to the variable `say_hello` and then call it,
demonstrating that functions
can be treated as values.
2. Higher-Order Functions
A higher-order function is a function that takes another function as a parameter or returns a
function as a result.
Higher-order functions are powerful because they enable you to create more abstract and modular
code.
Example of a higher-order function:
def apply_operation(x, y, operation):
return operation(x, y)
def add(a, b):
return a + b
def multiply(a, b):
return a * b
# Using apply_operation with different functions
print(apply_operation(5, 3, add)) # Output: 8
print(apply_operation(5, 3, multiply)) # Output: 15
In this example, `apply_operation` is a higher-order function that takes an operation function as a
parameter. By passing
different functions, we can change the behavior of `apply_operation`, making it more versatile.
3. Lambda Expressions
Lambda expressions, or anonymous functions, are a concise way to define simple functions
without naming them. In Python,
lambda expressions are often used with higher-order functions like `map`, `filter`, and `reduce`.
Example of a lambda function:
add = lambda x, y: x + y
print(add(2, 3)) # Output: 5
Using lambdas with `map`:
numbers = [1, 2, 3, 4, 5]
squared = list(map(lambda x: x ** 2, numbers))
print(squared) # Output: [1, 4, 9, 16, 25]
In this example, we use a lambda function with `map` to apply a square operation to each item in
a list.
4. Practical Examples: Mapping, Filtering, and Reducing
Python provides built-in functions `map`, `filter`, and `reduce` (from the functools module) that
support functional
programming. These functions allow for efficient processing of data without explicitly writing loops.
Example of `map` and `filter`:
numbers = [1, 2, 3, 4, 5, 6]
even_numbers = list(filter(lambda x: x % 2 == 0, numbers))
doubled_numbers = list(map(lambda x: x * 2, even_numbers))
print(even_numbers) # Output: [2, 4, 6]
print(doubled_numbers) # Output: [4, 8, 12]
The `filter` function selects elements based on a condition, while `map` applies a function to each
element.
5. The Power of Functional Abstractions
Treating functions as first-class citizens and using higher-order functions allow for highly modular
code that is both
readable and maintainable. In Python, this concept is particularly useful in situations where you
want to separate
data processing logic from specific implementations, making code easy to test and extend.