fastcons
Fastcons is a Python extension module that aims to provide an efficient implementation of cons.
The fastcons module provides two types: nil and cons. The nil type represents the empty list, while the cons type represents a pair - an immutable cell containing two elements.
Currently requires Python 3.12, and Linux or MacOS.
Installation
You can install fastcons using pip:
pip install fastcons
Usage
The fastcons module provides two types:
nil: represents the empty list; andcons: represents a pair.
You can create the nil object by calling nil().
You can create a cons object by calling cons(head, tail). cons can be used to create linked lists: a chain of cons objects is considered a list if it is terminated by nil(), e.g. cons(1, cons(2, cons(3, nil()))).
You can efficiently create cons lists from Python sequences using the cons.from_xs method, where xs is a sequence.
from fastcons import cons, nil
# Create a cons list using the cons function
xs = cons(1, cons(2, cons(3, nil())))
# Create a cons list from a Python sequence
ys = cons.from_xs([1, 2, 3])
# Access the head and tail of a cons list
assert xs.head == 1
assert xs.tail.head == 2
# Test for equality
assert xs == ys
The cons objects are printed using Lisp-style notation, which makes it easier to read long lists.
>>> cons.from_xs(range(1, 4))
(1 2 3)
>>> cons("foo", "bar")
('foo' . 'bar')
>>> cons(cons(1, 2), cons(cons(3, 4), nil()))
((1 . 2) (3 . 4))
cons.lift can be used to recursively transform dicts, lists, tuples and generators to cons objects. dicts will be transformed to cons lists of pairs (association lists or alists), the rest will be transformed to alists.
>>> cons.lift({'a': 2, 'b': 3})
(('a' . 2) ('b' . 3))
>>> cons.lift((1, 2, 3))
(1 2 3)
>>> cons.lift(x for x in ('a', 'b', 'c'))
('a' 'b' 'c')
>>> cons.lift({x: {x: y}} for x, y in zip(['a', 'b', 'c'], range(1, 4)))
((('a' ('a' . 1))) (('b' ('b' . 2))) (('c' ('c' . 3))))
The nil object provides a to_list() method that returns an empty Python list:
>>> nil().to_list()
[]
Pattern matching
PEP 622 pattern matching is supported for cons and nil:
>>> match nil():
... case nil():
... print(nil())
...
nil()
>>> match cons(1, nil()):
... case cons(a, d):
... print(f"{a = }, {d = }")
...
a = 1, d = nil()
API Reference
nil()
Returns the singleton nil object. The nil object is falsy.
nil.to_list()
Returns an empty Python list.
cons(head, tail)
Returns a cons object with the given head and tail.
cons.from_xs(xs)
Returns a cons object created from the Python sequence xs.
cons.lift(xs)
Recursively create a cons structure by converting:
- lists, tuples, and generators to
conslists; and - dicts to
conslists of pairs (association lists).
assoc(object, alist)
Find the first pair in alist whose car is equal to object, and return that pair. If no pair is found, or alist is nil(), return nil().
assp(predicate, alist)
Return the first pair in alist for which the result of calling 'predicate' on its car is truthy. 'predicate' will be called with a single positional argument.
License
fastcons is released under the MIT license.
Metadata
Release files for fastcons 0.5.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distributions (wheels)
Total release size: 303.4 kB
Release files / fastcons-0.5.0-cp313-cp313-musllinux_1_2_x86_64.whl
| Download URL | fastcons-0.5.0-cp313-cp313-musllinux_1_2_x86_64.whl |
|---|---|
| Size | 35.6 kB |
| Tags | CPython 3.13 Linux musl 1.2+ x86-64 |
|
SHA-256 checksum How to use checksums |
140be273bc8c283e67319f4064e175a97198d4d39ca34375b9665869522d3e7a
|
|
BLAKE2b-256 checksum How to use checksums |
61ebdd925588fa0c847f0190dec6f2b6d3702e07ea23d218b71401b8aab30546
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.4.27
|
Release files / fastcons-0.5.0-cp313-cp313-musllinux_1_2_i686.whl
| Download URL | fastcons-0.5.0-cp313-cp313-musllinux_1_2_i686.whl |
|---|---|
| Size | 33.5 kB |
| Tags | CPython 3.13 Linux musl 1.2+ x86-32 |
|
SHA-256 checksum How to use checksums |
16c89e5afb99927098e7c92865b4034bfc131f04d9dc597a40b5acda4c9505c7
|
|
BLAKE2b-256 checksum How to use checksums |
36eec1cb622ddd4f76c509409ea5986d40387f3231c649cac99c4b234387b184
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.4.27
|
Release files / fastcons-0.5.0-cp313-cp313-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
| Download URL | fastcons-0.5.0-cp313-cp313-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl |
|---|---|
| Size | 37.0 kB |
| Tags | CPython 3.13 Linux glibc 2.17+ x86-64 Linux glibc 2.5+ x86-64 |
|
SHA-256 checksum How to use checksums |
6789b6f4f788618f7ef352f419dc05a7114d08441e14509137a7f8c82617a58a
|
|
BLAKE2b-256 checksum How to use checksums |
b5062e59907654ce2e6f8d59037260aacc00805dd9c9b9f3f814a1de8a151d66
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.4.27
|
Release files / fastcons-0.5.0-cp313-cp313-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
| Download URL | fastcons-0.5.0-cp313-cp313-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl |
|---|---|
| Size | 34.2 kB |
| Tags | CPython 3.13 Linux glibc 2.17+ x86-32 Linux glibc 2.5+ x86-32 |
|
SHA-256 checksum How to use checksums |
7407d0c68fe5a785873f3301a1a2055ed5a38b7a7b3a58cb10bc9b4a8724f93b
|
|
BLAKE2b-256 checksum How to use checksums |
2a04fcf52f8edd0015ed786dc3c9b23151046792b08d5e4d33326cd7c845b260
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.4.27
|
Release files / fastcons-0.5.0-cp313-cp313-macosx_11_0_arm64.whl
| Download URL | fastcons-0.5.0-cp313-cp313-macosx_11_0_arm64.whl |
|---|---|
| Size | 11.5 kB |
| Tags | CPython 3.13 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
dd486f490a61dcb70c9ac5fde554f743e4daa5cdedbc6240517c2690341dae68
|
|
BLAKE2b-256 checksum How to use checksums |
ab287892a1bc370d28d18e89f3a8df2c4ea5b0a8fe73aa33529b96b06b77c755
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.4.27
|
Release files / fastcons-0.5.0-cp312-cp312-musllinux_1_2_x86_64.whl
| Download URL | fastcons-0.5.0-cp312-cp312-musllinux_1_2_x86_64.whl |
|---|---|
| Size | 35.5 kB |
| Tags | CPython 3.12 Linux musl 1.2+ x86-64 |
|
SHA-256 checksum How to use checksums |
208efb6dc1324dceafc74fbd85c3ece4fe001de6353a0298ba1d55ee6a99748c
|
|
BLAKE2b-256 checksum How to use checksums |
76334c391d66999215b031161ce6aeec23732884113edee7ccbe47df80ec81c8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.4.27
|
Release files / fastcons-0.5.0-cp312-cp312-musllinux_1_2_i686.whl
| Download URL | fastcons-0.5.0-cp312-cp312-musllinux_1_2_i686.whl |
|---|---|
| Size | 33.5 kB |
| Tags | CPython 3.12 Linux musl 1.2+ x86-32 |
|
SHA-256 checksum How to use checksums |
2daf9037f7b4b77434756193f48c86e02810ecde7e495f98f52e989f7f03d5c2
|
|
BLAKE2b-256 checksum How to use checksums |
702bd73bef709d7066c182bf8c022ad741949a71f97c96d14397835ae619b985
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.4.27
|
Release files / fastcons-0.5.0-cp312-cp312-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
| Download URL | fastcons-0.5.0-cp312-cp312-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl |
|---|---|
| Size | 37.0 kB |
| Tags | CPython 3.12 Linux glibc 2.17+ x86-64 Linux glibc 2.5+ x86-64 |
|
SHA-256 checksum How to use checksums |
6a69d20f6b9b7f89798b0e92e6c6bd7e7d38d2624c5d0d9226ca2d5f2ad97964
|
|
BLAKE2b-256 checksum How to use checksums |
95650276857a8fe8d449278e339f4f20ee48e7314867519eaf5ed0f27b6270da
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.4.27
|
Release files / fastcons-0.5.0-cp312-cp312-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
| Download URL | fastcons-0.5.0-cp312-cp312-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl |
|---|---|
| Size | 34.3 kB |
| Tags | CPython 3.12 Linux glibc 2.17+ x86-32 Linux glibc 2.5+ x86-32 |
|
SHA-256 checksum How to use checksums |
905dcae823ddc5b4d7841baf4aeb3bb78f3ab1ac6c3669740411983e7d50aaa6
|
|
BLAKE2b-256 checksum How to use checksums |
a5bf704243be945ca6f343a94dfc75fd7fafa87316dc2ece9d50e9afbe6f26b4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.4.27
|
Release files / fastcons-0.5.0-cp312-cp312-macosx_11_0_arm64.whl
| Download URL | fastcons-0.5.0-cp312-cp312-macosx_11_0_arm64.whl |
|---|---|
| Size | 11.5 kB |
| Tags | CPython 3.12 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
fdcef9400059696946a9262b6dc2b78675de1754eba4ade47a4019f148e2a27f
|
|
BLAKE2b-256 checksum How to use checksums |
6357eafd1c55d5af2adcef889400d7350aff2f5235bb9429474ce772c50c4d6e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.4.27
|
Security for fastcons 0.5.0
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.