An open API service indexing awesome lists of open source software.

https://github.com/fastai/fastdispatch

Wrapper for plum dispatch to make it more compatible with fastcore's typedispatch
https://github.com/fastai/fastdispatch

fastai functional-programming multiple-dispatch plum python

Last synced: 10 days ago
JSON representation

Wrapper for plum dispatch to make it more compatible with fastcore's typedispatch

Awesome Lists containing this project

README

          

# fastdispatch

[![CI](https://github.com/fastai/fastdispatch/actions/workflows/test.yaml/badge.svg)](https://github.com/fastai/fastdispatch/actions/workflows/test.yaml)
[![Deploy to GitHub
Pages](https://github.com/fastai/fastdispatch/actions/workflows/deploy.yaml/badge.svg)](https://github.com/fastai/fastdispatch/actions/workflows/deploy.yaml)

Wrapper for plum dispatch to make it more compatible with fastcore’s
typedispatch. Hopefully this is just temporary, and instead the
functionality here will be moved into plum.

## Install

`pip install fastdispatch`

## How to use

`fastdispatch` works just like plum, with a few extensions. We recommend
reading through their [very informative
docs](https://github.com/wesselb/plum), however, here’s a quick example
to get started:

``` python
from fastcore.test import test_fail
from fastdispatch import *
```

Decorate type annotated Python functions with `fastdispatch.dispatch` to
add them as *methods* to a dispatched *function* (following [Julia’s
terminology](https://docs.julialang.org/en/v1/manual/methods/)):

``` python
@dispatch
def f(x: str): return "This is a string!"

@dispatch
def f(x: int): return "This is an integer!"
```

``` python
f('1')
```

'This is a string!'

``` python
f(1)
```

'This is an integer!'

If there’s no matching method, `plum.NotFoundLookupError` is raised:

``` python
test_fail(lambda: f(1.0), contains='For function "f", signature Signature(builtins.float) could not be resolved.')
```