Skip to content

Python-flavored Option[T] and Result[T, E] types.

Notifications You must be signed in to change notification settings

Enegg/py-monads

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

monads

A small package providing python-flavored Option[T] and Result[T, E] types. Fully statically typed.

features

monads.option:

  • Option[T] = Some[T] | Null
  • Null is an Enum with a single member, null
  • support for match statement:
    match option:
        case Some(value):
            print(value)
    
        case Null.Null:
            ...

monads.result:

  • Result[T, E] = Ok[T] | Err[E]
  • support for match statement:
    match result:
        case Ok(value):
            print(value)
    
        case Err(value):
            print(value)

monads.tools:

  • Turn T | None into Option[T] with from_none
  • Turn exceptions into Null with try_option
  • Turn functions that return T / raise E into Result[T, E] with try_result
  • CatchResult context manager to turn arbitrary block of code into Result

About

Python-flavored Option[T] and Result[T, E] types.

Resources

Stars

Watchers

Forks

Languages