A cons extension module for Python
Project description
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
cons
lists; and - dicts to
cons
lists 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.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
Hashes for fastcons-0.5.0-cp313-cp313-musllinux_1_2_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 140be273bc8c283e67319f4064e175a97198d4d39ca34375b9665869522d3e7a |
|
MD5 | f032263a5e00441193477eb6ea30da25 |
|
BLAKE2b-256 | 61ebdd925588fa0c847f0190dec6f2b6d3702e07ea23d218b71401b8aab30546 |
Hashes for fastcons-0.5.0-cp313-cp313-musllinux_1_2_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 16c89e5afb99927098e7c92865b4034bfc131f04d9dc597a40b5acda4c9505c7 |
|
MD5 | e21afd9d879bd679669cd50ca5c1b6d5 |
|
BLAKE2b-256 | 36eec1cb622ddd4f76c509409ea5986d40387f3231c649cac99c4b234387b184 |
Hashes for fastcons-0.5.0-cp313-cp313-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6789b6f4f788618f7ef352f419dc05a7114d08441e14509137a7f8c82617a58a |
|
MD5 | 13fae97685e11c0eef917c5c051bd201 |
|
BLAKE2b-256 | b5062e59907654ce2e6f8d59037260aacc00805dd9c9b9f3f814a1de8a151d66 |
Hashes for fastcons-0.5.0-cp313-cp313-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7407d0c68fe5a785873f3301a1a2055ed5a38b7a7b3a58cb10bc9b4a8724f93b |
|
MD5 | 24cf6faa56b78d3aa142e11524a920c5 |
|
BLAKE2b-256 | 2a04fcf52f8edd0015ed786dc3c9b23151046792b08d5e4d33326cd7c845b260 |
Hashes for fastcons-0.5.0-cp313-cp313-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dd486f490a61dcb70c9ac5fde554f743e4daa5cdedbc6240517c2690341dae68 |
|
MD5 | 8dab26ae6c2348a39b2b5b2383884bb4 |
|
BLAKE2b-256 | ab287892a1bc370d28d18e89f3a8df2c4ea5b0a8fe73aa33529b96b06b77c755 |
Hashes for fastcons-0.5.0-cp312-cp312-musllinux_1_2_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 208efb6dc1324dceafc74fbd85c3ece4fe001de6353a0298ba1d55ee6a99748c |
|
MD5 | c91266d875b50b36265e83d51b045538 |
|
BLAKE2b-256 | 76334c391d66999215b031161ce6aeec23732884113edee7ccbe47df80ec81c8 |
Hashes for fastcons-0.5.0-cp312-cp312-musllinux_1_2_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2daf9037f7b4b77434756193f48c86e02810ecde7e495f98f52e989f7f03d5c2 |
|
MD5 | fb294044eada9898aa7229d1bd028e2b |
|
BLAKE2b-256 | 702bd73bef709d7066c182bf8c022ad741949a71f97c96d14397835ae619b985 |
Hashes for fastcons-0.5.0-cp312-cp312-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6a69d20f6b9b7f89798b0e92e6c6bd7e7d38d2624c5d0d9226ca2d5f2ad97964 |
|
MD5 | 86e8300530fe299801164eb7a012f007 |
|
BLAKE2b-256 | 95650276857a8fe8d449278e339f4f20ee48e7314867519eaf5ed0f27b6270da |
Hashes for fastcons-0.5.0-cp312-cp312-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 905dcae823ddc5b4d7841baf4aeb3bb78f3ab1ac6c3669740411983e7d50aaa6 |
|
MD5 | 27c86a7a71675ca1f7b8b300757fcd87 |
|
BLAKE2b-256 | a5bf704243be945ca6f343a94dfc75fd7fafa87316dc2ece9d50e9afbe6f26b4 |
Hashes for fastcons-0.5.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fdcef9400059696946a9262b6dc2b78675de1754eba4ade47a4019f148e2a27f |
|
MD5 | 162e6b3f6098ee9344ec699b6d680581 |
|
BLAKE2b-256 | 6357eafd1c55d5af2adcef889400d7350aff2f5235bb9429474ce772c50c4d6e |