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
cons
behavior can be changed and support for new collections can be added through the generic functionscons.core._car
andcons.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
File details
Details for the file cons-0.4.6.tar.gz
.
File metadata
- Download URL: cons-0.4.6.tar.gz
- Upload date:
- Size: 26.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.17
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 669fe9d5ee916d5e42b9cac6acc911df803d04f2e945c1604982a04d27a29b47 |
|
MD5 | 20d58dac0a00683f3d58f040e964049b |
|
BLAKE2b-256 | 1c9023f2b21206c4dc62edc3ee9c051fe06ef92b9213141498505a3e19b36c09 |