10章単子――モナドThe One
![](/api/v2/epubs/9784814400225/files/images/figure10.png)
制約
- 値から変換できる抽象化の存在。
- この抽象化は次の操作を提供する。(1)値をラップして抽象化する、(2)関数の並びを作るために関数を結合(bind)する、(3)ラップを解いて最終的な値を調べる。
- 大きな問題は、関数を束ねたパイプラインで解決し、最後にラップを解く。
- 単子スタイルでは、結合操作は単に与えられた関数を呼び出すだけである。その際、抽象化が保持する値を与え、その戻り値を保持する。
プログラム
1 #!/usr/bin/env python 2 import sys, re, operator, string 3 4 # 5 # モナド(the one)クラス 6 # 7 class TFTheOne: 8 def __init__(self, v): 9 self._value = v 10 11 def bind(self, func): 12 self._value = func(self._value) 13 return self 14 15 def printme(self): 16 print(self._value) 17 18 # 19 # 関数 20 # 21 def read_file(path_to_file): 22 with open(path_to_file) as f: 23 data = f.read() 24 return data 25 26 def filter_chars(str_data): ...
Get プログラミング文体練習 ―Pythonで学ぶ40のプログラミングスタイル now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.