Implement Left-to-Right Function Composition

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/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`") subgraph Lab Skills python/comments -.-> lab-13606{{"`Implement Left-to-Right Function Composition`"}} python/keyword_arguments -.-> lab-13606{{"`Implement Left-to-Right Function Composition`"}} python/tuples -.-> lab-13606{{"`Implement Left-to-Right Function Composition`"}} python/function_definition -.-> lab-13606{{"`Implement Left-to-Right Function Composition`"}} python/lambda_functions -.-> lab-13606{{"`Implement Left-to-Right Function Composition`"}} python/importing_modules -.-> lab-13606{{"`Implement Left-to-Right Function Composition`"}} python/using_packages -.-> lab-13606{{"`Implement Left-to-Right Function Composition`"}} python/standard_libraries -.-> lab-13606{{"`Implement Left-to-Right Function Composition`"}} end

Reverse Compose Functions

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
from functools import reduce

def compose_right(*fns):
  return reduce(lambda f, g: lambda *args: g(f(*args)), fns)
add = lambda x, y: x + y
square = lambda x: x * x
add_and_square = compose_right(add, square)
add_and_square(1, 2) ## 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