GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - niconunez96/pynction: Functional python

BranchesTags
Open more actions menu

Latest commit

 

History

235 Commits
235 Commits

Folders and files

NameName
Last commit message
Last commit date
.github/workflows
.github/workflows
 
 
docs/pynction
docs/pynction
 
 
pynction
pynction
 
 
tests
tests
 
 
.bandit
.bandit
 
 
.gitignore
.gitignore
 
 
.pre-commit-config.yaml
.pre-commit-config.yaml
 
 
CHANGELOG.md
CHANGELOG.md
 
 
CODE_OF_CONDUCT.md
CODE_OF_CONDUCT.md
 
 
LICENSE
LICENSE
 
 
Makefile
Makefile
 
 
README.md
README.md
 
 
performance_test.py
performance_test.py
 
 
poetry.lock
poetry.lock
 
 
pyproject.toml
pyproject.toml
 
 
samples.py
samples.py
 
 

Repository files navigation

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

About

Functional python

pypi.org/project/pynction/

Topics

functional-programminginmutablelambdasmonadspython

Resources

Readme
MIT license

Code of conduct

Code of conduct
Activity

Stars

5 stars

Watchers

1 watching

Forks

0 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.