Python 的 cons 扩展模块
项目描述
fastcons
Fastcons 是一个 Python 扩展模块,旨在提供 cons 的有效实现。
fastcons 模块提供两种类型: nil
和 cons
。nil
类型表示空列表,而 cons
类型表示一对 - 包含两个元素的不可变单元格。
目前需要 Python 3.11,以及 Linux 或 MacOS。
安装
您可以使用 pip 安装 fastcons
pip install fastcons
用法
fastcons 模块提供两种类型
nil
:表示空列表;cons
:表示一对。
您可以通过调用 nil()
创建 nil
对象。
您可以通过调用 cons(head, tail)
创建 cons
对象。 cons
可以用来创建链表:如果链表以 nil()
结尾,则一系列 cons
对象被视为列表,例如 cons(1, cons(2, cons(3, nil())))
。
您可以使用 cons.from_xs
方法从 Python 序列高效地创建 cons
列表,其中 xs
是一个序列。
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
cons
对象使用 Lisp 风格的表示法打印,这使得阅读长列表更容易。
>>> 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
可以用于递归地将字典、列表、元组和生成器转换为 cons
对象。字典将被转换为对(关联列表或 alists),其余将被转换为 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))))
模式匹配
支持 PEP 622 模式匹配的 cons
和 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 参考
nil()
返回单例 nil
对象。nil
对象是假的。
cons(head, tail)
返回具有给定 head
和 tail
的 cons
对象。
cons.from_xs(xs)
返回由 Python 序列 xs
创建的 cons
对象。
cons.lift(xs)
递归创建一个 cons
结构,通过将
- 列表、元组和生成器转换为
cons
列表;以及 - 字典转换为
cons
列表对(关联列表)。
assoc(object, alist)
在 alist
中找到第一个 car 等于 object
的对,并返回该对。如果没有找到对,或者 alist
是 nil()
,则返回 nil()
。
assp(predicate, alist)
返回 alist 中第一个满足对其 car 调用 'predicate' 结果为真值的对。'predicate' 将以单个位置参数调用。
许可证
fastcons
采用 MIT 许可证发布。
项目详情
fastcons-0.4.1-cp312-cp312-musllinux_1_1_x86_64.whl 的哈希
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 9209403ade04ea8757faf36d2a5db0db4092b853830627e950d9897ad28e9cf7 |
|
MD5 | 146d3977b3310aa73e03cf2192899643 |
|
BLAKE2b-256 | c447ffc7868cfc321b086762e75ce175a3eae7a820813c3d3737b04e0fc04f03 |
fastcons-0.4.1-cp312-cp312-musllinux_1_1_i686.whl 的哈希
算法 | 哈希摘要 | |
---|---|---|
SHA256 | e83b81be03032f66617f9cfc1c21c38981f8767ced8df9781e1f93ecd31076be |
|
MD5 | f4685871538cecc8f6acc47cf3412dc5 |
|
BLAKE2b-256 | 4215bfcaadd7e6498f1aa515935aca7a0c0ee2f07c8702f34a1cfd0dddce2a4c |
fastcons-0.4.1-cp312-cp312-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl 的哈希
算法 | 哈希摘要 | |
---|---|---|
SHA256 | bbd2c8e462001c7bc545965303b88c9ce693211a00edfea8a0cd17b0f1b6079b |
|
MD5 | 5de6ef83fb7fb785ddf31064499a84cf |
|
BLAKE2b-256 | ae65961951bba7fa910e345d6dde319924d26e5b4b7a7e6058cbd1e109bf5f5c |
fastcons-0.4.1-cp312-cp312-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl 的哈希
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 634fb6950e7f26d38d7ca31dd020ed9a6ee0e308f5aeab43eb0b5e9d9ecfdf07 |
|
MD5 | 209259ebf3221988af0042320533b40e |
|
BLAKE2b-256 | 76185735a6d5a533f1049d3694df62f2284381b1577335b20e9a69ee671ee2a1 |
fastcons-0.4.1-cp312-cp312-macosx_10_9_x86_64.whl 的哈希
算法 | 哈希摘要 | |
---|---|---|
SHA256 | bbd3475bbc1aaee8fab59bab086a418a05853e6ed84c7c9870f545ddfae164e7 |
|
MD5 | b85f352d1ce8cbdfe8af31697e601637 |
|
BLAKE2b-256 | c894f4947083d04781447a14210def1040d509287383a5fc106a0a29fff0d86c |
fastcons-0.4.1-cp311-cp311-musllinux_1_1_x86_64.whl 的哈希
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 2a0e84c96b30b9fa9c5cd1ba627076376e5de869c623389e4510204eb7532393 |
|
MD5 | 902e3786619c12d01f1ff1a24e51d8f8 |
|
BLAKE2b-256 | 39aac3e493fc461a38df2c4151e22086bf21dbd1a3580361212169495cad7a32 |
fastcons-0.4.1-cp311-cp311-musllinux_1_1_i686.whl 的哈希
算法 | 哈希摘要 | |
---|---|---|
SHA256 | e5c776c46b7c62272e189e3c9eccdc17b5b22191a9ff8bc483bae619cb2cc8db |
|
MD5 | b442d5b770ba1b8eba1610852e3d5e7f |
|
BLAKE2b-256 | d83469003b531f94f7d3e5e4089bec374d261807dd5f0697d6759c5e47cb4ec6 |
哈希值 for fastcons-0.4.1-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 4003cf045d4f06f7aba468add4d934cb95a1981a988285ed12eb626dcde09d94 |
|
MD5 | 60b3db9fdb63858429da10e20214728e |
|
BLAKE2b-256 | 5e121305654a19820c8038774879bc757e19d1d2085867158ec50580df24b27a |
哈希值 for fastcons-0.4.1-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 70666f65e1b4a6bb17f6197715d5d3756747969d27a3fa0a2547dcc31e27fffe |
|
MD5 | d8262fb84061c1d0f7b923481ed26504 |
|
BLAKE2b-256 | d544872747a0c3ffccc67d2a8073d6d90a6253448f1b610e6d4ba7163fcefa00 |
哈希值 for fastcons-0.4.1-cp311-cp311-macosx_10_9_x86_64.whl
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 97328f1e5dcc179d4e1f709020c7d12f912a245aeecd807454273e511bbc3f69 |
|
MD5 | 28147e33ec4e02981cf62fd7c052cfb5 |
|
BLAKE2b-256 | 2aab78ae75e60d50b2d9ed683d3076d8d6c5e83a78faa470357a441fab3a07d3 |