Fundamentals 6 min read

Higher-Order Functions and Function Composition in Functional Programming

Higher-order functions and function composition are core concepts in functional programming, allowing functions to be passed as parameters and returned as values, and enabling the construction of complex functionality by composing functions together.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Higher-Order Functions and Function Composition in Functional Programming

Higher-order functions and function composition are core concepts in functional programming, allowing functions to be passed as parameters and returned as values, and enabling the construction of complex functionality by composing functions together. This article provides a detailed explanation of higher-order functions and function composition concepts and usage, along with 10 practical code examples.

1. Definition of Higher-Order Functions: Higher-order functions are functions that accept one or more functions as parameters and/or return a function.

2. Example of Functions as Parameters:

def
apply_func
(func, x)
:
return
func(x)
def
square
(x)
:
return
x **
2
result = apply_func(square,
5
)
print(result)
# 输出: 25

3. Example of Functions as Return Values:

def
create_adder
(n)
:
def
adder
(x)
:
return
x + n
return
adder
add_5 = create_adder(
5
)
result = add_5(
3
)
print(result)
# 输出: 8

4. Definition of Function Composition: Function composition is the process of combining multiple functions in sequence to form a new function.

5. Example of Function Composition:

def
add
(x, y)
:
return
x + y
def
square
(x)
:
return
x **
2
composed_func =
lambda
x: square(add(x,
5
))
result = composed_func(
3
)
print(result)
# 输出: 64

6. Using map() for Function Mapping:

numbers = [1, 2, 3, 4, 5]
squared_numbers = list(map(lambda x: x ** 2, numbers))
print(squared_numbers)
# 输出: [1, 4, 9, 16, 25]

7. Using filter() for Conditional Filtering:

numbers = [1, 2, 3, 4, 5]
even_numbers = list(filter(lambda x: x % 2 == 0, numbers))
print(even_numbers)
# 输出: [2, 4]

8. Using reduce() for Cumulative Calculations:

from functools import reduce
numbers = [1, 2, 3, 4, 5]
sum_of_numbers = reduce(lambda x, y: x + y, numbers)
print(sum_of_numbers)
# 输出: 15

9. Using List Comprehensions for Transformation and Filtering:

numbers = [1, 2, 3, 4, 5]
squared_even_numbers = [x ** 2
for
x
in
numbers
if
x % 2 == 0]
print
(squared_even_numbers)
# 输出: [4, 16]

10. Using functools.partial() for Function Currying:

from
functools
import
partial
def
add
(x, y)
:
return
x + y
add_5 = partial(add,
5
)
result = add_5(
3
)
print(result)
# 输出: 8

These code examples demonstrate common application scenarios for higher-order functions and function composition, including functions as parameters, functions as return values, function composition, using map() for function mapping, using filter() for conditional filtering, using reduce() for cumulative calculations, using list comprehensions for transformation and filtering, and using functools.partial() for function currying. These techniques can help you better utilize higher-order functions and function composition to build functional programming style code.

Hope these examples are helpful to you! If you have any further questions, please feel free to ask.

pythonFunctional Programmingmapfunction compositionreduceFilterHigher-order FunctionsCurryinglambda functions
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.