Skip to main content

py_monad_do

A simple monad do notation implementation with nice syntax using generators. Also includes sample monads such as Maybe and List.

Sample code:

from monad_do import *

@do(List)
def test_list(a, b):
    x = yield [a * 10, a] # Native lists are coerced into monad Lists here.
    y = yield [b * 100, b * 10, b]
    return [x + y]


print(test_list(3, 4)) # List([430, 70, 34, 403, 43, 7])

Monad instances derive from the Monad class and provide the methods pure(static) and bind. The do decorators binds the values yielded from a generator to its later computation. In a sense, yield works like <- in Haskell.

Note that generators are uncopyable, so if some code needs to be run more than once (such as the case for the List monad), the generator is run from the beginning once again, with the values sent into it recorded to eliminate duplicate computation. This requires that the generators decorated by do to be more or less "pure".

The do decorator is implemented inside monad_do.do_cached. There is also a simpler implementation in monad_do.do_simple which sketches the basic idea, but runs the generator from the beginning for each yield.

The implementation is primarily inspired by these following materials:

Type hints are not incorporated. For now, the weak support for function types (on arguments) makes type hinting more of a burden then something helpful.

Acknowledgements

Thank @danoneata for adding the Reader Monad.

Metadata

Release files for monad-do 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for monad-do 0.1.1
File Size Uploaded
monad_do-0.1.1.tar.gz 7.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for monad-do 0.1.1
File Interpreter ABI Platform
monad_do-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 16.6 kB

Release files / monad_do-0.1.1.tar.gz

Download URL monad_do-0.1.1.tar.gz
Size 7.2 kB
Tags Source
SHA-256 checksum
How to use checksums
bb1f21bffa5303b84233707e317bb8d4ecb359ac92fcc601b1138635752542de
BLAKE2b-256 checksum
How to use checksums
e2aefffed32ee300d0770af1d717e26421372a12e6f860945cdc0840b4b5e7bf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.9.1

Release files / monad_do-0.1.1-py3-none-any.whl

Download URL monad_do-0.1.1-py3-none-any.whl
Size 9.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6ea2c373be6a8a2338a68242227ae6c0ecd3a62ee865947b7a29e133afd94061
BLAKE2b-256 checksum
How to use checksums
7bd447c3e45b2690dbb1ed4108f6514114bc221feb15d6d1e589420682a4dd40
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.9.1

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page