Dispatchers

symbolic_dispatch([f, cls])

Return a generic dispatch function with symbolic data implementations.

verb_dispatch(cls[, f])

Wrap singledispatch.

Pipeable([f, calls])

Enable function composition through the right bitshift (>>) operator.

siuba.siu.dispatchers.symbolic_dispatch

siuba.siu.dispatchers.symbolic_dispatch(f=None, cls=<class 'object'>)

Return a generic dispatch function with symbolic data implementations.

The function dispatches (Call or Symbolic) -> FuncArg.

Parameters
  • cls – A class to dispatch on.

  • f – A function to call if no classes match while dispatching.

Examples

Here is an example of running separate add functions on integers and strings.

>>> @symbolic_dispatch(cls = int)
... def add1(x): return x + 1
>>> @add1.register(str)
... def _add1_str(x): return int(x) + 1
>>> add1(1)
2
>>> add1("1")
2

Note that passing a symbolic causes it to return a symbolic, so you can continue creating expressions.

>>> from siuba.siu import _
>>> type(add1(_.a.b) + _.c.d)
<class 'siuba.siu.symbolic.Symbolic'>

symbolic dispatch raises a NotImplementedError by default if it no function f is passed. However, you can override the default as follows:

>>> @symbolic_dispatch
... def my_func(x): raise NotImplementedError("some error message")

siuba.siu.dispatchers.verb_dispatch

siuba.siu.dispatchers.verb_dispatch(cls, f=None)

Wrap singledispatch. Making sure to keep its attributes on the wrapper.

This wrapper has three jobs:
  1. strip symbols off of calls

  2. pass NoArgs instance for calls like some_func(), so dispatcher can handle

  3. return a Pipeable when the first arg of a call is a symbol

Parameters
  • cls – A class to dispatch on.

  • f – A function to call if no classes match while dispatching.

siuba.siu.dispatchers.Pipeable

class siuba.siu.dispatchers.Pipeable(f=None, calls=None)

Enable function composition through the right bitshift (>>) operator.

Parameters
  • f – A function to be called.

  • calls (sequence, optional) – A list-like of functions to be called, with each result chained into the next.

Examples

>>> f = lambda x: x + 1

Eager evaluation:

>>> 1 >> Pipeable(f)
2

Defer to a pipe:

>>> p = Pipeable(f) >> Pipeable(f)
>>> 1 >> p
3
>>> p_undo = p >> (lambda x: x - 3)
>>> 1 >> p_undo
0
>>> from siuba.siu import _
>>> p_undo_sym = p >> (_ - 3)
>>> 1 >> p_undo_sym
0