A library that encapsulates callback functions within a continuation monad, utilizing a trampoline scheduler to enable stack-safe computations.
Project description
Continuation-Monad
A Python library implementing stack-safe continuations based on schedulers, ensuring deadlock-free asynchronous computations.
Features
- Trampoline-based: Stack-safe execution of the continuation monad through trampolining
- Continuation certificate: Ensures the execution of a Flowable completes, albeit with some computational overhead.
- Scheduler-based: Explicit control over execution context through different scheduler implementations
- Composable operations: Monadic operators for clean functional pipelines
Installation
You can install Continuation-Monad using pip:
pip install continuationmonad
Example
import continuationmonad
def count_down(count: int):
print(f'{count=}')
if count == 0:
return continuationmonad.from_(count)
else:
# schedule recursive call on the trampoline
return continuationmonad.tail_rec(lambda: count_down(count - 1))
# runs continuation and returns 0
result = continuationmonad.run(
count_down(5)
)
Schedulers and Trampolines
The library provides the following scheduler implementations
init_main_scheduler: Initializes a scheduler that runs in the main thread. It starts execution via therun()method, which blocks untilstop()is called to terminate the scheduler.init_event_loop_scheduler: Creates a scheduler that operates on a dedicated background thread. This is useful for offloading tasks from the main thread or isolating execution contexts.scheduler = continuationmonad.init_event_loop_scheduler()
init_trampoline: Returns a lightweight scheduler that only implements theschedule()method. It does not support delayed execution methods likeschedule_relative()orschedule_absolute(). This scheduler runs tasks immediately in a loop until its queue is exhausted.
Operations
Creating Continuation Monads
defer- creates a continuation monad that defers the subscription until a source is connected (see defer example)from_: Create a continuation monad from a value:c = continuationmonad.from_(5)
get_trampoline- retrieve trampoline associated with the continuation monadc = continuationmonad.get_trampoline()
schedule_on- schedule elements emitted by the source on a dedicated schedulerc = continuationmonad.schedule_on(scheduler)
schedule_trampoline- schedule item on trampolinec = continuationmonad.schedule_trampoline()
tail_rec- recursively call a function on trampoline
Transforming operators
connect- connects the continuation monad to deferred one or multiple subscribers (see defer example)flat_map- apply a function to the item emitted by the source and flattens the resultmap- map the item emitted by the source by applying the given function
Combining operators
zip- create a new continuation monad from two (or more) continuation monads by combining their items in a tuplec = continuationmonad.zip(( continuationmonad.from_(1), continuationmonad.from_(2), ))
Other operators
fork- runs the continuation monad on a specified trampoline (see defer example)c = continuationmonad.fork( source=source, scheduler=scheduler, weight=1, # specify continuation weight )
run- runs the continuation monad on a new trampoline and returns its result# execute a continuation monad result = continuationmonad.run(source)
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file continuationmonad-0.0.3.tar.gz.
File metadata
- Download URL: continuationmonad-0.0.3.tar.gz
- Upload date:
- Size: 19.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
50d4462c4221adbd4b46cbffd8a386870ded4f71e5aa0df256a7a22f8cd4e3db
|
|
| MD5 |
fa7fdf6afda9ede2020ba112c120a383
|
|
| BLAKE2b-256 |
ed2e41658defefa8565a2db6874e854f584b1773ff0b12dd8c91e8daad09f446
|
File details
Details for the file continuationmonad-0.0.3-py3-none-any.whl.
File metadata
- Download URL: continuationmonad-0.0.3-py3-none-any.whl
- Upload date:
- Size: 40.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d7f44382122468bb82c88e5543ab4554dda516800a39ceefa2103adbcc4e2d5b
|
|
| MD5 |
4fa00ce7e0902ae92ef8f9f9d1341ee3
|
|
| BLAKE2b-256 |
8df72f08577ba79b08c5b23c30988f7448135f067998d3a467080c8894941a45
|