Addind the maybe type to python
Project description
Adds the Maybe type to Python
Classes
- Maybe: A union type that represents a value of Some[T] or None
- Some: A container for a value of type T. Has the functions bind and map
Functions
- with_maybe: A decorator that takes a function, which might throw an exception, and returns a function that returns a Maybe
Examples
from maybe import Maybe, Some
def divide(a: int, b: int) -> Maybe[int]:
if b == 0:
return None
return Some(a // b)
res = divide(10, 2) # Some(5)
res = divide(10, 0) # None
from maybe import with_maybe, Some
@with_maybe
def divide(a: int, b: int) -> int:
return a // b
res = divide(10, 2) # Some(5)
res = divide(10, 0) # None
from maybe import Maybe, Some
def divide(a: int, b: int) -> Maybe[int]:
if b == 0:
return None
return Some(a // b)
def plus_one(a: int) -> int:
return a + 1
def minus_one(a: int) -> Maybe[int]:
return Some(a - 1)
res1 = divide(10, 2).map(plus_one) # Some(6)
res1 = divide(10, 2).bind(plus_one) # Some(Some(6))
res2 = divide(10, 2).bind(minus_one) # Some(4)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
maybe_type-1.0.5.tar.gz
(5.5 kB
view hashes)
Built Distribution
Close
Hashes for maybe_type-1.0.5-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9d3f5011f939f3bcc6cc50427a37f73d38c231689a6d9781cc84bf5f39a4e7dd |
|
MD5 | 4eb8b6b613875a6960d32e8e39d68314 |
|
BLAKE2b-256 | d0b4b05c2c22532c2560d63bf51042993844f544d75c4f88a1e80858e9378004 |