@@ -178,7 +178,7 @@ class EQDispatcher(object):
178178 e.g.::
179179
180180 >>> sync_perform(EQDispatcher({MyIntent(1, 2): 'the-result'}),
181- Effect(MyIntent(1, 2)))
181+ ... Effect(MyIntent(1, 2)))
182182 'the-result'
183183
184184 assuming MyIntent supports ``__eq__`` by value.
@@ -194,3 +194,35 @@ def __call__(self, intent):
194194 for k , v in self .mapping .items ():
195195 if k == intent :
196196 return sync_performer (lambda d , i : v )
197+
198+
199+ class EQFDispatcher (object ):
200+ """
201+ A dispatcher that looks up intents by equality and performs them by
202+ invoking a provided function.
203+
204+ Users provide a mapping of intents to functions, where the intents are
205+ matched against the intents being performed with a simple equality check
206+ (not a type check!).
207+
208+ e.g.::
209+
210+ >>> sync_perform(
211+ ... EQFDispatcher({
212+ ... MyIntent(1, 2): lambda i: 'the-result'}),
213+ ... Effect(MyIntent(1, 2)))
214+ 'the-result'
215+
216+ assuming MyIntent supports ``__eq__`` by value.
217+ """
218+ def __init__ (self , mapping ):
219+ """
220+ :param mapping: Mapping of intents to results.
221+ """
222+ self .mapping = mapping
223+
224+ def __call__ (self , intent ):
225+ # Avoid hashing, because a lot of intents aren't hashable.
226+ for k , v in self .mapping .items ():
227+ if k == intent :
228+ return sync_performer (lambda d , i : v (i ))
0 commit comments