Created
January 17, 2022 02:43
-
-
Save ktgw0316/fb5431f172ef011e132a1e07444078b3 to your computer and use it in GitHub Desktop.
関数型はプログラミングスタイル Python版
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
| #!/usr/bin/env python | |
| # cf. https://zenn.dev/j5ik2o/articles/97b458eff8283c783d9b#go | |
| # val/letのような機能はないが、変数の値を上書きしないルールで | |
| # カバーすることになるかと。Javaと似た運用になりそう。 | |
| class Main: | |
| def main(self): | |
| self.run() | |
| # (1) 命令型スタイル | |
| def print_args1(self, args): | |
| i = 0 | |
| while i < len(args): | |
| print(args[i]) | |
| i += 1 | |
| # (2) 変数を削除したスタイル。まだ副作用はある | |
| def print_args2(self, args): | |
| for arg in args: | |
| print(arg) | |
| # (4) 変数も副作用もない純粋関数型スタイル | |
| def formatted_args(self, args): | |
| return "\n".join(args) | |
| def run(self): | |
| args = ["Hello", "World"] | |
| self.print_args1(args) | |
| self.print_args2(args) | |
| print(self.formatted_args(args)) | |
| if __name__ == "__main__": | |
| Main().run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment