Skip to main content

An efficient implementation of cons

Project description

fastcons

Fastcons is a Python extension module that provides an efficient implementation of cons lists.

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.11.

Installation

You can install fastcons using pip:

pip install fastcons

Usage

The fastcons module provides two types:

  • nil: represents the empty list; and
  • cons: 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
lst = cons(1, cons(2, cons(3, nil())))

# Create a cons list from a Python sequence
lst2 = cons.from_xs([1, 2, 3])

# Access the head and tail of a cons list
assert lst.head == 1
assert lst.tail.head == 2

# Test for equality
assert lst == lst2

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))

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.

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.

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

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

fastcons-0.2.0-cp311-cp311-musllinux_1_1_x86_64.whl (29.8 kB view details)

Uploaded CPython 3.11 musllinux: musl 1.1+ x86-64

fastcons-0.2.0-cp311-cp311-musllinux_1_1_i686.whl (28.8 kB view details)

Uploaded CPython 3.11 musllinux: musl 1.1+ i686

fastcons-0.2.0-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (27.0 kB view details)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

fastcons-0.2.0-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (25.2 kB view details)

Uploaded CPython 3.11 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

fastcons-0.2.0-cp311-cp311-macosx_10_9_x86_64.whl (8.1 kB view details)

Uploaded CPython 3.11 macOS 10.9+ x86-64

File details

Details for the file fastcons-0.2.0-cp311-cp311-musllinux_1_1_x86_64.whl.

File metadata

File hashes

Hashes for fastcons-0.2.0-cp311-cp311-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 674f6d48d1bed53bb66a0ae6e377d5eb33734fce5f7e3b2ebb785ce2030f50e7
MD5 93348b8e9377219e5853454415d445af
BLAKE2b-256 7a3e32405a47c9f261b44dccbecd877304ec65d6cb522f1a8bdd65d77244c37d

See more details on using hashes here.

File details

Details for the file fastcons-0.2.0-cp311-cp311-musllinux_1_1_i686.whl.

File metadata

File hashes

Hashes for fastcons-0.2.0-cp311-cp311-musllinux_1_1_i686.whl
Algorithm Hash digest
SHA256 e284c54714b54aae55b6f396677ea842398dcb3eabb22b477342bbd51297c262
MD5 5c9b1b65e4487b5a849a773b9f9b3e81
BLAKE2b-256 ba6ecbf0b8516df2fd905d51060cd4bf5c773cb49090d60e3831098286a32a26

See more details on using hashes here.

File details

Details for the file fastcons-0.2.0-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for fastcons-0.2.0-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 68b9deccb1bc36a35183253ad646adace7c0c0f7c75b87a8d7e17f9a7dbd834d
MD5 03348b477bb35fd255e6f938fe625ca8
BLAKE2b-256 c0a9cff4bf4c91acc760c65677c21e4b72c089685426f765f92be9a7e421f8d1

See more details on using hashes here.

File details

Details for the file fastcons-0.2.0-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl.

File metadata

File hashes

Hashes for fastcons-0.2.0-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 94000a41f0d28c5b45f0867de90bfc0c723e521cabc55d0f2355c37fc26b68eb
MD5 c03e1ab13de11aeb55b046a30f889c34
BLAKE2b-256 4d569709d4f7c5d36d20eda357e18ff61635f1206b64b4b6b06d69006db5d205

See more details on using hashes here.

File details

Details for the file fastcons-0.2.0-cp311-cp311-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for fastcons-0.2.0-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 7a421fa1989be8ef32b8ea372e2c5d5232468df1d65aa75afb6ae6e6c8d74d57
MD5 d7c9d39a58993f7f572019daa20f6ecd
BLAKE2b-256 7b77e097fc4d634f95c182fb2242b7d4fac6ee3ff01614b6f7006a96cdf7f482

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page