Skip to main content

Library for metaprogramming

Project description

Act

Library for dynamic functional programming and more.

Enter this command and go to the documentation:

pip install act4

Overview

from typing import Optional, Callable

from act import *


def division_between(a: int, b: int) -> int | bad[str]:
    if a == 0 or b == 0:
        return bad("division by zero")

    return a / b


WithNumber = type(number=N)

WithMultiplication = type(multiplication=M)
WithDivision = type(division=D)

Result = WithMultiplication[N] & WithDivision[N]


@fbind_by(... |then>> on(None, bad("something is missing")))
@do(maybe, optionally, for_input=optionally)
def func(do: Do, a: WithNumber[Optional[int]], b: WithNumber[Optional[int]]) -> Result[int]:
    maybe, optionally = do

    first_number = optionally.same(a.number)
    second_number = optionally.same(b.number)
    
    division = maybe(division_between)(first_number, second_number)
    multiplication = first_number * second_number

    return Result(multiplication, division)


# As a result, `func` has this type.
func: Callable[
    [Optional[WithNumber[Optional[int]]], Optional[WithNumber[Optional[int]]]],
    Result[int] | bad[str],
]

assert func(WithNumber(16), WithNumber(2)) == obj(multiplication=32, division=8)
assert func(WithNumber(16), WithNumber(0)) == bad("division by zero")
assert func(WithNumber(16), WithNumber(None)) == bad("something is missing")
assert func(WithNumber(16), None) == bad("something is missing")


class RawResult:
    def __init__(self, multiplication: int, division: int) -> None:
        self.multiplication = multiplication
        self.division = division


assert (
    Result(32, 8)
    == RawResult(32, 8)
    == WithMultiplication(32) & WithDivision(8)
    == obj(multiplication=32, division=8)
)

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

act4-3.2.0.tar.gz (80.7 kB view details)

Uploaded Source

File details

Details for the file act4-3.2.0.tar.gz.

File metadata

  • Download URL: act4-3.2.0.tar.gz
  • Upload date:
  • Size: 80.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.0

File hashes

Hashes for act4-3.2.0.tar.gz
Algorithm Hash digest
SHA256 da4482c5f92a88727c671030bf732cba9292e0b7aea4b644c4087ea88b33bfb3
MD5 48bdf0b45929d9601ae9889d270a046f
BLAKE2b-256 abaa6aaa5d9ffa7fe1b784decc03a635720b19a0145b9f6f5f3b5d879ea1d354

See more details on using hashes here.

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