Skip to main content

Functional based library to support monads and other functional programming concepts

Project description

Pynction 🐍

continuous_integration codecov License: MIT

Functional based library to support haskell monads like Either, Maybe in a scala fashion style. The library also contains Try monad inspired from vavr and a Stream class which is pretty similar to scala and java stream API

Inspired in: VΛVR

Why should you use it ?

Probably if you have reached this library you already know something about functional programming and Monads. Well this library is another one that empowers your imperative code to start using functional programming concepts. This type of programming makes your code declarative as long as give you support to the most famous monads like Maybe and Either. These monads make your interfaces explicit for error handling so paraphrasing If it compiles, it works this time it is If mypy is happy, your code works

Basic examples

Stream examples

from pynction import stream_of, stream


foo = (
    stream_of([1, 2, 3, 4])
    .map(lambda a: a + 1)
    .filter(lambda n: n % 2 == 0)
    .flat_map(lambda n: [n, n * 2])
    .to_list()
)

# foo => [2, 4, 4, 8]

bar = (
    stream("example", "e", "something")
    .take_while(lambda s: s.startswith("e"))
    .to_list()
)

# bar => ["example", "e"]

Maybe examples

from pynction import maybe, nothing

def divide_10_by(n: int) -> Maybe[int]:
    if n == 0:
        return nothing
    return maybe(10 / n)

result = divide_10_by(2).get_or_else_get(-1)
# result => 5
result = divide_10_by(0).get_or_else_get(-1)
# result => -1

Try examples

from pynction import try_of


def add_10(n: int) -> int:
    if n > 10:
        raise Exception("n must be less than 10")
    return n + 10

try_example_1 = try_of(lambda: add_10(11)).map(lambda a: a + 1)
try_example_1.on(
    on_success=lambda a: print(f"Result: {a}"),
    on_failure=lambda e: print(f"Error: {e}"),
)
# ==> Will print "Error: n must be less than 10"

try_example_2 = try_of(lambda: add_10(9)).map(lambda a: a + 1)
try_example_2.on(
    on_success=lambda a: print(f"Result: {a}"),
    on_failure=lambda e: print(f"Error: {e}"),
)
# ==> Will print "Result: 20"

Either examples

from pynction import left, right, Either


LESS_THAN_10_LETTERS = Literal["LESS_THAN_10_LETTERS"]
GREATER_THAN_100 = Literal["GREATER_THAN_100"]
WordTransformationError = Literal[LESS_THAN_10_LETTERS, GREATER_THAN_100]

def make_upper_case_first_n_letters(word: str, number: int) -> Either[WordTransformationError, str]:
    if len(word) < 10:
        return left("LESS_THAN_10_LETTERS")
    elif number > 100:
        return left("GREATER_THAN_100")
    else:
        return right(word.upper()[0:number])

result = make_upper_case_first_n_letters("example", 10)
print(result) # ==> Will be Left("LESS_THAN_10_LETTERS")

API

Check the docs

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

pynction-0.4.1.tar.gz (13.5 kB view details)

Uploaded Source

Built Distribution

pynction-0.4.1-py3-none-any.whl (14.7 kB view details)

Uploaded Python 3

File details

Details for the file pynction-0.4.1.tar.gz.

File metadata

  • Download URL: pynction-0.4.1.tar.gz
  • Upload date:
  • Size: 13.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.9.6 readme-renderer/37.3 requests/2.28.1 requests-toolbelt/0.10.1 urllib3/1.26.13 tqdm/4.64.1 importlib-metadata/6.0.0 keyring/23.13.1 rfc3986/2.0.0 colorama/0.4.6 CPython/3.10.9

File hashes

Hashes for pynction-0.4.1.tar.gz
Algorithm Hash digest
SHA256 90e6e4ae78b5605f79e6a6031c46e45c9c858c8d45b3d4e689fc8a74aaa13c82
MD5 72baa50b5273c42a5e1037c3a50dff9d
BLAKE2b-256 9274d665fc1fe24370bea5ba113c6ba4bf82d85311672588b544f072b11ee212

See more details on using hashes here.

File details

Details for the file pynction-0.4.1-py3-none-any.whl.

File metadata

  • Download URL: pynction-0.4.1-py3-none-any.whl
  • Upload date:
  • Size: 14.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.9.6 readme-renderer/37.3 requests/2.28.1 requests-toolbelt/0.10.1 urllib3/1.26.13 tqdm/4.64.1 importlib-metadata/6.0.0 keyring/23.13.1 rfc3986/2.0.0 colorama/0.4.6 CPython/3.10.9

File hashes

Hashes for pynction-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 099afdd50d1f4ba6bf9234798b174c485eab42836795cf1cfa015687932ffa21
MD5 9f29ec94463365a4c127855862c1b699
BLAKE2b-256 92de735c90c11134560f17aa853c2daff83b20de42ba22d78ba219b058c3c7fd

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