Created
March 25, 2023 09:50
-
-
Save FuruNov/32d1bd1a940cbee61bb3316a3d5762ec to your computer and use it in GitHub Desktop.
引数に取った関数の合成関数を返す関数
This file contains hidden or 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
| 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