Simple, functional, composable containers like Maybe
.
Properly typed and supports pattern matching on Python 3.10+.
Inspired by the containers in the Rust standard library.
>>> from cans import Just, Nothing, Maybe
>>> greeting: Maybe[str] = Just("Hello")
...
>>> def first(m: list[str]) -> Maybe[str]:
... return Just(m[0]) if m else Nothing()
...
>>> first(["howdy", "hi", "hello"]).map(str.title).unwrap()
"Howdy"
...
>>> # Python 3.10+ only
>>> match greeting:
... case Just(n):
... print(f"{greeting} world!")
... case Nothing():
... print("Hi world!")
Hello world!
Among the supported methods are flatmap
, filter
, zip
,
as well as the relevant
collection APIs.
See the documentation for a complete overview.
- Other containers