Skip to main content

UFO 🛸

uniform functional orchestrators

Delicious monads and functional programming patterns in python!

Check out the api docs for reference

Coverage Badge Static Badge

Project Goals

This library is meant to implement a simple tools for functional programming in python in a way that:

  • Can be intergrated quickly/simply with existing code
  • Maintains type safety
  • Depends only on the standard library

Quick Start

Install libary with pip:

pip install monadcontainers

You can now import monad classes for handling functional chains over data.

Say we want to pass a value through a list of functions like so:

def add_six(x: int) -> int:
  return x + 6

def minus_two(x: int) -> int:
  return x - 2

x = minus_two(add_six(4))  # x == 8

This can be a little messy with long calls, more importantly monads can handle repeated work for us with each call (but we'll cover that in a sec).

A basic identity Monad implementing the above looks like this:

from monadcontainers.monads import Monad

x = (
  Monad(4)
  .bind(add_six)
  .bind(minus_two)
)  # x == Monad(8)

y = x.value  # y == 8

This above pattern chains the Monad contained value through the relevant functions. For alternative syntactical sugar to the bind method, you can use >>:

x = (
  Monad(4)
  >> add_six
  >> minus_two
)  # x == Monad(8)

We can also use Monads to handle additional functionality for us, like error handling with the Result monad:

def divide_by_zero(x: int) -> int:
    return x / 0

x = (
  Result(4)
  >> add_six
  >> divide_by_zero
  >> minus_two
)  # x is a result monad

x.unwrap()  # <- raises divide by zero exception
x.unwrap_or(42)  # <- evaluates to 42
x.value  # <- is None (since exception has been raised)
x.exception  # <- is a ZeroDivisionError
x.recover(add_six)  # <- will apply the given function to the last non-error state

Monad's can be a helpful pattern for functional programming, see the docs for more info on available Monad classes.

Enjoy! 👍

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ufo_tools-0.3.0.tar.gz (55.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ufo_tools-0.3.0-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

Details for the file ufo_tools-0.3.0.tar.gz.

File metadata

  • Download URL: ufo_tools-0.3.0.tar.gz
  • Upload date:
  • Size: 55.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.3

File hashes

Hashes for ufo_tools-0.3.0.tar.gz
Algorithm Hash digest
SHA256 61289051ba8da6f7b8968cbb5490f13e9439623d51fb29528251f89f66b36b58
MD5 874e817c7b2bb4848032c0e28bc55419
BLAKE2b-256 6c4e8b01afa40ee83f4ac15b65e5865db0028c03d1d2d6d14b0d76d4abb67735

See more details on using hashes here.

File details

Details for the file ufo_tools-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: ufo_tools-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 7.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.3

File hashes

Hashes for ufo_tools-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 43a8163aa89c60f7a63d47121e9f813db66d8f1264f766c5944850b117aa5d89
MD5 b557b99cc9310989f92d85c68caa803c
BLAKE2b-256 456c8a2c0d6d7de902b999cff127278a289242fadbd6ae4d9649026c89b10f7e

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.3.0 This release

2 files

0.2.2

2 files

0.2.1

2 files

0.2.0

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page