@@ -58,17 +58,44 @@ To perform the ReadLine intent, we must implement a performer function:
5858 def perform_read_line (dispatcher , readline ):
5959 return raw_input (readline.prompt)
6060
61- ``Effect `` objects can be passed to :func: `effect.perform `, along with a
62- dispatcher which looks up the performer based on the intent.
61+
62+ To do something with the result of the effect, we must attach callbacks with
63+ the ``on `` method:
6364
6465.. code :: python
6566
66- def main ():
67- eff = get_user_name()
68- eff = effect.on(
69- success = lambda result : print (" I like {} too!" .format(result)),
70- error = lambda e : print (" sorry, there was an error. {} " .format(e)))
67+ def greet ():
68+ return get_user_name().on(
69+ success = lambda r : Effect(Print(" Hello," , r)),
70+ error = lambda exc_info : Effect(Print(" There was an error!" , exc_info[1 ])))
71+
72+
73+ (Here we assume another intent, ``Print ``, which shows some text to the user.)
74+
75+ A (sometimes) nicer syntax is provided for adding callbacks, called ``do ``
76+ notation.
77+
78+ .. code :: python
7179
80+ from effect.do import do
81+
82+ @do
83+ def greet ():
84+ try :
85+ name = yield get_user_name()
86+ except Exception as e:
87+ yield Effect(Print(" There was an error!" , e))
88+ else :
89+ yield Effect(Print(" Hello," , name))
90+
91+ Finally, to actually perform these effects, they can be passed to
92+ :func: `effect.perform `, along with a dispatcher which looks up the performer
93+ based on the intent.
94+
95+ .. code :: python
96+
97+ def main ():
98+ eff = greet()
7299 dispatcher = TypeDispatcher({ReadLine: perform_read_line})
73100 perform(dispatcher, effect)
74101
0 commit comments