Created
December 20, 2014 18:41
-
-
Save radix/e0d137122fafbf4f65fa to your computer and use it in GitHub Desktop.
Example of using an Effect to add scope to nested effects
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
from __future__ import print_function | |
from characteristic import attributes | |
from effect import Effect, perform, ConstantIntent, base_dispatcher, sync_performer | |
from effect.dispatcher import TypeDispatcher, ComposedDispatcher | |
@attributes(['name']) | |
class MyThing(object): | |
"""Intent.""" | |
pass | |
@attributes(['scope', 'effect']) | |
class MyDecorator(object): | |
"""Intent which decorates another Effect.""" | |
pass | |
def effect_to_box(eff, box): | |
return eff.on(box.succeed, box.fail) | |
def perform_decorator(dispatcher, decorator, box): | |
print("performing a", decorator) | |
def scoped_performer(innerdisp, mything, innerbox): | |
print("performing", mything, "in scope", decorator.scope) | |
innerbox.succeed(decorator.scope + "." + mything.name) | |
new_disp = ComposedDispatcher([ | |
TypeDispatcher({MyThing: scoped_performer}), | |
dispatcher]) | |
perform(new_disp, effect_to_box(decorator.effect, box)) | |
td = TypeDispatcher({MyDecorator: perform_decorator}) | |
disp = ComposedDispatcher([td, base_dispatcher]) | |
eff = Effect( | |
MyDecorator( | |
scope='a', | |
effect=Effect( | |
MyThing(name='foo') | |
).on( | |
lambda r: Effect( | |
MyThing(name='bar') | |
).on( | |
lambda r2: (r, r2) | |
) | |
) | |
) | |
).on(print) | |
perform(disp, eff) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment