Skip to content

Instantly share code, notes, and snippets.

@radix
Created December 20, 2014 18:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save radix/e0d137122fafbf4f65fa to your computer and use it in GitHub Desktop.
Save radix/e0d137122fafbf4f65fa to your computer and use it in GitHub Desktop.
Example of using an Effect to add scope to nested effects
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