Skip to main content

An implementation of Lisp/Scheme-like cons in Python.

Project description

Build Status Coverage Status PyPI

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 functions cons.core._car and cons.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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cons-0.2.9.tar.gz (21.4 kB view details)

Uploaded Source

Built Distribution

cons-0.2.9-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file cons-0.2.9.tar.gz.

File metadata

  • Download URL: cons-0.2.9.tar.gz
  • Upload date:
  • Size: 21.4 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

Hashes for cons-0.2.9.tar.gz
Algorithm Hash digest
SHA256 67326a6656c0f2121500ff9219b188aedb5c82abcb1e23485761c9383467f789
MD5 b3fc3df3acc2ad6c620c1f65e1b8823c
BLAKE2b-256 04cd36084aadad0b6a9b101a9ab2b557de1fb3a117831cfbac78404a4a07f741

See more details on using hashes here.

File details

Details for the file cons-0.2.9-py3-none-any.whl.

File metadata

  • Download URL: cons-0.2.9-py3-none-any.whl
  • Upload date:
  • Size: 6.1 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

Hashes for cons-0.2.9-py3-none-any.whl
Algorithm Hash digest
SHA256 32687f22ca91ff75abdd32f5022e8d14a595a2370ae6e4d593f8a7d67efaeff5
MD5 7abd230807158161d2bbf21aa7380124
BLAKE2b-256 1d38f1f35f1c3de7eddf38ab1f309150fc79423b66695c8f49a97a7a68a83b2d

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