Last active
March 13, 2017 09:22
-
-
Save jaantollander/a6b98c7e2c0cafb67a604d1009aadcd8 to your computer and use it in GitHub Desktop.
Python script for composing functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Adapted from a comment from: | |
# https://mathieularose.com/function-composition-in-python/ | |
import functools | |
def compose(*funcs): | |
"""Compose functions f, g, h, ... into ...(f(g(h(x)))) | |
Args: | |
*funcs: Functions to be composed. Fuctions must take only one argument. | |
""" | |
return lambda x: functools.reduce(lambda v, f: f(v), reversed(funcs), x) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment