Skip to main content

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.11, 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; 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
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))))

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.

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

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

Built Distributions

fastcons-0.4.1-cp312-cp312-musllinux_1_1_x86_64.whl (38.9 kB view details)

Uploaded CPython 3.12 musllinux: musl 1.1+ x86-64

fastcons-0.4.1-cp312-cp312-musllinux_1_1_i686.whl (36.9 kB view details)

Uploaded CPython 3.12 musllinux: musl 1.1+ i686

fastcons-0.4.1-cp312-cp312-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (36.4 kB view details)

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

fastcons-0.4.1-cp312-cp312-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (33.8 kB view details)

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

fastcons-0.4.1-cp312-cp312-macosx_10_9_x86_64.whl (10.9 kB view details)

Uploaded CPython 3.12 macOS 10.9+ x86-64

fastcons-0.4.1-cp311-cp311-musllinux_1_1_x86_64.whl (37.9 kB view details)

Uploaded CPython 3.11 musllinux: musl 1.1+ x86-64

fastcons-0.4.1-cp311-cp311-musllinux_1_1_i686.whl (36.0 kB view details)

Uploaded CPython 3.11 musllinux: musl 1.1+ i686

fastcons-0.4.1-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (35.3 kB view details)

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

fastcons-0.4.1-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (33.0 kB view details)

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

fastcons-0.4.1-cp311-cp311-macosx_10_9_x86_64.whl (10.7 kB view details)

Uploaded CPython 3.11 macOS 10.9+ x86-64

File details

Details for the file fastcons-0.4.1-cp312-cp312-musllinux_1_1_x86_64.whl.

File metadata

File hashes

Hashes for fastcons-0.4.1-cp312-cp312-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 9209403ade04ea8757faf36d2a5db0db4092b853830627e950d9897ad28e9cf7
MD5 146d3977b3310aa73e03cf2192899643
BLAKE2b-256 c447ffc7868cfc321b086762e75ce175a3eae7a820813c3d3737b04e0fc04f03

See more details on using hashes here.

File details

Details for the file fastcons-0.4.1-cp312-cp312-musllinux_1_1_i686.whl.

File metadata

File hashes

Hashes for fastcons-0.4.1-cp312-cp312-musllinux_1_1_i686.whl
Algorithm Hash digest
SHA256 e83b81be03032f66617f9cfc1c21c38981f8767ced8df9781e1f93ecd31076be
MD5 f4685871538cecc8f6acc47cf3412dc5
BLAKE2b-256 4215bfcaadd7e6498f1aa515935aca7a0c0ee2f07c8702f34a1cfd0dddce2a4c

See more details on using hashes here.

File details

Details for the file fastcons-0.4.1-cp312-cp312-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.4.1-cp312-cp312-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 bbd2c8e462001c7bc545965303b88c9ce693211a00edfea8a0cd17b0f1b6079b
MD5 5de6ef83fb7fb785ddf31064499a84cf
BLAKE2b-256 ae65961951bba7fa910e345d6dde319924d26e5b4b7a7e6058cbd1e109bf5f5c

See more details on using hashes here.

File details

Details for the file fastcons-0.4.1-cp312-cp312-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl.

File metadata

File hashes

Hashes for fastcons-0.4.1-cp312-cp312-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 634fb6950e7f26d38d7ca31dd020ed9a6ee0e308f5aeab43eb0b5e9d9ecfdf07
MD5 209259ebf3221988af0042320533b40e
BLAKE2b-256 76185735a6d5a533f1049d3694df62f2284381b1577335b20e9a69ee671ee2a1

See more details on using hashes here.

File details

Details for the file fastcons-0.4.1-cp312-cp312-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for fastcons-0.4.1-cp312-cp312-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 bbd3475bbc1aaee8fab59bab086a418a05853e6ed84c7c9870f545ddfae164e7
MD5 b85f352d1ce8cbdfe8af31697e601637
BLAKE2b-256 c894f4947083d04781447a14210def1040d509287383a5fc106a0a29fff0d86c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastcons-0.4.1-cp311-cp311-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 2a0e84c96b30b9fa9c5cd1ba627076376e5de869c623389e4510204eb7532393
MD5 902e3786619c12d01f1ff1a24e51d8f8
BLAKE2b-256 39aac3e493fc461a38df2c4151e22086bf21dbd1a3580361212169495cad7a32

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastcons-0.4.1-cp311-cp311-musllinux_1_1_i686.whl
Algorithm Hash digest
SHA256 e5c776c46b7c62272e189e3c9eccdc17b5b22191a9ff8bc483bae619cb2cc8db
MD5 b442d5b770ba1b8eba1610852e3d5e7f
BLAKE2b-256 d83469003b531f94f7d3e5e4089bec374d261807dd5f0697d6759c5e47cb4ec6

See more details on using hashes here.

File details

Details for the file fastcons-0.4.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.4.1-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 4003cf045d4f06f7aba468add4d934cb95a1981a988285ed12eb626dcde09d94
MD5 60b3db9fdb63858429da10e20214728e
BLAKE2b-256 5e121305654a19820c8038774879bc757e19d1d2085867158ec50580df24b27a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastcons-0.4.1-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 70666f65e1b4a6bb17f6197715d5d3756747969d27a3fa0a2547dcc31e27fffe
MD5 d8262fb84061c1d0f7b923481ed26504
BLAKE2b-256 d544872747a0c3ffccc67d2a8073d6d90a6253448f1b610e6d4ba7163fcefa00

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastcons-0.4.1-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 97328f1e5dcc179d4e1f709020c7d12f912a245aeecd807454273e511bbc3f69
MD5 28147e33ec4e02981cf62fd7c052cfb5
BLAKE2b-256 2aab78ae75e60d50b2d9ed683d3076d8d6c5e83a78faa470357a441fab3a07d3

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