Skip to main content

in-line functional composition

Project description

Guide

Composition with |

g(f(x)) == (f | g)(x)

To achieve inline composition, functions must be wrapped with the Composable object (f)

from auttcomp.extensions import Api as f

square = f(lambda x: x ** 2)
add3 = f(lambda x: x + 3)

comp = square | add3
assert comp(3) == 12

Automatic wrapping

If the composition chain starts with a Composable, the rest of the chain is automatically wrapped

from auttcomp.extensions import Api as f

square = f(lambda x: x ** 2)
add3 = lambda x: x + 3

comp = square | add3 | (lambda x: x + 10)
assert comp(3) == 22

Partial Application with &

Consider python's map function - map(func, data)

In this example, & is used to partially apply the square func to map

from auttcomp.extensions import Api as f

square = lambda x: x ** 2
pmap = f(map) & square

assert list(pmap([1, 2, 3])) == [1, 4, 9]

Identity functions and invocation with >

When Composable receives a non-callable type, it constructs an identity function for that data

from auttcomp.extensions import Api as f
from auttcomp.testing.testBase import getHuggingFaceSample

data = getHuggingFaceSample()

idFunc = f(data.models)
justDataModelsAgain = idFunc()

The data in this sample is a search result from the Hugging Face api.

We'll explore the data with a query soon, but first we'd like to know about it's structure. It's difficult to understand the structure of the model just by looking at the raw data, so we'll use the f.shape function to help us understand it.

The f.shape function accepts any data as input, and prints a summary to the console.

When the invocation operator (>) is used, the identity function on the left is invoked first (returning data), and the data is passed as an argument into f.shape

f(data.models) > f.shape

Result:

[ { 'author': 'str',
    'authorData': { '_id': 'str',
                    'avatarUrl': 'str',
                    'followerCount': 'int',
                    'fullname': 'str',
                    'isEnterprise': 'bool',
                    'isHf': 'bool',
                    'isMod': 'bool',
                    'isPro': 'bool',
                    'name': 'str',
                    'type': 'str'},
    'downloads': 'int',
    'gated': 'bool|str',
    'id': 'str',
    'inference': 'str',
    'isLikedByUser': 'bool',
    'lastModified': 'str',
    'likes': 'int',
    'pipeline_tag': 'str',
    'private': 'bool',
    'repoType': 'str',
    'widgetOutputUrls': ['str']}]

Extensions Api

Python already has many common higher order functions (map, filter, reduce, etc). Those functions, and others can be implemented as follows.

#list the author of each model
f(data.models) > f(map) & (lambda x: x.authorData) | f(map) & (lambda x: x.name) | list

However, for convenience, many common functions have been curried and attached to f. So the same query could also be described as...

f(data.models) > f.map(lambda x: x.authorData) | f.map(lambda x: x.name) | list

or even...

getAuthorData = lambda x: x.authorData
getName = lambda x: x.name
comp = f.map & getAuthorData | f.map & getName | list
f(data.models) > comp

Example query

Let's create a list which shows the authors with the most downloads.

f.shape will be used to show the result of the query at each stage.

First, group by author

f(data.models) > f.group(lambda x: x.author) | list | f.shape
[ { 'key': 'str',
    'value': [ { 'author': 'str',
                 'authorData': { '_id': 'str',
                                 'avatarUrl': 'str',
                                 'followerCount': 'int',
                                 'fullname': 'str',
                                 'isEnterprise': 'bool',
                                 'isHf': 'bool',
                                 'isMod': 'bool',
                                 'isPro': 'bool',
                                 'name': 'str',
                                 'type': 'str'},
                 'downloads': 'int',
                 'gated': 'bool|str',
                 'id': 'str',
                 'inference': 'str',
                 'isLikedByUser': 'bool',
                 'lastModified': 'str',
                 'likes': 'int',
                 'pipeline_tag': 'str',
                 'private': 'bool',
                 'repoType': 'str',
                 'widgetOutputUrls': ['str']}]}]

Next, map to tuple of (key, sumDownloads)

(
  f(data.models)
  > f.group(lambda x: x.author)
  | f.map(lambda g: (
      g.key,
      f(g.value) > f.map(lambda x: x.downloads) | sum
  ))
  | list 
  | f.shape
)
[('str', 'int')]

Finally, sort by descending downloads and take the top 5 results

(
  f(data.models)
  > f.group(lambda x: x.author)
  | f.map(lambda g: (
    g.key,
    f(g.value) > f.map(lambda x: x.downloads) | sum
  ))
  | f.sortByDescending(lambda x: x[1])
  | f.take(5)
  | list
)
[('black-forest-labs', 1548084),
 ('deepseek-ai', 1448374),
 ('microsoft', 264891),
 ('unsloth', 142908),
 ('openbmb', 135782)]

Testing

pytest 7.4.3

Misc

developed on Python 3.12.8

No dependencies outside of core python

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

auttcomp-1.0.1.tar.gz (16.6 kB view details)

Uploaded Source

Built Distribution

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

auttcomp-1.0.1-py3-none-any.whl (20.9 kB view details)

Uploaded Python 3

File details

Details for the file auttcomp-1.0.1.tar.gz.

File metadata

  • Download URL: auttcomp-1.0.1.tar.gz
  • Upload date:
  • Size: 16.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for auttcomp-1.0.1.tar.gz
Algorithm Hash digest
SHA256 ac8d5e7ec3b637314b9788986a500a73b11824d3d06bad1ea6a2ef834d46d7e6
MD5 6532f81b9b32a27b2074e087dfb197cb
BLAKE2b-256 c1022094b600863ec8c0cf56b6cd617f4b24062003ab93fe3ad36436f995079a

See more details on using hashes here.

File details

Details for the file auttcomp-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: auttcomp-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 20.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for auttcomp-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 35a6adafc87404a1c63ca67d55d8ecb97cb40d19e92c819295bcefc256664073
MD5 27ef2b2ec33524e6d483e4ea9c0a045f
BLAKE2b-256 0b264ba7ea0cb351351fca1b96ed18086c748208290f8b0b1d569e2ab985967b

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