aspis.common

Common functional programming utilities for Aspis.

This module provides the main public API for the Aspis library, including:

  • Function composition: pipe, compose

  • Currying: curry decorator for automatic partial application

  • Arithmetic operations: add, multiply, divide (all curried)

  • Predicates: all, any, all_pass, any_pass, equals, prop_eq

  • Data manipulation: prop, assoc, adjust

  • List operations: add_index, add_index_right, aperture

  • Function utilities: flip, identity, always, apply, apply_to, apply_spec

All exported functions support currying and are designed for composition.

Example

>>> import aspis.common as A
>>> users = [{'name': 'Alice', 'age': 30}, {'name': 'Bob', 'age': 25}]
>>> find_alice = A.prop_eq('name', 'Alice')
>>> alice = next(filter(find_alice, users))
>>> alice['name']
'Alice'

Modules

add(a, b, /)

Curried addition function.

add_index(func, arr)

Maps a function over a sequence, passing the index as a second argument, allowing the map function to have access to the index of the element being processed.

add_index_right(func, arr)

Maps a function over the reversed sequence, passing both the index and the element to the function.

adjust(idx, func, seq)

Adjusts the element at the specified index in a sequence by applying a function to it.

all(pred, iterable)

Returns True if all elements in the iterable satisfy the predicate.

all_pass(preds, var)

Checks if all predicates in the given sequence return True when applied to the input variable.

always(x)

Returns a function that always returns the same value.

any(fn, arr)

Returns True if any element in the array satisfies the predicate.

any_pass(preds, var)

Checks if any predicate in the given sequence returns True when applied to the input variable.

aperture(n, arr)

Returns a new list containing lists of n consecutive elements from the original sequence.

apply(func, *args, **kwargs)

Alias for functools.partial - partial function application.

apply_spec(spec, *args, **kwargs)

Given a spec object recursively mapping properties to functions, creates a function producing an object of the same structure, by mapping each property to the result of calling its associated function with the supplied arguments.

apply_to(val, func)

Takes a value and applies a function to it.

assoc(k, v, obj)

Associates a value with a key in an object.

compose(*funcs)

Performs right-to-left function composition.

curry(fn)

Transforms a function into a curried version.

divide(a, b, /)

Curried division function.

equals(a, b, /)

Curried equality comparison function.

flip(fn)

Returns a new function much like the supplied one, except that the first two arguments' order is reversed.

identity(x)

The identity function.

multiply(a, b, /)

Curried multiplication function.

pipe(first_func, *funcs)

Performs left-to-right function composition.

prop(p, obj)

Returns the value of property p in object obj if it exists, otherwise return None

prop_eq(p, value, obj)

Returns a check if the specified property of an object equals the given value.