Reverse Compose Functions | Challenge

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

Function composition is a technique used in functional programming to combine two or more functions to create a new function. In Python, we can use the compose function from the functools module to perform function composition. However, the compose function performs right-to-left function composition, which may not be suitable for all use cases. In this challenge, you will implement a function that performs left-to-right function composition.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/ErrorandExceptionHandlingGroup(["`Error and Exception Handling`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/FunctionsGroup -.-> python/keyword_arguments("`Keyword Arguments`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/FunctionsGroup -.-> python/lambda_functions("`Lambda Functions`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/using_packages("`Using Packages`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/ErrorandExceptionHandlingGroup -.-> python/custom_exceptions("`Custom Exceptions`") subgraph Lab Skills python/comments -.-> lab-13074{{"`Reverse Compose Functions | Challenge`"}} python/keyword_arguments -.-> lab-13074{{"`Reverse Compose Functions | Challenge`"}} python/tuples -.-> lab-13074{{"`Reverse Compose Functions | Challenge`"}} python/function_definition -.-> lab-13074{{"`Reverse Compose Functions | Challenge`"}} python/lambda_functions -.-> lab-13074{{"`Reverse Compose Functions | Challenge`"}} python/importing_modules -.-> lab-13074{{"`Reverse Compose Functions | Challenge`"}} python/using_packages -.-> lab-13074{{"`Reverse Compose Functions | Challenge`"}} python/standard_libraries -.-> lab-13074{{"`Reverse Compose Functions | Challenge`"}} python/custom_exceptions -.-> lab-13074{{"`Reverse Compose Functions | Challenge`"}} end

Reverse Compose Functions

Problem

Write a function compose_right that takes one or more functions as arguments and returns a new function that performs left-to-right function composition. The first (leftmost) function can accept one or more arguments; the remaining functions must be unary.

Your implementation should use the reduce function from the functools module to perform left-to-right function composition.

from functools import reduce

def compose_right(*fns):
  ## your code here

Example

add = lambda x, y: x + y
square = lambda x: x * x
add_and_square = compose_right(add, square)
assert add_and_square(1, 2) == 9

In the example above, we define two functions add and square. We then use the compose_right function to create a new function add_and_square that first adds two numbers and then squares the result. We then call the add_and_square function with the arguments 1 and 2, which returns 9.

Summary

In this challenge, you implemented a function compose_right that performs left-to-right function composition. You used the reduce function from the functools module to perform left-to-right function composition.

Other Python Tutorials you may like