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
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
File details
Details for the file cons-0.1.3.tar.gz
.
File metadata
- Download URL: cons-0.1.3.tar.gz
- Upload date:
- Size: 5.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9d03a5ebf045300803c43de45e01c1a5fd9c71294f9a8ac986e2d7f2bdb05ee6 |
|
MD5 | 4f394013fa833bda843497b899b1a7b7 |
|
BLAKE2b-256 | b62b3bb3752d9ec9b9dd2e804778857970fb2b0086bb2c0a9f4e05b149960b80 |
File details
Details for the file cons-0.1.3-py3-none-any.whl
.
File metadata
- Download URL: cons-0.1.3-py3-none-any.whl
- Upload date:
- Size: 5.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a6a5062549f57cbdf2c9c13639ef5242e4aa331e3f53491f17ab05ab7527f572 |
|
MD5 | 152603bc45cd8c23d380fbfba5986213 |
|
BLAKE2b-256 | 7236e069fd8378cea5c8c8eb6def56fa7fa840949a8dacb9aab85507fc514d2f |