# Path: cell/workflow/monads.py
# def callcc(usecc):
#     return ContinuationMonad(
#         lambda cont: usecc(
#             lambda val: ContinuationMonad(
#                 lambda _: cont(val))
#         ).run(cont)
#     )
# 
# def done(val):
#     raise Done(val)
# 
# class ContinuationMonad(Monad):
# 
#     def __init__(self, run):
#         self.run = run
# 
#     def __call__(self, cont=fid):
#         return self.run(cont)
# 
#     def bind(self, bindee):
#         return ContinuationMonad(
#             lambda cont: self.run(
#                 lambda val: bindee(val).run(cont))
#         )
# 
#     @classmethod
#     def unit(cls, val):
#         return cls(lambda cont: cont(val))
# 
#     @classmethod
#     def zero(cls):
#         return cls(lambda cont: None)
# 
# @decorator_with_args
# def do(fun, args, kwargs, Monad):
# 
#     @handle_monadic_throws(Monad)
#     def run_maybe_iterator():
#         it = fun(*args, **kwargs)
# 
#         if isinstance(it, types.GeneratorType):
# 
#             @handle_monadic_throws(Monad)
#             def send(val):
#                 try:
#                     # here's the real magic
#                     # --what magic? please explain or the comment goes :) [ask]
#                     monad = it.send(val)
#                     return monad.bind(send)
#                 except StopIteration:
#                     return Monad.unit(None)
#             return send(None)
#         else:
#             # not a generator
#             return Monad.unit(None) if it is None else it
# 
#     return run_maybe_iterator()


from collections import deque
from .monads import callcc, done, ContinuationMonad, do



class Mailbox:

    def __init__(self, name=None):
        self.name = name
        self.messages = deque()
        self.handlers = deque()

    def send(self, message):
        try:
            handler = self.handlers.popleft()
        except IndexError:
            self.messages.append(message)
        else:
            handler(message)()

    def receive(self):

# Path: cell/workflow/monads.py
# def callcc(usecc):
#     return ContinuationMonad(
#         lambda cont: usecc(
#             lambda val: ContinuationMonad(
#                 lambda _: cont(val))
#         ).run(cont)
#     )
# 
# def done(val):
#     raise Done(val)
# 
# class ContinuationMonad(Monad):
# 
#     def __init__(self, run):
#         self.run = run
# 
#     def __call__(self, cont=fid):
#         return self.run(cont)
# 
#     def bind(self, bindee):
#         return ContinuationMonad(
#             lambda cont: self.run(
#                 lambda val: bindee(val).run(cont))
#         )
# 
#     @classmethod
#     def unit(cls, val):
#         return cls(lambda cont: cont(val))
# 
#     @classmethod
#     def zero(cls):
#         return cls(lambda cont: None)
# 
# @decorator_with_args
# def do(fun, args, kwargs, Monad):
# 
#     @handle_monadic_throws(Monad)
#     def run_maybe_iterator():
#         it = fun(*args, **kwargs)
# 
#         if isinstance(it, types.GeneratorType):
# 
#             @handle_monadic_throws(Monad)
#             def send(val):
#                 try:
#                     # here's the real magic
#                     # --what magic? please explain or the comment goes :) [ask]
#                     monad = it.send(val)
#                     return monad.bind(send)
#                 except StopIteration:
#                     return Monad.unit(None)
#             return send(None)
#         else:
#             # not a generator
#             return Monad.unit(None) if it is None else it
# 
#     return run_maybe_iterator()


# Path: cell/workflow/common.py
from collections import deque
from .monads import callcc, done, ContinuationMonad, do




class Mailbox:

    def __init__(self, name=None):
        self.name = name
        self.messages = deque()
        self.handlers = deque()

    def send(self, message):
        try:
            handler = self.handlers.popleft()
        except IndexError:
            self.messages.append(message)
        else:
            handler(message)()

    def receive(self):

        return callcc(self.react)