An implementation of Lisp/Scheme-like cons in Python.
Project description
Python cons
An implementation of cons in Python.
Usage and Design
The cons package attempts to emulate the semantics of Lisp/Scheme's cons as closely as possible while incorporating all the built-in Python sequence types:
>>> from cons import cons, car, cdr
>>> cons(1, [])
[1]
>>> cons(1, ())
(1,)
>>> cons(1, [2, 3])
[1, 2, 3]
In general, cons is designed to work with collections.abc.Sequence types.
According to the cons package, None corresponds to the empty built-in list, as nil does in some Lisps:
>>> cons(1, None)
[1]
The cons package follows Scheme-like semantics for empty sequences:
>>> car([])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ConsError: Not a cons pair
>>> cdr([])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ConsError: Not a cons pair
By default, str types are not considered cons-pairs, although they are sequences:
>>> cons("a", "string")
ConsPair('a' 'a string')
This setting can be overridden and other types can be similarly excluded from consideration by registering classes with the abc-based classes MaybeCons and NonCons.
Features
- Built-in support for the standard Python ordered sequence types: i.e.
list,tuple,Iterator,OrderedDict.
>>> from collections import OrderedDict
>>> cons(('a', 1), OrderedDict())
OrderedDict([('a', 1)])
- Existing
consbehavior can be changed and support for new collections can be added through the generic functionscons.core._carandcons.core._cdr. - Built-in support for
unification.
>>> from unification import unify, reify, var
>>> unify([1, 2], cons(var('car'), var('cdr')), {})
{~car: 1, ~cdr: [2]}
>>> reify(cons(1, var('cdr')), {var('cdr'): [2, 3]})
[1, 2, 3]
>>> reify(cons(1, var('cdr')), {var('cdr'): None})
[1]
Installation
pip install cons
Development
First obtain the project source:
git clone git@github.com:pythological/python-cons.git
Create a virtual environment and install the development dependencies:
$ pip install -r requirements.txt
Set up pre-commit hooks:
$ pre-commit install --install-hooks
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file cons-0.4.7.tar.gz.
File metadata
- Download URL: cons-0.4.7.tar.gz
- Upload date:
- Size: 8.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0a96cd2abd6a9f494816c1272cf5583a960041750c2d7a48eeeccd47ce369dfd
|
|
| MD5 |
261f61883e3fe2b011a1d8b1f6f18a07
|
|
| BLAKE2b-256 |
ae200eca1dcdbac64a570e60df66119847f94cdd513178d9c222c15101ca1022
|
File details
Details for the file cons-0.4.7-py3-none-any.whl.
File metadata
- Download URL: cons-0.4.7-py3-none-any.whl
- Upload date:
- Size: 8.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e38ee12cf703559ea744c94f725bee0e2329f32daf0249b49db1b0437cc6cb94
|
|
| MD5 |
e8f33127c09d8459040f129eed5128d2
|
|
| BLAKE2b-256 |
a59fbffa3362895e5437d9d12e3bbd242f86d91af1d7cd26f6e14ebb6376581b
|