Skip to main content

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


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)

Uploaded Source

Built Distribution

maybe_type-1.0.5-py3-none-any.whl (6.3 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page