Skip to content

Instantly share code, notes, and snippets.

@FuruNov
Created March 25, 2023 09:50
Show Gist options
  • Select an option

  • Save FuruNov/32d1bd1a940cbee61bb3316a3d5762ec to your computer and use it in GitHub Desktop.

Select an option

Save FuruNov/32d1bd1a940cbee61bb3316a3d5762ec to your computer and use it in GitHub Desktop.
引数に取った関数の合成関数を返す関数
def compose(*functions):
def inner(arg):
for function in reversed(functions):
arg = function(arg)
return arg
return inner
def add2(x):
return x + 2
def multiply3(x):
return x * 3
def square(x):
return x ** 2
composed_function = compose(square, multiply3, add2)
print(composed_function(5)) # 出力: 169
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment