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
- Host: GitHub
- URL: https://github.com/fastai/fastdispatch
- Owner: fastai
- License: apache-2.0
- Created: 2022-07-03T03:06:50.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2022-07-17T12:08:35.000Z (almost 4 years ago)
- Last Synced: 2026-02-11T09:07:41.235Z (3 months ago)
- Topics: fastai, functional-programming, multiple-dispatch, plum, python
- Language: Jupyter Notebook
- Homepage: https://fastai.github.io/fastdispatch/
- Size: 303 KB
- Stars: 3
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# fastdispatch
[](https://github.com/fastai/fastdispatch/actions/workflows/test.yaml)
[](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.')
```