Skip to main content

ag.funutils

Fun functional utilities for python. This library provides a chain method and several operators that you can chain together to create pipelines of functional transformations, like with Lodash's chain or Clojure's threading macros.

The provided functions are "sugary". They do NOT provide performance optimizations, error handling, or guarantees of statelessness.

Each operator will take a transformation and return a function that applies that transformation to an iterable. Because most operators are wrappers around built-in functions like of the same name, they return iterables and the results of most chains will need to be converted to a list to be immediately useful. The documentation for associated standard library functions should largely be applicable to the provided operators.

Examples

Also see the tests.

from ag.funutils import fun

add_one = fun.map(lambda x: x + "1")
upper = fun.map(str.upper)

fun.chain(
    ["a", "b", "c", "d"],
    add_one,
    upper,
    list
) # => ["A1", "B1", "C1", "D1"]

big_transform = [add_one, upper]

fun.chain(
    ["a", "b", "c", "d"],
    *big_transform,
    list
) # => ["A1", "B1", "C1", "D1"])

fun.chain(
    ["a", "b", "c", "d"],
    fun.tap(print), # => "[a, b, c, d]"
    *big_transform,
    fun.tap(print), # => "[A1, B1, C1, D1]"
    fun.sort(reverse=True),
    list
) # => ["D1", "C1", "B1", "A1"]

fun.chain(
    ["a", "b", "c", "d"],
    *big_transform,
    fun.reduce(lambda acc, x: acc + x)
) # => "A1B1C1D1"

# Values that are tuples will be spread into the transformations,
# which lets you work with dicts.

data = {
    'beep': 1,
    'boop': 2,
    'buup': 3,
}

add_one = fun.map(lambda k, v: (k, v + 1))
evens = fun.filter(lambda k, v: v % 2 == 0)
beep_buup = fun.reduce(lambda acc, k, v: f'{acc}{k}{v}', '')

result = fun.chain(
    data.items(),
    add_one,
    evens,
    add_one,
    beep_buup
) # => 'beep3buup5'

Reference

  • ag.funutils.chain(data, *transforms):
    • Provides data as an argument to the first transform, then the result of each transform to the following one. Each transform should be a function that takes a single, iterable argument and returns an iterable. The exception is the reduce operator, which can return a single value if it is the last operator in the chain.
  • ag.funutils.map(transform):
    • returns a function which takes an iterable and applies transform to each item of the iterable
  • ag.funutils.filter(condition):
    • returns a function which takes an iterable and returns an iterable with only items matching the condition
  • ag.funutils.sort(key=None, reverse=False):
    • returns a function which takes an iterable and return an iterable sorted according to the value returned by the key function. Compares items directly by default.
  • ag.funutils.reduce(transform, initial=None):
    • returns a function which takes an iterable and reduces it to a single value. If no initial value is provided, the first item is used as the initial value.
  • ag.funutils.tap(fn):
    • returns a function which takes a single argument, passes it to fn, and returns it. Intended for debugging purposes, in particular: ag.funutils.tap(print).

Development

Requires pipenv and python 3.7.

$ ./scripts/setup.sh
$ ./scripts/test.sh

Metadata

Release files for ag.funutils 1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ag.funutils 1.2
File Size Uploaded Provenance
ag.funutils-1.2.tar.gz 3.4 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for ag.funutils 1.2
File Interpreter ABI Platform
ag.funutils-1.2-py3-none-any.whl Python 3 none any Details

Total release size: 7.8 kB

Release files / ag.funutils-1.2.tar.gz

Download URL ag.funutils-1.2.tar.gz
Size 3.4 kB
Tags Source
SHA-256 checksum
How to use checksums
3e589c48e8461bf7b0fdf28c7859db944129b80cecc6ff834a3e59b29376da52
BLAKE2b-256 checksum
How to use checksums
da6778c46b97e7697642144675acc9942c0e4013d4666d4909816305b0e85816
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.8.0

Release files / ag.funutils-1.2-py3-none-any.whl

Download URL ag.funutils-1.2-py3-none-any.whl
Size 4.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c5b9a53d416ceb3625ac7d686a76eb91f7d829f36df78802b14fccd40b9f4ba1
BLAKE2b-256 checksum
How to use checksums
35a7318a95a7cbeb239b89207b5bd71faf6e886b0808e1b8878edc175ff4bb63
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.8.0

Security for ag.funutils 1.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.2 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page