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

Uploaded CPython 3.11 musllinux: musl 1.1+ x86-64

fastcons-0.2.1-cp311-cp311-musllinux_1_1_i686.whl (29.1 kB view details)

Uploaded CPython 3.11 musllinux: musl 1.1+ i686

fastcons-0.2.1-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (27.1 kB view details)

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

fastcons-0.2.1-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (25.6 kB view details)

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

fastcons-0.2.1-cp311-cp311-macosx_10_9_x86_64.whl (8.2 kB view details)

Uploaded CPython 3.11 macOS 10.9+ x86-64

File details

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

File metadata

File hashes

Hashes for fastcons-0.2.1-cp311-cp311-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 d916012c3ca219eefce43765ebbb3aedd416128cedf285bd61224f75d2f89ae3
MD5 aee458b7d097522f1435a86c3add98a5
BLAKE2b-256 16bcd68a7be9803b64a1bbb666638408b00849719fd4c532dba939b7f3f88402

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastcons-0.2.1-cp311-cp311-musllinux_1_1_i686.whl
Algorithm Hash digest
SHA256 9a53d140aaaa3c47f1723aa5215ce470d09dde3713a083113b70fc0a0b5a60a3
MD5 fb18a6b17e370dd88c627b0f8436a3b8
BLAKE2b-256 55f0a86f43b532272383b6c72fd619a4526060ee271daebaf06e22ba0788df8d

See more details on using hashes here.

File details

Details for the file fastcons-0.2.1-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.1-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e2e612bc80e5c0ad22456e784526c5603aed3b87783a08770defded0cb81a678
MD5 071543ee91cdf7f63ce17a2777c088ce
BLAKE2b-256 1a9a6fbb0f7fcc37723e4923e520a989fe3a66e76b1107b88e5727898c4426ea

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastcons-0.2.1-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 10607a0aa07bced7a9b4c8444110bca17bcd9bc836e4a15bde092aa15218b428
MD5 65bf5999ca21073f9ea2a54c2c90e768
BLAKE2b-256 aeec01bdcaf8a660c4fd7dfe45d0a7e04336d253d8e11ae8b6b966416cd13efa

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastcons-0.2.1-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 6516b259013c61c52f04b283e678e8df1ad80f429625bd13c55e3ad6f6c35882
MD5 ab7ae434fbf26854fc1a78d11eb97c8c
BLAKE2b-256 e9efead2d0a4a1c50d1bee790cd46e547dd28abc7fcc1071a8d9d1cbf9d0569f

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