Skip to main content

Add Functional Programing to Python

Project description

pional

Functional programming primitives for Python, with full type safety.

Installation

uv add pional  # or pip install pional

Quick Example

from pional.adts import Result, Ok, Err
from pional.collections import Vec
from pional.iterators import Range

def divide(a: int, b: int) -> Result[float, str]:
    if b == 0:
        return Err("cannot divide by zero")
    return Ok(a / b)

first = Range(1, 100)
second = Range(1, 100)

result: Vec[float] = (
    first
    .rev()
    .zip(second)
    .filter_map(lambda ab: divide(*ab).ok())
    .collect(Vec)
)

Modules

pional.adts — Algebraic Data Types

  • Result[T, E] = Ok[T] | Err[E] — success/failure

    • unwrap(), unwrap_or(default), unwrap_err() (Err only)
    • map(f), map_err(f), and_then(f)
    • map_or(default, f), map_or_else(default_fn, f)
    • combine(other), combine_with(other, f)
    • ok()Option[T], err()Option[E]
    • is_ok(), is_err()
  • Option[T] = Some[T] | Nothing — optional value

    • unwrap(), unwrap_or(default)
    • map(f), and_then(f)
    • ok_or(err), ok_or_else(err_fn)Result[T, E]
    • is_some(), is_none()

pional.collections — Collections

  • Vec[T] — immutable-style vector (wraps list)

    • into_iter()Iter[T]
    • append(item), extend(items), insert(index, item)
    • pop()Option[T], get(index)Option[T]
    • sort(), sort_by(key)
  • HashMap[K, V] — hash map (wraps dict, extends MutableMapping)

    • insert(k, v), remove(k)Option[V], contains(k)
    • keys()Iter[K], values()Iter[V]
    • into_iter()Iter[tuple[K, V]]
  • HashSet[T] — hash set (wraps set, extends MutableSet)

    • insert(x), remove(x), contains(x)
    • add(x), discard(x)
    • into_iter()Iter[T]

pional.iterators — Iterator Combinators

  • Iter[T], Range — constructors

  • Lazy combinators: map, filter, filter_map, flat_map, flatten, enumerate, take, skip, chain, zip, inspect, peekable, rev, dedup, unique, cycle, chunks(n), windows(n)

  • Terminal methods: collect(Vec|HashSet|HashMap), count, first, last, nth, for_each, sum, product, min, max, fold, reduce, all, any, find, join, sorted(), sorted_by(key), partition(pred), group_by(key_fn)

pional.fn — Function Utilities

  • PipePipe(x).pipe(fn1).pipe(fn2)
  • ComposeCompose(fn1).compose(fn2)(x) (right-to-left)
  • identity(x) — returns x
  • constant(x) — returns a function that always returns x

Design

  • Fully type-annotated with Python 3.14+ generics (PEP 695)
  • ADTs use match-compatible __match_args__ for pattern matching
  • Iterators are lazy where possible
  • No external dependencies

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

pional-0.1.0.tar.gz (10.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pional-0.1.0-py3-none-any.whl (18.8 kB view details)

Uploaded Python 3

File details

Details for the file pional-0.1.0.tar.gz.

File metadata

  • Download URL: pional-0.1.0.tar.gz
  • Upload date:
  • Size: 10.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for pional-0.1.0.tar.gz
Algorithm Hash digest
SHA256 52f23a14bcd309ed396f6d6d848e9db0ba17de43a202c087a89ef417de65094b
MD5 87fee97596e70a919d3c34c752393e3f
BLAKE2b-256 0633d8cf5f2727761b2084aafccdd7cda47b32ac4daf36cf6f31beb286ba57ff

See more details on using hashes here.

File details

Details for the file pional-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: pional-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 18.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for pional-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 aac047f41ec1b325d84a20eed5d89311453687b7b3df921e4d9b4b9745be8638
MD5 860001d8059cef933d5bc1739a49c8dc
BLAKE2b-256 77140911576007b79e25c8a5d31a37de720d50f9d6bb803728a1b7cdbe5fc021

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page