<s> from __future__ import absolute_import <EOL> from collections import deque <EOL> from . monads import callcc , done , ContinuationMonad , do <EOL> class Mailbox ( object ) : <EOL> def __init__ ( self , name = None ) : <EOL> self . name = name <EOL> self . messages = deque ( ) <EOL> self . handlers = deque ( ) <EOL> def send ( self , message ) : <EOL> try : <EOL> handler = self . handlers . popleft ( ) <EOL> except IndexError : <EOL> self . messages . append ( message ) <EOL> else : <EOL> handler ( message ) ( ) <EOL> def receive ( self ) : <EOL> return callcc ( self . react ) <EOL> @ do ( ContinuationMonad ) <EOL> def react ( self , handler ) : <EOL> try : <EOL> message = self . messages . popleft ( ) <EOL> except IndexError : <EOL> self . handlers . append ( handler ) <EOL> done ( ContinuationMonad . zero ( ) ) <EOL> else : <EOL> yield handler ( message ) </s>
