Skip to content

Instantly share code, notes, and snippets.

@jaantollander
Last active March 13, 2017 09:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaantollander/a6b98c7e2c0cafb67a604d1009aadcd8 to your computer and use it in GitHub Desktop.
Save jaantollander/a6b98c7e2c0cafb67a604d1009aadcd8 to your computer and use it in GitHub Desktop.
Python script for composing functions
# 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