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.3.0-cp311-cp311-musllinux_1_1_x86_64.whl (30.6 kB view details)

Uploaded CPython 3.11 musllinux: musl 1.1+ x86-64

fastcons-0.3.0-cp311-cp311-musllinux_1_1_i686.whl (29.6 kB view details)

Uploaded CPython 3.11 musllinux: musl 1.1+ i686

fastcons-0.3.0-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (27.7 kB view details)

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

fastcons-0.3.0-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (26.3 kB view details)

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

fastcons-0.3.0-cp311-cp311-macosx_10_9_x86_64.whl (8.4 kB view details)

Uploaded CPython 3.11 macOS 10.9+ x86-64

File details

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

File metadata

File hashes

Hashes for fastcons-0.3.0-cp311-cp311-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 b5d13d184501075a36db35d26d3ecb89e166ecfa33db99fba49cffa8b501b7c6
MD5 60fc8e815dc468f506840165eaae5346
BLAKE2b-256 5ace69de4ad798d33ec5001aca3ea043a0d744a4b27096044dc4c5119ce4ac40

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastcons-0.3.0-cp311-cp311-musllinux_1_1_i686.whl
Algorithm Hash digest
SHA256 be6fbc732b352ebf3c97d22ef451305971372b73e16494a8747154e1b1fd199b
MD5 c4234bd311edeb0e6eaec1c4c700bd4e
BLAKE2b-256 df33988ddea86919e29fc3c463bfbfc4c6dd01eecacfc578c71db183d4e02f6d

See more details on using hashes here.

File details

Details for the file fastcons-0.3.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.3.0-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 4293f416448d547ec73c6d219771fed3674a08acc4a682618936f4282f7a23a4
MD5 d9d3e8d2c38a1666841aaedd28910966
BLAKE2b-256 8c9b635bedad11cddfbd88bf1033f5be51cb58a0dc9ae8ebc4a345002e7944e3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastcons-0.3.0-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 b67ff19acc6483b700bcb3df44e9011c8d92e0de05edc9e828b3d16f5aae2f58
MD5 ac24a3225edab6ab637e7dac571668ac
BLAKE2b-256 6b7df79f08e8856ea5cd166c56db248c3549889292d42104b1afb7fd4209913c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastcons-0.3.0-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 47491ef76369a12360f6e4a59a68a8f855b45513849e756ef67679f2d797b386
MD5 44cc1e16b2c4cb14379360e1138de0d3
BLAKE2b-256 19e641d81464ed4bd7da7a30369da49274a5c787ce62188c8a9620a82b34fca9

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